Skip to content

Commit c0446c6

Browse files
committed
Polish gh-1552
1 parent 2b7da9f commit c0446c6

File tree

3 files changed

+62
-123
lines changed

3 files changed

+62
-123
lines changed

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

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2024 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.
@@ -19,6 +19,7 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121
import java.util.function.Consumer;
22+
import java.util.function.Predicate;
2223

2324
import org.springframework.lang.Nullable;
2425
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
@@ -28,13 +29,14 @@
2829

2930
/**
3031
* An {@link OAuth2AuthenticationContext} that holds an {@link OAuth2AuthorizationCodeRequestAuthenticationToken} and additional information
31-
* and is used when validating the OAuth 2.0 Authorization Request used in the Authorization Code Grant.
32+
* and is used when validating the OAuth 2.0 Authorization Request parameters, as well as, determining if authorization consent is required.
3233
*
3334
* @author Joe Grandja
3435
* @since 0.4.0
3536
* @see OAuth2AuthenticationContext
3637
* @see OAuth2AuthorizationCodeRequestAuthenticationToken
3738
* @see OAuth2AuthorizationCodeRequestAuthenticationProvider#setAuthenticationValidator(Consumer)
39+
* @see OAuth2AuthorizationCodeRequestAuthenticationProvider#setAuthorizationConsentRequired(Predicate)
3840
*/
3941
public final class OAuth2AuthorizationCodeRequestAuthenticationContext implements OAuth2AuthenticationContext {
4042
private final Map<Object, Object> context;
@@ -66,22 +68,24 @@ public RegisteredClient getRegisteredClient() {
6668
}
6769

6870
/**
69-
* Returns the {@link OAuth2AuthorizationRequest oauth2 authorization request}.
71+
* Returns the {@link OAuth2AuthorizationRequest authorization request}.
7072
*
7173
* @return the {@link OAuth2AuthorizationRequest}
74+
* @since 1.3
7275
*/
7376
@Nullable
74-
public OAuth2AuthorizationRequest getOAuth2AuthorizationRequest() {
77+
public OAuth2AuthorizationRequest getAuthorizationRequest() {
7578
return get(OAuth2AuthorizationRequest.class);
7679
}
7780

7881
/**
79-
* Returns the {@link OAuth2AuthorizationConsent oauth2 authorization consent}.
82+
* Returns the {@link OAuth2AuthorizationConsent authorization consent}.
8083
*
8184
* @return the {@link OAuth2AuthorizationConsent}
85+
* @since 1.3
8286
*/
8387
@Nullable
84-
public OAuth2AuthorizationConsent getOAuth2AuthorizationConsent() {
88+
public OAuth2AuthorizationConsent getAuthorizationConsent() {
8589
return get(OAuth2AuthorizationConsent.class);
8690
}
8791

@@ -116,22 +120,22 @@ public Builder registeredClient(RegisteredClient registeredClient) {
116120
}
117121

118122
/**
119-
* Sets the {@link OAuth2AuthorizationRequest oauth2 authorization request}.
123+
* Sets the {@link OAuth2AuthorizationRequest authorization request}.
120124
*
121125
* @param authorizationRequest the {@link OAuth2AuthorizationRequest}
122126
* @return the {@link Builder} for further configuration
123-
* @since 1.3.0
127+
* @since 1.3
124128
*/
125129
public Builder authorizationRequest(OAuth2AuthorizationRequest authorizationRequest) {
126130
return put(OAuth2AuthorizationRequest.class, authorizationRequest);
127131
}
128132

129133
/**
130-
* Sets the {@link OAuth2AuthorizationConsent oauth2 authorization consent}.
134+
* Sets the {@link OAuth2AuthorizationConsent authorization consent}.
131135
*
132136
* @param authorizationConsent the {@link OAuth2AuthorizationConsent}
133137
* @return the {@link Builder} for further configuration
134-
* @since 1.3.0
138+
* @since 1.3
135139
*/
136140
public Builder authorizationConsent(OAuth2AuthorizationConsent authorizationConsent) {
137141
return put(OAuth2AuthorizationConsent.class, authorizationConsent);

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

+25-37
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public final class OAuth2AuthorizationCodeRequestAuthenticationProvider implemen
8181
private OAuth2TokenGenerator<OAuth2AuthorizationCode> authorizationCodeGenerator = new OAuth2AuthorizationCodeGenerator();
8282
private Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> authenticationValidator =
8383
new OAuth2AuthorizationCodeRequestAuthenticationValidator();
84-
private Predicate<OAuth2AuthorizationCodeRequestAuthenticationContext> requiresAuthorizationConsent;
84+
private Predicate<OAuth2AuthorizationCodeRequestAuthenticationContext> authorizationConsentRequired =
85+
OAuth2AuthorizationCodeRequestAuthenticationProvider::isAuthorizationConsentRequired;
8586

8687
/**
8788
* Constructs an {@code OAuth2AuthorizationCodeRequestAuthenticationProvider} using the provided parameters.
@@ -98,7 +99,6 @@ public OAuth2AuthorizationCodeRequestAuthenticationProvider(RegisteredClientRepo
9899
this.registeredClientRepository = registeredClientRepository;
99100
this.authorizationService = authorizationService;
100101
this.authorizationConsentService = authorizationConsentService;
101-
this.requiresAuthorizationConsent = this::requireAuthorizationConsent;
102102
}
103103

104104
@Override
@@ -117,11 +117,10 @@ public Authentication authenticate(Authentication authentication) throws Authent
117117
this.logger.trace("Retrieved registered client");
118118
}
119119

120-
OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext =
120+
OAuth2AuthorizationCodeRequestAuthenticationContext.Builder authenticationContextBuilder =
121121
OAuth2AuthorizationCodeRequestAuthenticationContext.with(authorizationCodeRequestAuthentication)
122-
.registeredClient(registeredClient)
123-
.build();
124-
this.authenticationValidator.accept(authenticationContext);
122+
.registeredClient(registeredClient);
123+
this.authenticationValidator.accept(authenticationContextBuilder.build());
125124

126125
if (!registeredClient.getAuthorizationGrantTypes().contains(AuthorizationGrantType.AUTHORIZATION_CODE)) {
127126
if (this.logger.isDebugEnabled()) {
@@ -170,23 +169,15 @@ public Authentication authenticate(Authentication authentication) throws Authent
170169
.state(authorizationCodeRequestAuthentication.getState())
171170
.additionalParameters(authorizationCodeRequestAuthentication.getAdditionalParameters())
172171
.build();
172+
authenticationContextBuilder.authorizationRequest(authorizationRequest);
173173

174174
OAuth2AuthorizationConsent currentAuthorizationConsent = this.authorizationConsentService.findById(
175175
registeredClient.getId(), principal.getName());
176-
177-
OAuth2AuthorizationCodeRequestAuthenticationContext.Builder authenticationContextBuilder =
178-
OAuth2AuthorizationCodeRequestAuthenticationContext.with(authorizationCodeRequestAuthentication)
179-
.registeredClient(registeredClient)
180-
.authorizationRequest(authorizationRequest);
181-
182176
if (currentAuthorizationConsent != null) {
183177
authenticationContextBuilder.authorizationConsent(currentAuthorizationConsent);
184178
}
185179

186-
OAuth2AuthorizationCodeRequestAuthenticationContext contextWithAuthorizationRequestAndAuthorizationConsent =
187-
authenticationContextBuilder.build();
188-
189-
if (requiresAuthorizationConsent.test(contextWithAuthorizationRequestAndAuthorizationConsent)) {
180+
if (this.authorizationConsentRequired.test(authenticationContextBuilder.build())) {
190181
String state = DEFAULT_STATE_GENERATOR.generateKey();
191182
OAuth2Authorization authorization = authorizationBuilder(registeredClient, principal, authorizationRequest)
192183
.attribute(OAuth2ParameterNames.STATE, state)
@@ -280,47 +271,44 @@ public void setAuthenticationValidator(Consumer<OAuth2AuthorizationCodeRequestAu
280271
}
281272

282273
/**
283-
* Sets the {@link Predicate} used to determine if authorization consent is required.
274+
* Sets the {@code Predicate} used to determine if authorization consent is required.
284275
*
285276
* <p>
286277
* The {@link OAuth2AuthorizationCodeRequestAuthenticationContext} gives the predicate access to the {@link OAuth2AuthorizationCodeRequestAuthenticationToken},
287278
* as well as, the following context attributes:
288-
* {@link OAuth2AuthorizationCodeRequestAuthenticationContext#getRegisteredClient()} containing {@link RegisteredClient} used to make the request.
289-
* {@link OAuth2AuthorizationCodeRequestAuthenticationContext#getOAuth2AuthorizationRequest()} containing {@link OAuth2AuthorizationRequest}.
290-
* {@link OAuth2AuthorizationCodeRequestAuthenticationContext#getOAuth2AuthorizationConsent()} containing {@link OAuth2AuthorizationConsent} granted in the request.
279+
* <ul>
280+
* <li>The {@link RegisteredClient} associated with the authorization request.</li>
281+
* <li>The {@link OAuth2AuthorizationRequest} containing the authorization request parameters.</li>
282+
* <li>The {@link OAuth2AuthorizationConsent} previously granted to the {@link RegisteredClient}, or {@code null} if not available.</li>
283+
* </ul>
291284
*
292-
* @param requiresAuthorizationConsent the {@link Predicate} that determines if authorization consent is required.
293-
* @since 1.3.0
285+
* @param authorizationConsentRequired the {@code Predicate} used to determine if authorization consent is required
286+
* @since 1.3
294287
*/
295-
public void setRequiresAuthorizationConsent(Predicate<OAuth2AuthorizationCodeRequestAuthenticationContext> requiresAuthorizationConsent) {
296-
Assert.notNull(requiresAuthorizationConsent, "requiresAuthorizationConsent cannot be null");
297-
this.requiresAuthorizationConsent = requiresAuthorizationConsent;
288+
public void setAuthorizationConsentRequired(Predicate<OAuth2AuthorizationCodeRequestAuthenticationContext> authorizationConsentRequired) {
289+
Assert.notNull(authorizationConsentRequired, "authorizationConsentRequired cannot be null");
290+
this.authorizationConsentRequired = authorizationConsentRequired;
298291
}
299292

300-
private boolean requireAuthorizationConsent(OAuth2AuthorizationCodeRequestAuthenticationContext context) {
301-
RegisteredClient registeredClient = context.getRegisteredClient();
302-
if (!registeredClient.getClientSettings().isRequireAuthorizationConsent()) {
293+
private static boolean isAuthorizationConsentRequired(OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext) {
294+
if (!authenticationContext.getRegisteredClient().getClientSettings().isRequireAuthorizationConsent()) {
303295
return false;
304296
}
305-
306-
OAuth2AuthorizationRequest authorizationRequest = context.getOAuth2AuthorizationRequest();
307297
// 'openid' scope does not require consent
308-
if (authorizationRequest.getScopes().contains(OidcScopes.OPENID) &&
309-
authorizationRequest.getScopes().size() == 1) {
298+
if (authenticationContext.getAuthorizationRequest().getScopes().contains(OidcScopes.OPENID) &&
299+
authenticationContext.getAuthorizationRequest().getScopes().size() == 1) {
310300
return false;
311301
}
312302

313-
OAuth2AuthorizationConsent authorizationConsent = context.getOAuth2AuthorizationConsent();
314-
if (authorizationConsent != null &&
315-
authorizationConsent.getScopes().containsAll(authorizationRequest.getScopes())) {
303+
if (authenticationContext.getAuthorizationConsent() != null &&
304+
authenticationContext.getAuthorizationConsent().getScopes().containsAll(authenticationContext.getAuthorizationRequest().getScopes())) {
316305
return false;
317306
}
318307

319308
return true;
320309
}
321310

322-
private static OAuth2Authorization.Builder authorizationBuilder(RegisteredClient registeredClient,
323-
Authentication principal,
311+
private static OAuth2Authorization.Builder authorizationBuilder(RegisteredClient registeredClient, Authentication principal,
324312
OAuth2AuthorizationRequest authorizationRequest) {
325313
return OAuth2Authorization.withRegisteredClient(registeredClient)
326314
.principalName(principal.getName())

0 commit comments

Comments
 (0)