Is it possible to add custom claims into an access token at the moment of requesting that?
I mean, by default authorization server adds its claims, but in mu case, I'd like request an access token requesting for additional custom claims.
Is it possible?
I'm trying that using nimbusds library:
Here my code:
/**
* Obtains an OAuth2 access token using the client credentials grant.
*
* @param clientId the client ID to authenticate with the token endpoint
* @param clientSecret the client secret to authenticate with the token endpoint
* @return the access token value as a string
*/
public String getToken(String clientId, String clientSecret) throws URISyntaxException, ParseException, IOException {
// Construct the client credentials grant
AuthorizationGrant clientGrant = new ClientCredentialsGrant();
// The credentials to authenticate the client at the token endpoint
ClientID clientID = new ClientID(clientId);
Secret clientSECRET= new Secret(clientSecret);
ClientAuthentication clientAuth = new ClientSecretBasic(clientID, clientSECRET);
// The request scope for the token (may be optional)
// Scope scope = new Scope("core");
// The token endpoint
URI tokenEndpoint = new URI("http://localhost:5444/oauth2/token");
// URI tokenEndpoint = new URI("http://localhost:8081/realms/master/protocol/openid-connect/token");
// Make the token request
TokenRequest request = new TokenRequest(tokenEndpoint, clientAuth, clientGrant, null, null, Map.of("custom", List.of("custom")));
TokenResponse response = TokenResponse.parse(request.toHTTPRequest().send());
if (! response.indicatesSuccess()) {
// We got an error response...
TokenErrorResponse errorResponse = response.toErrorResponse();
log.info("errorResponse: {}", errorResponse.toString());
}
AccessTokenResponse successResponse = response.toSuccessResponse();
// Get the access token
AccessToken accessToken = successResponse.getTokens().getAccessToken();
log.info("accessToken: {}", accessToken.toJSONString());
return accessToken.getValue();
}
As you can see, I'm trying to add custom values at this code line:
TokenRequest request = new TokenRequest(tokenEndpoint, clientAuth, clientGrant, null, null, Map.of("custom", List.of("custom")));
But token received doesn't have custom claims requested.
Any ideas?