Polish OAuth2AuthorizationConsentAuthenticationContext

Issue gh-470
This commit is contained in:
Joe Grandja
2021-11-30 06:16:01 -05:00
parent 332d1cc318
commit 9b60ed23e1
3 changed files with 30 additions and 21 deletions

View File

@@ -355,7 +355,8 @@ public final class OAuth2AuthorizationCodeRequestAuthenticationProvider implemen
if (this.authorizationConsentCustomizer != null) {
// @formatter:off
OAuth2AuthorizationConsentAuthenticationContext authorizationConsentAuthenticationContext =
OAuth2AuthorizationConsentAuthenticationContext.with(authorizationCodeRequestAuthentication, authorizationConsentBuilder)
OAuth2AuthorizationConsentAuthenticationContext.with(authorizationCodeRequestAuthentication)
.authorizationConsent(authorizationConsentBuilder)
.registeredClient(registeredClient)
.authorization(authorization)
.authorizationRequest(authorizationRequest)

View File

@@ -17,7 +17,6 @@ package org.springframework.security.oauth2.server.authorization.authentication;
import java.util.Map;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.core.authentication.OAuth2AuthenticationContext;
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
@@ -78,14 +77,13 @@ public final class OAuth2AuthorizationConsentAuthenticationContext extends OAuth
}
/**
* Constructs a new {@link Builder} with the provided {@link Authentication} and {@link OAuth2AuthorizationConsent.Builder}.
* Constructs a new {@link Builder} with the provided {@link OAuth2AuthorizationCodeRequestAuthenticationToken}.
*
* @param authentication the {@link Authentication}
* @param authorizationConsentBuilder the {@link OAuth2AuthorizationConsent.Builder}
* @param authentication the {@link OAuth2AuthorizationCodeRequestAuthenticationToken}
* @return the {@link Builder}
*/
public static Builder with(Authentication authentication, OAuth2AuthorizationConsent.Builder authorizationConsentBuilder) {
return new Builder(authentication, authorizationConsentBuilder);
public static Builder with(OAuth2AuthorizationCodeRequestAuthenticationToken authentication) {
return new Builder(authentication);
}
/**
@@ -93,10 +91,18 @@ public final class OAuth2AuthorizationConsentAuthenticationContext extends OAuth
*/
public static final class Builder extends AbstractBuilder<OAuth2AuthorizationConsentAuthenticationContext, Builder> {
private Builder(Authentication authentication, OAuth2AuthorizationConsent.Builder authorizationConsentBuilder) {
private Builder(OAuth2AuthorizationCodeRequestAuthenticationToken authentication) {
super(authentication);
Assert.notNull(authorizationConsentBuilder, "authorizationConsentBuilder cannot be null");
put(OAuth2AuthorizationConsent.Builder.class, authorizationConsentBuilder);
}
/**
* Sets the {@link OAuth2AuthorizationConsent.Builder authorization consent builder}.
*
* @param authorizationConsent the {@link OAuth2AuthorizationConsent.Builder}
* @return the {@link Builder} for further configuration
*/
public Builder authorizationConsent(OAuth2AuthorizationConsent.Builder authorizationConsent) {
return put(OAuth2AuthorizationConsent.Builder.class, authorizationConsent);
}
/**
@@ -135,6 +141,7 @@ public final class OAuth2AuthorizationConsentAuthenticationContext extends OAuth
* @return the {@link OAuth2AuthorizationConsentAuthenticationContext}
*/
public OAuth2AuthorizationConsentAuthenticationContext build() {
Assert.notNull(get(OAuth2AuthorizationConsent.Builder.class), "authorizationConsentBuilder cannot be null");
Assert.notNull(get(RegisteredClient.class), "registeredClient cannot be null");
Assert.notNull(get(OAuth2Authorization.class), "authorization cannot be null");
Assert.notNull(get(OAuth2AuthorizationRequest.class), "authorizationRequest cannot be null");

View File

@@ -51,23 +51,18 @@ public class OAuth2AuthorizationConsentAuthenticationContextTests {
@Test
public void withWhenAuthenticationNullThenThrowIllegalArgumentException() {
assertThatThrownBy(() -> OAuth2AuthorizationConsentAuthenticationContext.with(null, this.authorizationConsentBuilder))
assertThatThrownBy(() -> OAuth2AuthorizationConsentAuthenticationContext.with(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("authentication cannot be null");
}
@Test
public void withWhenAuthorizationConsentBuilderNullThenThrowIllegalArgumentException() {
assertThatThrownBy(() -> OAuth2AuthorizationConsentAuthenticationContext.with(this.authorizationCodeRequestAuthentication, null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("authorizationConsentBuilder cannot be null");
}
@Test
public void setWhenValueNullThenThrowIllegalArgumentException() {
OAuth2AuthorizationConsentAuthenticationContext.Builder builder =
OAuth2AuthorizationConsentAuthenticationContext.with(this.authorizationCodeRequestAuthentication, this.authorizationConsentBuilder);
OAuth2AuthorizationConsentAuthenticationContext.with(this.authorizationCodeRequestAuthentication);
assertThatThrownBy(() -> builder.authorizationConsent(null))
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> builder.registeredClient(null))
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> builder.authorization(null))
@@ -81,7 +76,12 @@ public class OAuth2AuthorizationConsentAuthenticationContextTests {
@Test
public void buildWhenRequiredValueNullThenThrowIllegalArgumentException() {
OAuth2AuthorizationConsentAuthenticationContext.Builder builder =
OAuth2AuthorizationConsentAuthenticationContext.with(this.authorizationCodeRequestAuthentication, this.authorizationConsentBuilder);
OAuth2AuthorizationConsentAuthenticationContext.with(this.authorizationCodeRequestAuthentication);
assertThatThrownBy(builder::build)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("authorizationConsentBuilder cannot be null");
builder.authorizationConsent(this.authorizationConsentBuilder);
assertThatThrownBy(builder::build)
.isInstanceOf(IllegalArgumentException.class)
@@ -104,7 +104,8 @@ public class OAuth2AuthorizationConsentAuthenticationContextTests {
@Test
public void buildWhenAllValuesProvidedThenAllValuesAreSet() {
OAuth2AuthorizationConsentAuthenticationContext context =
OAuth2AuthorizationConsentAuthenticationContext.with(this.authorizationCodeRequestAuthentication, this.authorizationConsentBuilder)
OAuth2AuthorizationConsentAuthenticationContext.with(this.authorizationCodeRequestAuthentication)
.authorizationConsent(this.authorizationConsentBuilder)
.registeredClient(this.registeredClient)
.authorization(this.authorization)
.authorizationRequest(this.authorizationRequest)