Skip to content

Commit e324369

Browse files
author
Greg Li
committed
Provide a configuration "enableDeviceAuthorizationEndpoint" to support enable/disable device authorization grant. The default value of enableDeviceAuthorizationEndpoint is true for backward capability.
1 parent d33e4d2 commit e324369

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

Diff for: oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/config/annotation/web/configurers/OAuth2AuthorizationServerConfigurer.java

+17-22
Original file line numberDiff line numberDiff line change
@@ -222,40 +222,26 @@ public OAuth2AuthorizationServerConfigurer tokenRevocationEndpoint(Customizer<OA
222222
}
223223

224224
/**
225-
* Configures the OAuth 2.0 Device Authorization Endpoint (disabled by default).
225+
* Configures the OAuth 2.0 Device Authorization Endpoint.
226226
*
227227
* @param deviceAuthorizationEndpointCustomizer the {@link Customizer} providing access to the {@link OAuth2DeviceAuthorizationEndpointConfigurer}
228228
* @return the {@link OAuth2AuthorizationServerConfigurer} for further configuration
229229
* @since 1.1
230230
*/
231231
public OAuth2AuthorizationServerConfigurer deviceAuthorizationEndpoint(Customizer<OAuth2DeviceAuthorizationEndpointConfigurer> deviceAuthorizationEndpointCustomizer) {
232-
OAuth2DeviceAuthorizationEndpointConfigurer deviceAuthorizationEndpointConfigurer =
233-
getConfigurer(OAuth2DeviceAuthorizationEndpointConfigurer.class);
234-
if (deviceAuthorizationEndpointConfigurer == null) {
235-
addConfigurer(OAuth2DeviceAuthorizationEndpointConfigurer.class,
236-
new OAuth2DeviceAuthorizationEndpointConfigurer(this::postProcess));
237-
deviceAuthorizationEndpointConfigurer = getConfigurer(OAuth2DeviceAuthorizationEndpointConfigurer.class);
238-
}
239-
deviceAuthorizationEndpointCustomizer.customize(deviceAuthorizationEndpointConfigurer);
232+
deviceAuthorizationEndpointCustomizer.customize(getConfigurer(OAuth2DeviceAuthorizationEndpointConfigurer.class));
240233
return this;
241234
}
242235

243236
/**
244-
* Configures the OAuth 2.0 Device Verification Endpoint (disabled by default).
237+
* Configures the OAuth 2.0 Device Verification Endpoint.
245238
*
246239
* @param deviceVerificationEndpointCustomizer the {@link Customizer} providing access to the {@link OAuth2DeviceVerificationEndpointConfigurer}
247240
* @return the {@link OAuth2AuthorizationServerConfigurer} for further configuration
248241
* @since 1.1
249242
*/
250243
public OAuth2AuthorizationServerConfigurer deviceVerificationEndpoint(Customizer<OAuth2DeviceVerificationEndpointConfigurer> deviceVerificationEndpointCustomizer) {
251-
OAuth2DeviceVerificationEndpointConfigurer deviceVerificationEndpointConfigurer =
252-
getConfigurer(OAuth2DeviceVerificationEndpointConfigurer.class);
253-
if (deviceVerificationEndpointConfigurer == null) {
254-
addConfigurer(OAuth2DeviceVerificationEndpointConfigurer.class,
255-
new OAuth2DeviceVerificationEndpointConfigurer(this::postProcess));
256-
deviceVerificationEndpointConfigurer = getConfigurer(OAuth2DeviceVerificationEndpointConfigurer.class);
257-
}
258-
deviceVerificationEndpointCustomizer.customize(deviceVerificationEndpointConfigurer);
244+
deviceVerificationEndpointCustomizer.customize(getConfigurer(OAuth2DeviceVerificationEndpointConfigurer.class));
259245
return this;
260246
}
261247

@@ -325,6 +311,10 @@ public void init(HttpSecurity httpSecurity) {
325311
}
326312
});
327313
}
314+
if (!isDeviceAuthorizationEnabled()) {
315+
this.configurers.remove(OAuth2DeviceAuthorizationEndpointConfigurer.class);
316+
this.configurers.remove(OAuth2DeviceVerificationEndpointConfigurer.class);
317+
}
328318

329319
List<RequestMatcher> requestMatchers = new ArrayList<>();
330320
this.configurers.values().forEach(configurer -> {
@@ -338,7 +328,7 @@ public void init(HttpSecurity httpSecurity) {
338328
ExceptionHandlingConfigurer<HttpSecurity> exceptionHandling = httpSecurity.getConfigurer(ExceptionHandlingConfigurer.class);
339329
if (exceptionHandling != null) {
340330
OrRequestMatcher preferredRequestMatcher = null;
341-
if (getRequestMatcher(OAuth2DeviceAuthorizationEndpointConfigurer.class) != null) {
331+
if (isDeviceAuthorizationEnabled()) {
342332
preferredRequestMatcher = new OrRequestMatcher(
343333
getRequestMatcher(OAuth2TokenEndpointConfigurer.class),
344334
getRequestMatcher(OAuth2TokenIntrospectionEndpointConfigurer.class),
@@ -359,9 +349,7 @@ public void init(HttpSecurity httpSecurity) {
359349

360350
@Override
361351
public void configure(HttpSecurity httpSecurity) {
362-
OAuth2DeviceAuthorizationEndpointConfigurer deviceAuthorizationEndpointConfigurer =
363-
getConfigurer(OAuth2DeviceAuthorizationEndpointConfigurer.class);
364-
if (deviceAuthorizationEndpointConfigurer != null) {
352+
if (isDeviceAuthorizationEnabled()) {
365353
OAuth2AuthorizationServerMetadataEndpointConfigurer auth2AuthorizationServerMetadataEndpointConfigurer =
366354
getConfigurer(OAuth2AuthorizationServerMetadataEndpointConfigurer.class);
367355

@@ -395,6 +383,11 @@ private boolean isOidcEnabled() {
395383
return getConfigurer(OidcConfigurer.class) != null;
396384
}
397385

386+
private boolean isDeviceAuthorizationEnabled() {
387+
OAuth2DeviceAuthorizationEndpointConfigurer deviceAuthorizationEndpointConfigurer = getConfigurer(OAuth2DeviceAuthorizationEndpointConfigurer.class);
388+
return deviceAuthorizationEndpointConfigurer != null && deviceAuthorizationEndpointConfigurer.isEnableDeviceAuthorizationEndpoint();
389+
}
390+
398391
private Map<Class<? extends AbstractOAuth2Configurer>, AbstractOAuth2Configurer> createConfigurers() {
399392
Map<Class<? extends AbstractOAuth2Configurer>, AbstractOAuth2Configurer> configurers = new LinkedHashMap<>();
400393
configurers.put(OAuth2ClientAuthenticationConfigurer.class, new OAuth2ClientAuthenticationConfigurer(this::postProcess));
@@ -403,6 +396,8 @@ private Map<Class<? extends AbstractOAuth2Configurer>, AbstractOAuth2Configurer>
403396
configurers.put(OAuth2TokenEndpointConfigurer.class, new OAuth2TokenEndpointConfigurer(this::postProcess));
404397
configurers.put(OAuth2TokenIntrospectionEndpointConfigurer.class, new OAuth2TokenIntrospectionEndpointConfigurer(this::postProcess));
405398
configurers.put(OAuth2TokenRevocationEndpointConfigurer.class, new OAuth2TokenRevocationEndpointConfigurer(this::postProcess));
399+
configurers.put(OAuth2DeviceAuthorizationEndpointConfigurer.class, new OAuth2DeviceAuthorizationEndpointConfigurer(this::postProcess));
400+
configurers.put(OAuth2DeviceVerificationEndpointConfigurer.class, new OAuth2DeviceVerificationEndpointConfigurer(this::postProcess));
406401
return configurers;
407402
}
408403

Diff for: oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/config/annotation/web/configurers/OAuth2DeviceAuthorizationEndpointConfigurer.java

+11
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ public final class OAuth2DeviceAuthorizationEndpointConfigurer extends AbstractO
6262
private Consumer<List<AuthenticationProvider>> authenticationProvidersConsumer = (authenticationProviders) -> {};
6363
private AuthenticationSuccessHandler deviceAuthorizationResponseHandler;
6464
private AuthenticationFailureHandler errorResponseHandler;
65+
66+
public boolean isEnableDeviceAuthorizationEndpoint() {
67+
return enableDeviceAuthorizationEndpoint;
68+
}
69+
6570
private String verificationUri;
71+
private boolean enableDeviceAuthorizationEndpoint = true;
6672

6773
/**
6874
* Restrict for internal use only.
@@ -161,6 +167,11 @@ public OAuth2DeviceAuthorizationEndpointConfigurer verificationUri(String verifi
161167
return this;
162168
}
163169

170+
public OAuth2DeviceAuthorizationEndpointConfigurer enableDeviceAuthorizationEndpoint(boolean enableDeviceAuthorizationEndpoint) {
171+
this.enableDeviceAuthorizationEndpoint = enableDeviceAuthorizationEndpoint;
172+
return this;
173+
}
174+
164175
@Override
165176
public void init(HttpSecurity builder) {
166177
AuthorizationServerSettings authorizationServerSettings =

Diff for: samples/demo-authorizationserver/src/main/java/sample/config/AuthorizationServerConfig.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ public SecurityFilterChain authorizationServerSecurityFilterChain(
100100

101101
// @formatter:off
102102
http.getConfigurer(OAuth2AuthorizationServerConfigurer.class)
103-
.deviceAuthorizationEndpoint(deviceAuthorizationEndpoint ->
104-
deviceAuthorizationEndpoint.verificationUri("/activate")
103+
.deviceAuthorizationEndpoint(deviceAuthorizationEndpoint -> {
104+
deviceAuthorizationEndpoint.verificationUri("/activate");
105+
deviceAuthorizationEndpoint.enableDeviceAuthorizationEndpoint(true);
106+
}
105107
)
106108
.deviceVerificationEndpoint(deviceVerificationEndpoint ->
107109
deviceVerificationEndpoint.consentPage(CUSTOM_CONSENT_PAGE_URI)

0 commit comments

Comments
 (0)