Skip to content

Commit c00226d

Browse files
committed
Store authorizedScopes attribute for client_credentials grant
Issue gh-213
1 parent 6ffda38 commit c00226d

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2ClientCredentialsAuthenticationProvider.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,22 @@ public Authentication authenticate(Authentication authentication) throws Authent
102102
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.UNAUTHORIZED_CLIENT));
103103
}
104104

105-
Set<String> scopes = registeredClient.getScopes(); // Default to configured scopes
105+
Set<String> authorizedScopes = registeredClient.getScopes(); // Default to configured scopes
106106
if (!CollectionUtils.isEmpty(clientCredentialsAuthentication.getScopes())) {
107107
Set<String> unauthorizedScopes = clientCredentialsAuthentication.getScopes().stream()
108108
.filter(requestedScope -> !registeredClient.getScopes().contains(requestedScope))
109109
.collect(Collectors.toSet());
110110
if (!CollectionUtils.isEmpty(unauthorizedScopes)) {
111111
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_SCOPE));
112112
}
113-
scopes = new LinkedHashSet<>(clientCredentialsAuthentication.getScopes());
113+
authorizedScopes = new LinkedHashSet<>(clientCredentialsAuthentication.getScopes());
114114
}
115115

116116
String issuer = this.providerSettings != null ? this.providerSettings.issuer() : null;
117117

118118
JoseHeader.Builder headersBuilder = JwtUtils.headers();
119119
JwtClaimsSet.Builder claimsBuilder = JwtUtils.accessTokenClaims(
120-
registeredClient, issuer, clientPrincipal.getName(), scopes);
120+
registeredClient, issuer, clientPrincipal.getName(), authorizedScopes);
121121

122122
// @formatter:off
123123
JwtEncodingContext context = JwtEncodingContext.with(headersBuilder, claimsBuilder)
@@ -137,7 +137,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
137137

138138
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
139139
jwtAccessToken.getTokenValue(), jwtAccessToken.getIssuedAt(),
140-
jwtAccessToken.getExpiresAt(), scopes);
140+
jwtAccessToken.getExpiresAt(), authorizedScopes);
141141

142142
// @formatter:off
143143
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(registeredClient)
@@ -146,6 +146,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
146146
.token(accessToken,
147147
(metadata) ->
148148
metadata.put(OAuth2Authorization.Token.CLAIMS_METADATA_NAME, jwtAccessToken.getClaims()))
149+
.attribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME, authorizedScopes)
149150
.build();
150151
// @formatter:on
151152

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2ClientCredentialsAuthenticationProviderTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ public void authenticateWhenValidAuthenticationThenReturnAccessToken() {
207207
assertThat(authorization.getPrincipalName()).isEqualTo(clientPrincipal.getName());
208208
assertThat(authorization.getAuthorizationGrantType()).isEqualTo(AuthorizationGrantType.CLIENT_CREDENTIALS);
209209
assertThat(authorization.getAccessToken()).isNotNull();
210-
assertThat(authorization.getAccessToken().getToken().getScopes()).isEqualTo(clientPrincipal.getRegisteredClient().getScopes());
210+
assertThat(authorization.<Set<String>>getAttribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME)).isNotNull();
211+
assertThat(authorization.getAccessToken().getToken().getScopes())
212+
.isEqualTo(authorization.getAttribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME));
211213
assertThat(accessTokenAuthentication.getPrincipal()).isEqualTo(clientPrincipal);
212214
assertThat(accessTokenAuthentication.getAccessToken()).isEqualTo(authorization.getAccessToken().getToken());
213215
}

0 commit comments

Comments
 (0)