'Laravel passport get token from database
I am using a Laravel version 5.5 and using Passport for authentication. I have created token using:
$token = $user->createToken('string')->accessToken;
it generates the token with 1075 characters with the entry in 'oauth_access_tokens'
table having the id of 80 characters.
How can I get the 1075 character token from the database using the using 80 character token?
Solution 1:[1]
(This was tested on Laravel 8).
The code used to generate the JWT token can be found in League\OAuth2\Server\Entities\Traits\AccessTokenTrait
.
which looks like this:
/**
* Generate a JWT from the access token
*
* @return Token
*/
private function convertToJWT()
{
$this->initJwtConfiguration();
return $this->jwtConfiguration->builder()
->permittedFor($this->getClient()->getIdentifier())
->identifiedBy($this->getIdentifier())
->issuedAt(new DateTimeImmutable())
->canOnlyBeUsedAfter(new DateTimeImmutable())
->expiresAt($this->getExpiryDateTime())
->relatedTo((string) $this->getUserIdentifier())
->withClaim('scopes', $this->getScopes())
->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey());
}
Therefore in order to re-create your JWT token using the data found in oauth_access_tokens
table, use the following code:
$oauthClientId = 1; // CHANGE! The ID of oauth client found in "oauth_clients" table
$tokenId = "..." // CHANGE! The 80 character ID found in "oauth_access_tokens"
$clientRepository = app(\Laravel\Passport\Bridge\ClientRepository::class);
$clientEntity = $clientRepository->getClientEntity($personalAccessClientId);
$token = Token::query()->where('id', $tokenId)->firstOrFail();
$issuedAt = Carbon::createFromFormat('Y-m-d H:i:s', $token->created_at)
->toDateTimeImmutable();
$expiresAt = Carbon::createFromFormat('Y-m-d H:i:s', $token->expires_at)
->toDateTimeImmutable();
$scopes = collect($token->scopes)->map(function ($id) {
return new \Laravel\Passport\Scope($id);
})->all();
$jwtConfiguration = \Lcobucci\JWT\Configuration::forAsymmetricSigner(
new Sha256(),
LocalFileReference::file(Passport::keyPath('oauth-private.key'), ''),
InMemory::plainText('')
);
$jwtToken = $jwtConfiguration->builder()
->permittedFor($clientEntity->getIdentifier())
->identifiedBy($tokenId)
->issuedAt($issuedAt)
->canOnlyBeUsedAfter($issuedAt)
->expiresAt($expiresAt)
->relatedTo((string)$token->user_id)
->withClaim('scopes', $scopes)
->getToken($jwtConfiguration->signer(), $jwtConfiguration->signingKey());
$token = (string)$jwtToken;
Solution 2:[2]
I haven't managed to find a way to get access_token from my passport, but I found the solution to generate a token on your own. It's part of the issues on Github. You could check the solution in the link, hopefully, it worked for me.
You could also check the generation of access_token in the passport repository link
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Yahya Uddin |
Solution 2 | sivan |