Replies: 2 comments
|
The token is not signed with the personal access client's secret. The decoded header says If you need to verify the signature manually, verify the original first two JWT segments without decoding and re-encoding them: function base64UrlDecode(string $value): string
{
$padding = (4 - strlen($value) % 4) % 4;
return base64_decode(
strtr($value, '-_', '+/').str_repeat('=', $padding),
true
);
}
[$header, $payload, $signature] = explode('.', $accessToken);
$publicKey = file_get_contents(storage_path('oauth-public.key'));
$result = openssl_verify(
$header.'.'.$payload,
base64UrlDecode($signature),
$publicKey,
OPENSSL_ALGO_SHA256,
);
if ($result !== 1) {
throw new RuntimeException('Invalid access-token signature.');
}The public key is normally generated with Passport's Do not decode and then re-encode the header or payload before verification: JWT signatures cover the exact Base64URL text that was issued, and a semantically identical JSON object can have different bytes. A valid signature alone is not sufficient for authorization. You must also validate time and audience/issuer claims as applicable, scopes, and whether Passport considers the token revoked. For an API endpoint, the safer option is to let Passport's guard / League OAuth2 resource-server middleware perform the complete validation instead of implementing only the signature check. Passport key documentation: https://laravel.com/docs/11.x/passport#deploying-passport |
|
I don't think you should manually verify the Passport access token using Laravel Passport already handles access-token validation through its authentication guard. You can protect your API route with Passport: Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:api');Then send the token as a Bearer token: Authorization: Bearer YOUR_ACCESS_TOKENPassport will handle the token parsing and signature/expiration/revocation checks for you. If you really need to inspect or verify the JWT manually, you should use a JWT library and verify it using Passport's configured public key and the algorithm specified by the token, rather than recreating the signature with So the main issue with the current implementation is that it is trying to reproduce Passport's token verification logic manually. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hello,
I'm having problems with Laravel Passport and more specifically with the generation of Personal Access Tokens.
I'm currently using Laravel Passport and the classic version of OAuth 2 to authorise access to user data for third-party applications.
I now want to generate Personal Access Tokens for another client.
Implementation is quick and efficient, and I can generate tokens without any problems.
Out of curiosity I wanted to check the signature but unfortunately I can't manage to generate the same signature manually.
Here is a sample of the code i used to test :
The token was generated via the /oauth/personal-access-tokens route.
Any ideas ? algorithm used or function to verify the signature is wrong ?
Thanks
All reactions