[13.x] Use the oauth_scopes
property of the bearer token on TokenGuard
#1755
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 thetokenCan
method on the authenticatedApp\Models\User
instance:The problem is that the
TokenGuard
guard (and alsoCheckClientCredentials
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 theoauth_scopes
property instead.This PR fixes the issue by separating the logic into new
Passport\AccessToken
class from thePassport\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. Theuser()->token()
returnsTransientToken
if the user is authenticated via the cookie just like before, but now returnsPassport\AccessToken
instance instead ofPassport\Token
instance when the user is authenticated via bearer token, but to avoid any breaking change, this class dynamically forwards method calls toPassport\Token
.