8000 [13.x] Use the `oauth_scopes` property of the bearer token on `TokenGuard` by hafezdivandari · Pull Request #1755 · laravel/passport · GitHub
[go: up one dir, main page]

Skip to content

[13.x] Use the oauth_scopes property of the bearer token on TokenGuard #1755

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

Merged
merged 3 commits into from
Jun 20, 2024

Conversation

hafezdivandari
Copy link
Contributor

Fixes #382

As discussed here, this PR fixes a long-standing issue #382.

The TokenGuard authenticates the user via bearer token and assigns this token to the authenticated user. This will be useful when the developer wants to check if the token has a given scope using the tokenCan method on the authenticated App\Models\User instance:

$request->user()->tokenCan('place-orders')

The problem is that the TokenGuard guard (and also CheckClientCredentials middleware) unnecessarily query the DB to find the token's record and retrieve its scopes, but we have already decoded the token and we can simply use the oauth_scopes property instead.

This PR fixes the issue by separating the logic into new Passport\AccessToken class from the Passport\Token model, so we don't need to query the DB to find the token. This can be considered as performance optimization and also a security fix!

Upgrade Guide

There shouldn't be any breaking change here, the user()->tokenCan() is the only documented functionality and works like before. The user()->token() returns TransientToken if the user is authenticated via the cookie just like before, but now returns Passport\AccessToken instance instead of Passport\Token instance when the user is authenticated via bearer token, but to avoid any breaking change, this class dynamically forwards method calls to Passport\Token.

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@hafezdivandari hafezdivandari changed the title [13.x] Add a new AccessToken class [13.x] Use the ouath_scopes property of the access token on TokenGuard and CheckClientCredentials middleware Jun 17, 2024
@hafezdivandari hafezdivandari changed the title [13.x] Use the ouath_scopes property of the access token on TokenGuard and CheckClientCredentials middleware [13.x] Use the oauth_scopes property of the bearer token on TokenGuard Jun 17, 2024
@hafezdivandari hafezdivandari marked this pull request as ready for review June 17, 2024 14:26
@taylorotwell
Copy link
Member

Is it not possible for the scopes to be manipulated by the user if we are using the scopes present on the request attributes?

@hafezdivandari
Copy link
Contributor Author
hafezdivandari commented Jun 19, 2024

@taylorotwell the oauth_scopes are not on the request attributes, but encrypted inside the bearer token. We should only trust the values on the signed / enceypted token itself, the user has authorized this client with these scopes. On the token guard, we decode the token and use oauth_access_token_id to determine if the token exists and not revoked, oauth_user_id to find the user, oauth_client_id to find the client and we should use oauth_scopes to determine the authorized scopes too.

@taylorotwell taylorotwell merged commit 22b3861 into laravel:13.x Jun 20, 2024
9 checks passed
@hafezdivandari hafezdivandari deleted the 13.x-new-access-token branch June 20, 2024 15:17
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

Successfully merging this pull request may close these issues.

2 participants
0