-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Description
- How can I return the information of the custom token?
The OAuth2TokenIntrospectionAuthenticationProvider authentication provider cannot fully return token information, for example, I added custom authorities information. This is important to me because the resource server needs it to extract the corresponding permissions for the token
- code
private static OAuth2TokenIntrospection withActiveTokenClaims(
OAuth2Authorization.Token<AbstractOAuth2Token> authorizedToken, RegisteredClient authorizedClient) {
OAuth2TokenIntrospection.Builder tokenClaims = OAuth2TokenIntrospection.builder(true)
.clientId(authorizedClient.getClientId());
// TODO Set "username"
AbstractOAuth2Token token = authorizedToken.getToken();
if (token.getIssuedAt() != null) {
tokenClaims.issuedAt(token.getIssuedAt());
}
if (token.getExpiresAt() != null) {
tokenClaims.expiresAt(token.getExpiresAt());
}
if (OAuth2AccessToken.class.isAssignableFrom(token.getClass())) {
OAuth2AccessToken accessToken = (OAuth2AccessToken) token;
tokenClaims.scopes(scopes -> scopes.addAll(accessToken.getScopes()));
tokenClaims.tokenType(accessToken.getTokenType().getValue());
Map<String, Object> claims = authorizedToken.getClaims();
if (!CollectionUtils.isEmpty(claims)) {
// Assuming JWT as it's the only (currently) supported access token format
JwtClaimAccessor jwtClaims = () -> claims;
Instant notBefore = jwtClaims.getNotBefore();
if (notBefore != null) {
tokenClaims.notBefore(notBefore);
}
tokenClaims.subject(jwtClaims.getSubject());
List<String> audience = jwtClaims.getAudience();
if (!CollectionUtils.isEmpty(audience)) {
tokenClaims.audiences(audiences -> audiences.addAll(audience));
}
tokenClaims.issuer(jwtClaims.getIssuer().toExternalForm());
String jti = jwtClaims.getId();
if (StringUtils.hasText(jti)) {
tokenClaims.id(jti);
}
}
}
return tokenClaims.build();
}
- return
{
"active": true,
"client_id": "system",
"iat": 1636896937,
"exp": 1636900537,
"scope": "all",
"token_type": "Bearer",
"nbf": 1636896937,
"sub": "admin",
"aud": [
"system"
],
"iss": "http://127.0.0.1:9000"
}
- should return
{
"sub": "admin",
"aud": "system",
"nbf": 1636896937,
"user_id": 1,
"scope": [
"all"
],
"iss": "http://127.0.0.1:9000",
"exp": 1636900537,
"iat": 1636896937,
"authorities": [
"ROLE_admin"
]
}
endink and 0x676e67
Metadata
Metadata
Assignees
Labels
type: enhancementA general enhancementA general enhancement