Ask Your Question

Revision history [back]

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.