Skip to content

Commit 5352e34

Browse files
fine-pinejgrandja
authored andcommitted
Disallow usage of the openid scope in device authorization requests
Closes gh-2177 Signed-off-by: fine-pine <[email protected]>
1 parent 2ec9c2f commit 5352e34

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 the original author or authors.
2+
* Copyright 2020-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
3939
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
4040
import org.springframework.security.oauth2.core.OAuth2UserCode;
4141
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
42+
import org.springframework.security.oauth2.core.oidc.OidcScopes;
4243
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
4344
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
4445
import org.springframework.security.oauth2.server.authorization.OAuth2TokenType;
@@ -120,6 +121,9 @@ public Authentication authenticate(Authentication authentication) throws Authent
120121
throwError(OAuth2ErrorCodes.INVALID_SCOPE, OAuth2ParameterNames.SCOPE);
121122
}
122123
}
124+
if (requestedScopes.contains(OidcScopes.OPENID)) {
125+
throwError(OAuth2ErrorCodes.INVALID_SCOPE, OAuth2ParameterNames.SCOPE);
126+
}
123127
}
124128

125129
if (this.logger.isTraceEnabled()) {

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 the original author or authors.
2+
* Copyright 2020-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
3535
import org.springframework.security.oauth2.core.OAuth2UserCode;
3636
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
37+
import org.springframework.security.oauth2.core.oidc.OidcScopes;
3738
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
3839
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
3940
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
@@ -165,6 +166,23 @@ public void authenticateWhenInvalidScopesThenThrowOAuth2AuthenticationException(
165166
// @formatter:on
166167
}
167168

169+
@Test
170+
public void authenticateWhenOpenIdScopeThenThrowOAuth2AuthenticationException() {
171+
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
172+
.authorizationGrantType(AuthorizationGrantType.DEVICE_CODE)
173+
.scope(OidcScopes.OPENID)
174+
.build();
175+
Authentication authentication = createAuthentication(registeredClient);
176+
// @formatter:off
177+
assertThatExceptionOfType(OAuth2AuthenticationException.class)
178+
.isThrownBy(() -> this.authenticationProvider.authenticate(authentication))
179+
.withMessageContaining(OAuth2ParameterNames.SCOPE)
180+
.extracting(OAuth2AuthenticationException::getError)
181+
.extracting(OAuth2Error::getErrorCode)
182+
.isEqualTo(OAuth2ErrorCodes.INVALID_SCOPE);
183+
// @formatter:on
184+
}
185+
168186
@Test
169187
public void authenticateWhenDeviceCodeIsNullThenThrowOAuth2AuthenticationException() {
170188
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)