To extract the scope of the present JWT token while inside the User Checker in Symfony, you can use the following code:
// Retrieve the token object from the request
$token = $this->tokenStorage->getToken();
if ($token !== null && $token instanceof JWTUserToken) {
// Get the JWT token from the user token
$jwtToken = $token->getCredentials();
// Get the token claims
$claims = $jwtToken->getClaims();
// Extract the scope claim from the token claims
if (isset($claims['scope'])) {
$scope = $claims['scope'];
}
}
Here, we first retrieve the token object from the request using the TokenStorage service. If the token is not null and is an instance of JWTUserToken, we extract the JWT token from it using the getCredentials()
method.
We then get the token claims using the getClaims()
method and extract the scope claim from it. Finally, we can use the scope value as required.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-11-04 11:00:00 +0000
Seen: 9 times
Last updated: Oct 05 '22