Skip to content

Commit 61621c5

Browse files
yonyesjgrandja
authored andcommitted
Default authorized scope to empty for client_credentials grant
Closes gh-780
1 parent 532cade commit 61621c5

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.security.oauth2.server.authorization.authentication;
1717

18+
import java.util.Collections;
1819
import java.util.LinkedHashSet;
1920
import java.util.Set;
2021

@@ -87,7 +88,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
8788
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.UNAUTHORIZED_CLIENT);
8889
}
8990

90-
Set<String> authorizedScopes = registeredClient.getScopes(); // Default to configured scopes
91+
Set<String> authorizedScopes = Collections.EMPTY_SET; // Empty by default
9192
if (!CollectionUtils.isEmpty(clientCredentialsAuthentication.getScopes())) {
9293
for (String requestedScope : clientCredentialsAuthentication.getScopes()) {
9394
if (!registeredClient.getScopes().contains(requestedScope)) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,22 @@ public void authenticateWhenScopeRequestedThenAccessTokenContainsScope() {
211211
assertThat(accessTokenAuthentication.getAccessToken().getScopes()).isEqualTo(requestedScope);
212212
}
213213

214+
@Test
215+
public void authenticateWhenNoScopeRequestedThenAccessTokenNotContainsAnyScope() {
216+
RegisteredClient registeredClient = TestRegisteredClients.registeredClient2().build();
217+
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(
218+
registeredClient, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, registeredClient.getClientSecret());
219+
OAuth2ClientCredentialsAuthenticationToken authentication =
220+
new OAuth2ClientCredentialsAuthenticationToken(clientPrincipal, null, null);
221+
222+
when(this.jwtEncoder.encode(any()))
223+
.thenReturn(createJwt(Collections.singleton("mapped-scoped")));
224+
225+
OAuth2AccessTokenAuthenticationToken accessTokenAuthentication =
226+
(OAuth2AccessTokenAuthenticationToken) this.authenticationProvider.authenticate(authentication);
227+
assertThat(accessTokenAuthentication.getAccessToken().getScopes()).isEmpty();
228+
}
229+
214230
@Test
215231
public void authenticateWhenAccessTokenNotGeneratedThenThrowOAuth2AuthenticationException() {
216232
RegisteredClient registeredClient = TestRegisteredClients.registeredClient2().build();

0 commit comments

Comments
 (0)