Skip to content

Commit 9b60ed2

Browse files
committed
Polish OAuth2AuthorizationConsentAuthenticationContext
Issue gh-470
1 parent 332d1cc commit 9b60ed2

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ private Authentication authenticateAuthorizationConsent(Authentication authentic
355355
if (this.authorizationConsentCustomizer != null) {
356356
// @formatter:off
357357
OAuth2AuthorizationConsentAuthenticationContext authorizationConsentAuthenticationContext =
358-
OAuth2AuthorizationConsentAuthenticationContext.with(authorizationCodeRequestAuthentication, authorizationConsentBuilder)
358+
OAuth2AuthorizationConsentAuthenticationContext.with(authorizationCodeRequestAuthentication)
359+
.authorizationConsent(authorizationConsentBuilder)
359360
.registeredClient(registeredClient)
360361
.authorization(authorization)
361362
.authorizationRequest(authorizationRequest)

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.Map;
1919

20-
import org.springframework.security.core.Authentication;
2120
import org.springframework.security.oauth2.core.authentication.OAuth2AuthenticationContext;
2221
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
2322
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
@@ -78,25 +77,32 @@ public OAuth2AuthorizationRequest getAuthorizationRequest() {
7877
}
7978

8079
/**
81-
* Constructs a new {@link Builder} with the provided {@link Authentication} and {@link OAuth2AuthorizationConsent.Builder}.
80+
* Constructs a new {@link Builder} with the provided {@link OAuth2AuthorizationCodeRequestAuthenticationToken}.
8281
*
83-
* @param authentication the {@link Authentication}
84-
* @param authorizationConsentBuilder the {@link OAuth2AuthorizationConsent.Builder}
82+
* @param authentication the {@link OAuth2AuthorizationCodeRequestAuthenticationToken}
8583
* @return the {@link Builder}
8684
*/
87-
public static Builder with(Authentication authentication, OAuth2AuthorizationConsent.Builder authorizationConsentBuilder) {
88-
return new Builder(authentication, authorizationConsentBuilder);
85+
public static Builder with(OAuth2AuthorizationCodeRequestAuthenticationToken authentication) {
86+
return new Builder(authentication);
8987
}
9088

9189
/**
9290
* A builder for {@link OAuth2AuthorizationConsentAuthenticationContext}.
9391
*/
9492
public static final class Builder extends AbstractBuilder<OAuth2AuthorizationConsentAuthenticationContext, Builder> {
9593

96-
private Builder(Authentication authentication, OAuth2AuthorizationConsent.Builder authorizationConsentBuilder) {
94+
private Builder(OAuth2AuthorizationCodeRequestAuthenticationToken authentication) {
9795
super(authentication);
98-
Assert.notNull(authorizationConsentBuilder, "authorizationConsentBuilder cannot be null");
99-
put(OAuth2AuthorizationConsent.Builder.class, authorizationConsentBuilder);
96+
}
97+
98+
/**
99+
* Sets the {@link OAuth2AuthorizationConsent.Builder authorization consent builder}.
100+
*
101+
* @param authorizationConsent the {@link OAuth2AuthorizationConsent.Builder}
102+
* @return the {@link Builder} for further configuration
103+
*/
104+
public Builder authorizationConsent(OAuth2AuthorizationConsent.Builder authorizationConsent) {
105+
return put(OAuth2AuthorizationConsent.Builder.class, authorizationConsent);
100106
}
101107

102108
/**
@@ -135,6 +141,7 @@ public Builder authorizationRequest(OAuth2AuthorizationRequest authorizationRequ
135141
* @return the {@link OAuth2AuthorizationConsentAuthenticationContext}
136142
*/
137143
public OAuth2AuthorizationConsentAuthenticationContext build() {
144+
Assert.notNull(get(OAuth2AuthorizationConsent.Builder.class), "authorizationConsentBuilder cannot be null");
138145
Assert.notNull(get(RegisteredClient.class), "registeredClient cannot be null");
139146
Assert.notNull(get(OAuth2Authorization.class), "authorization cannot be null");
140147
Assert.notNull(get(OAuth2AuthorizationRequest.class), "authorizationRequest cannot be null");

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,18 @@ public class OAuth2AuthorizationConsentAuthenticationContextTests {
5151

5252
@Test
5353
public void withWhenAuthenticationNullThenThrowIllegalArgumentException() {
54-
assertThatThrownBy(() -> OAuth2AuthorizationConsentAuthenticationContext.with(null, this.authorizationConsentBuilder))
54+
assertThatThrownBy(() -> OAuth2AuthorizationConsentAuthenticationContext.with(null))
5555
.isInstanceOf(IllegalArgumentException.class)
5656
.hasMessage("authentication cannot be null");
5757
}
5858

59-
@Test
60-
public void withWhenAuthorizationConsentBuilderNullThenThrowIllegalArgumentException() {
61-
assertThatThrownBy(() -> OAuth2AuthorizationConsentAuthenticationContext.with(this.authorizationCodeRequestAuthentication, null))
62-
.isInstanceOf(IllegalArgumentException.class)
63-
.hasMessage("authorizationConsentBuilder cannot be null");
64-
}
65-
6659
@Test
6760
public void setWhenValueNullThenThrowIllegalArgumentException() {
6861
OAuth2AuthorizationConsentAuthenticationContext.Builder builder =
69-
OAuth2AuthorizationConsentAuthenticationContext.with(this.authorizationCodeRequestAuthentication, this.authorizationConsentBuilder);
62+
OAuth2AuthorizationConsentAuthenticationContext.with(this.authorizationCodeRequestAuthentication);
7063

64+
assertThatThrownBy(() -> builder.authorizationConsent(null))
65+
.isInstanceOf(IllegalArgumentException.class);
7166
assertThatThrownBy(() -> builder.registeredClient(null))
7267
.isInstanceOf(IllegalArgumentException.class);
7368
assertThatThrownBy(() -> builder.authorization(null))
@@ -81,7 +76,12 @@ public void setWhenValueNullThenThrowIllegalArgumentException() {
8176
@Test
8277
public void buildWhenRequiredValueNullThenThrowIllegalArgumentException() {
8378
OAuth2AuthorizationConsentAuthenticationContext.Builder builder =
84-
OAuth2AuthorizationConsentAuthenticationContext.with(this.authorizationCodeRequestAuthentication, this.authorizationConsentBuilder);
79+
OAuth2AuthorizationConsentAuthenticationContext.with(this.authorizationCodeRequestAuthentication);
80+
81+
assertThatThrownBy(builder::build)
82+
.isInstanceOf(IllegalArgumentException.class)
83+
.hasMessage("authorizationConsentBuilder cannot be null");
84+
builder.authorizationConsent(this.authorizationConsentBuilder);
8585

8686
assertThatThrownBy(builder::build)
8787
.isInstanceOf(IllegalArgumentException.class)
@@ -104,7 +104,8 @@ public void buildWhenRequiredValueNullThenThrowIllegalArgumentException() {
104104
@Test
105105
public void buildWhenAllValuesProvidedThenAllValuesAreSet() {
106106
OAuth2AuthorizationConsentAuthenticationContext context =
107-
OAuth2AuthorizationConsentAuthenticationContext.with(this.authorizationCodeRequestAuthentication, this.authorizationConsentBuilder)
107+
OAuth2AuthorizationConsentAuthenticationContext.with(this.authorizationCodeRequestAuthentication)
108+
.authorizationConsent(this.authorizationConsentBuilder)
108109
.registeredClient(this.registeredClient)
109110
.authorization(this.authorization)
110111
.authorizationRequest(this.authorizationRequest)

0 commit comments

Comments
 (0)