Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieving a personal access token after it's been created #552

Closed
qng5150 opened this issue Oct 31, 2017 · 12 comments
Closed

Retrieving a personal access token after it's been created #552

qng5150 opened this issue Oct 31, 2017 · 12 comments

Comments

@qng5150
Copy link

qng5150 commented Oct 31, 2017

My current workflow is:

  1. After user registered, automatically create a Personal Access Token (PAT) to access the API
  2. However once the token is created, how are we able to display the PAT again.
    // Creating a token with scopes... $token = $user->createToken('Pricing Token', ['get-pricing])->accessToken;

Essentially it would be good for the user, once they have logged in to my secured site to download the Token at any time to use on their own site. Essentially I'm using the PAT to identify the user so don't need the full OAUTH workflow.

Reading through the source code, I'm able to regenerate the Access token, but how are we able to retrieve the original Token generated in Step 2.

@peyobr
Copy link

peyobr commented Nov 11, 2017

+1

1 similar comment
@Mont4
Copy link

Mont4 commented Apr 21, 2018

+1

@FrittenKeeZ
Copy link

I created this trait to grab the latest valid token for a user, with the default personal access client.

<?php

namespace App\Models\Traits;

use Laravel\Passport\ClientRepository;
use Laravel\Passport\Token;
use Laravel\Passport\TokenRepository;

trait AccessToken
{
    /**
     * Get personal access token for user.
     *
     * @return \Laravel\Passport\Token|null
     */
    public function getToken(): ?Token
    {
        return app(TokenRepository::class)->findValidToken(
            $this,
            app(ClientRepository::class)->personalAccessClient()
        );
    }
}

@judasane
Copy link

Why not acces it from database?

@driesvints
Copy link
Member

driesvints commented Oct 18, 2018

Hey there. There's a token and tokens method on the HasApiTokens trait. Is this what you need? Please note that these aren't filtered on revoked etc.

@mikebronner
Copy link

@driesvints it seems those two methods don't include personal access tokens?

@kevinb1989
Copy link

Is it possible for me to retrieve the ACCESS TOKEN string from a Laravel\Passport\Token object?

@julesgraus
Copy link

julesgraus commented Nov 12, 2019

Is it possible for me to retrieve the ACCESS TOKEN string from a Laravel\Passport\Token object?

I would like to know this too! In passport it is pretty easy to create tokens and clients, but not so easy (not documented well enough) to get tokens after they are created.

#edit. I want to get back at my above statement. It seems that some (if not all) services (GitHub too) issue you a personal access token only once. Then you need to store it safely yourself. They usually wil let you delete previously generated tokens, but you cannot view them again. I think that this is just how it is meant to work.

@VishalParkash
Copy link

Why not acces it from database?

Do you have code for this task ?

@rddewan
Copy link

rddewan commented Sep 1, 2021

I am using Laravel 8 and Passport 10.1 . is there any to retrieve a Personal Access Token after it has been created ?

@marwan2
Copy link

marwan2 commented Mar 12, 2023

This thread discussion doesn't reply to who ask to get created Personal Access Token yet ?
I'm having the same concern

@BourneSuper
Copy link

BourneSuper commented Oct 13, 2023

can not reuse a token in server side, the created token is only save by client. And server saved hash( token ), which is not reverseable.

HasApiTokens.php

    public function createToken(string $name, array $abilities = ['*'], DateTimeInterface $expiresAt = null)
    {
        $plainTextToken = sprintf(
            '%s%s%s',
            config('sanctum.token_prefix', ''),
            $tokenEntropy = Str::random(40),
            hash('crc32b', $tokenEntropy)
        );

        $token = $this->tokens()->create([
            'name' => $name,
            'token' => hash('sha256', $plainTextToken),
            'abilities' => $abilities,
            'expires_at' => $expiresAt,
        ]);

        return new NewAccessToken($token, $token->getKey().'|'.$plainTextToken);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests