Polish oauth2-core format

Issue gh-8945
This commit is contained in:
Rob Winch 2020-08-24 09:49:25 -05:00
parent dc47a7575e
commit a729d24d47
19 changed files with 489 additions and 127 deletions

View File

@ -55,8 +55,15 @@ public final class MapOAuth2AccessTokenResponseConverter
additionalParameters.put(entry.getKey(), entry.getValue()); additionalParameters.put(entry.getKey(), entry.getValue());
} }
} }
return OAuth2AccessTokenResponse.withToken(accessToken).tokenType(accessTokenType).expiresIn(expiresIn) // @formatter:off
.scopes(scopes).refreshToken(refreshToken).additionalParameters(additionalParameters).build(); return OAuth2AccessTokenResponse.withToken(accessToken)
.tokenType(accessTokenType)
.expiresIn(expiresIn)
.scopes(scopes)
.refreshToken(refreshToken)
.additionalParameters(additionalParameters)
.build();
// @formatter:on
} }
private OAuth2AccessToken.TokenType getAccessTokenType(Map<String, String> tokenResponseParameters) { private OAuth2AccessToken.TokenType getAccessTokenType(Map<String, String> tokenResponseParameters) {

View File

@ -215,12 +215,16 @@ public final class OAuth2AuthorizationRequest implements Serializable {
*/ */
public static Builder from(OAuth2AuthorizationRequest authorizationRequest) { public static Builder from(OAuth2AuthorizationRequest authorizationRequest) {
Assert.notNull(authorizationRequest, "authorizationRequest cannot be null"); Assert.notNull(authorizationRequest, "authorizationRequest cannot be null");
// @formatter:off
return new Builder(authorizationRequest.getGrantType()) return new Builder(authorizationRequest.getGrantType())
.authorizationUri(authorizationRequest.getAuthorizationUri()) .authorizationUri(authorizationRequest.getAuthorizationUri())
.clientId(authorizationRequest.getClientId()).redirectUri(authorizationRequest.getRedirectUri()) .clientId(authorizationRequest.getClientId())
.scopes(authorizationRequest.getScopes()).state(authorizationRequest.getState()) .redirectUri(authorizationRequest.getRedirectUri())
.scopes(authorizationRequest.getScopes())
.state(authorizationRequest.getState())
.additionalParameters(authorizationRequest.getAdditionalParameters()) .additionalParameters(authorizationRequest.getAdditionalParameters())
.attributes(authorizationRequest.getAttributes()); .attributes(authorizationRequest.getAttributes());
// @formatter:on
} }
/** /**

View File

@ -77,8 +77,12 @@ public class OAuth2AccessTokenResponseHttpMessageConverter
// Object and then convert values to String // Object and then convert values to String
Map<String, Object> tokenResponseParameters = (Map<String, Object>) this.jsonMessageConverter Map<String, Object> tokenResponseParameters = (Map<String, Object>) this.jsonMessageConverter
.read(STRING_OBJECT_MAP.getType(), null, inputMessage); .read(STRING_OBJECT_MAP.getType(), null, inputMessage);
return this.tokenResponseConverter.convert(tokenResponseParameters.entrySet().stream() // @formatter:off
return this.tokenResponseConverter.convert(tokenResponseParameters
.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, (entry) -> String.valueOf(entry.getValue())))); .collect(Collectors.toMap(Map.Entry::getKey, (entry) -> String.valueOf(entry.getValue()))));
// @formatter:on
} }
catch (Exception ex) { catch (Exception ex) {
throw new HttpMessageNotReadableException( throw new HttpMessageNotReadableException(

View File

@ -123,9 +123,15 @@ class OAuth2AccessTokenResponseBodyExtractor
refreshToken = accessTokenResponse.getTokens().getRefreshToken().getValue(); refreshToken = accessTokenResponse.getTokens().getRefreshToken().getValue();
} }
Map<String, Object> additionalParameters = new LinkedHashMap<>(accessTokenResponse.getCustomParameters()); Map<String, Object> additionalParameters = new LinkedHashMap<>(accessTokenResponse.getCustomParameters());
return OAuth2AccessTokenResponse.withToken(accessToken.getValue()).tokenType(accessTokenType) // @formatter:off
.expiresIn(expiresIn).scopes(scopes).refreshToken(refreshToken) return OAuth2AccessTokenResponse.withToken(accessToken.getValue())
.additionalParameters(additionalParameters).build(); .tokenType(accessTokenType)
.expiresIn(expiresIn)
.scopes(scopes)
.refreshToken(refreshToken)
.additionalParameters(additionalParameters)
.build();
// @formatter:on
} }
} }

View File

@ -49,9 +49,15 @@ public class OAuth2AccessTokenResponseMapConverterTests {
Set<String> scopes = new HashSet<>(); Set<String> scopes = new HashSet<>();
scopes.add("read"); scopes.add("read");
scopes.add("write"); scopes.add("write");
OAuth2AccessTokenResponse build = OAuth2AccessTokenResponse.withToken("access-token-value-1234").expiresIn(3699) // @formatter:off
.additionalParameters(additionalParameters).refreshToken("refresh-token-value-1234").scopes(scopes) OAuth2AccessTokenResponse build = OAuth2AccessTokenResponse.withToken("access-token-value-1234")
.tokenType(OAuth2AccessToken.TokenType.BEARER).build(); .expiresIn(3699)
.additionalParameters(additionalParameters)
.refreshToken("refresh-token-value-1234")
.scopes(scopes)
.tokenType(OAuth2AccessToken.TokenType.BEARER)
.build();
// @formatter:on
Map<String, String> result = this.messageConverter.convert(build); Map<String, String> result = this.messageConverter.convert(build);
Assert.assertEquals(7, result.size()); Assert.assertEquals(7, result.size());
Assert.assertEquals("access-token-value-1234", result.get("access_token")); Assert.assertEquals("access-token-value-1234", result.get("access_token"));
@ -65,8 +71,11 @@ public class OAuth2AccessTokenResponseMapConverterTests {
@Test @Test
public void convertMinimal() { public void convertMinimal() {
// @formatter:off
OAuth2AccessTokenResponse build = OAuth2AccessTokenResponse.withToken("access-token-value-1234") OAuth2AccessTokenResponse build = OAuth2AccessTokenResponse.withToken("access-token-value-1234")
.tokenType(OAuth2AccessToken.TokenType.BEARER).build(); .tokenType(OAuth2AccessToken.TokenType.BEARER)
.build();
// @formatter:on
Map<String, String> result = this.messageConverter.convert(build); Map<String, String> result = this.messageConverter.convert(build);
Assert.assertEquals(3, result.size()); Assert.assertEquals(3, result.size());
Assert.assertEquals("access-token-value-1234", result.get("access_token")); Assert.assertEquals("access-token-value-1234", result.get("access_token"));

View File

@ -45,27 +45,44 @@ public class OAuth2AccessTokenResponseTests {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void buildWhenTokenValueIsNullThenThrowIllegalArgumentException() { public void buildWhenTokenValueIsNullThenThrowIllegalArgumentException() {
OAuth2AccessTokenResponse.withToken(null).tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(EXPIRES_IN) // @formatter:off
OAuth2AccessTokenResponse.withToken(null)
.tokenType(OAuth2AccessToken.TokenType.BEARER)
.expiresIn(EXPIRES_IN)
.build(); .build();
// @formatter:on
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void buildWhenTokenTypeIsNullThenThrowIllegalArgumentException() { public void buildWhenTokenTypeIsNullThenThrowIllegalArgumentException() {
OAuth2AccessTokenResponse.withToken(TOKEN_VALUE).tokenType(null).expiresIn(EXPIRES_IN).build(); // @formatter:off
OAuth2AccessTokenResponse.withToken(TOKEN_VALUE)
.tokenType(null)
.expiresIn(EXPIRES_IN)
.build();
// @formatter:on
} }
@Test @Test
public void buildWhenExpiresInIsZeroThenExpiresAtOneSecondAfterIssueAt() { public void buildWhenExpiresInIsZeroThenExpiresAtOneSecondAfterIssueAt() {
// @formatter:off
OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE) OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE)
.tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(0).build(); .tokenType(OAuth2AccessToken.TokenType.BEARER)
.expiresIn(0)
.build();
// @formatter:on
assertThat(tokenResponse.getAccessToken().getExpiresAt()) assertThat(tokenResponse.getAccessToken().getExpiresAt())
.isEqualTo(tokenResponse.getAccessToken().getIssuedAt().plusSeconds(1)); .isEqualTo(tokenResponse.getAccessToken().getIssuedAt().plusSeconds(1));
} }
@Test @Test
public void buildWhenExpiresInIsNegativeThenExpiresAtOneSecondAfterIssueAt() { public void buildWhenExpiresInIsNegativeThenExpiresAtOneSecondAfterIssueAt() {
// @formatter:off
OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE) OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE)
.tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(-1L).build(); .tokenType(OAuth2AccessToken.TokenType.BEARER)
.expiresIn(-1L)
.build();
// @formatter:on
assertThat(tokenResponse.getAccessToken().getExpiresAt()) assertThat(tokenResponse.getAccessToken().getExpiresAt())
.isEqualTo(tokenResponse.getAccessToken().getIssuedAt().plusSeconds(1)); .isEqualTo(tokenResponse.getAccessToken().getIssuedAt().plusSeconds(1));
} }
@ -77,9 +94,15 @@ public class OAuth2AccessTokenResponseTests {
Map<String, Object> additionalParameters = new HashMap<>(); Map<String, Object> additionalParameters = new HashMap<>();
additionalParameters.put("param1", "value1"); additionalParameters.put("param1", "value1");
additionalParameters.put("param2", "value2"); additionalParameters.put("param2", "value2");
// @formatter:off
OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE) OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE)
.tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(expiresAt.toEpochMilli()).scopes(scopes) .tokenType(OAuth2AccessToken.TokenType.BEARER)
.refreshToken(REFRESH_TOKEN_VALUE).additionalParameters(additionalParameters).build(); .expiresIn(expiresAt.toEpochMilli())
.scopes(scopes)
.refreshToken(REFRESH_TOKEN_VALUE)
.additionalParameters(additionalParameters)
.build();
// @formatter:on
assertThat(tokenResponse.getAccessToken()).isNotNull(); assertThat(tokenResponse.getAccessToken()).isNotNull();
assertThat(tokenResponse.getAccessToken().getTokenValue()).isEqualTo(TOKEN_VALUE); assertThat(tokenResponse.getAccessToken().getTokenValue()).isEqualTo(TOKEN_VALUE);
assertThat(tokenResponse.getAccessToken().getTokenType()).isEqualTo(OAuth2AccessToken.TokenType.BEARER); assertThat(tokenResponse.getAccessToken().getTokenType()).isEqualTo(OAuth2AccessToken.TokenType.BEARER);
@ -97,9 +120,15 @@ public class OAuth2AccessTokenResponseTests {
Map<String, Object> additionalParameters = new HashMap<>(); Map<String, Object> additionalParameters = new HashMap<>();
additionalParameters.put("param1", "value1"); additionalParameters.put("param1", "value1");
additionalParameters.put("param2", "value2"); additionalParameters.put("param2", "value2");
// @formatter:off
OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE) OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE)
.tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(expiresAt.toEpochMilli()).scopes(scopes) .tokenType(OAuth2AccessToken.TokenType.BEARER)
.refreshToken(REFRESH_TOKEN_VALUE).additionalParameters(additionalParameters).build(); .expiresIn(expiresAt.toEpochMilli())
.scopes(scopes)
.refreshToken(REFRESH_TOKEN_VALUE)
.additionalParameters(additionalParameters)
.build();
// @formatter:on
OAuth2AccessTokenResponse withResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse).build(); OAuth2AccessTokenResponse withResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse).build();
assertThat(withResponse.getAccessToken().getTokenValue()) assertThat(withResponse.getAccessToken().getTokenValue())
.isEqualTo(tokenResponse.getAccessToken().getTokenValue()); .isEqualTo(tokenResponse.getAccessToken().getTokenValue());
@ -120,17 +149,25 @@ public class OAuth2AccessTokenResponseTests {
Map<String, Object> additionalParameters = new HashMap<>(); Map<String, Object> additionalParameters = new HashMap<>();
additionalParameters.put("param1", "value1"); additionalParameters.put("param1", "value1");
additionalParameters.put("param2", "value2"); additionalParameters.put("param2", "value2");
// @formatter:off
OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE) OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE)
.tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(expiresAt.toEpochMilli()).scopes(scopes) .tokenType(OAuth2AccessToken.TokenType.BEARER)
.additionalParameters(additionalParameters).build(); .expiresIn(expiresAt.toEpochMilli())
.scopes(scopes)
.additionalParameters(additionalParameters)
.build();
// @formatter:on
OAuth2AccessTokenResponse withResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse).build(); OAuth2AccessTokenResponse withResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse).build();
assertThat(withResponse.getRefreshToken()).isNull(); assertThat(withResponse.getRefreshToken()).isNull();
} }
@Test @Test
public void buildWhenResponseAndExpiresInThenExpiresAtEqualToIssuedAtPlusExpiresIn() { public void buildWhenResponseAndExpiresInThenExpiresAtEqualToIssuedAtPlusExpiresIn() {
// @formatter:off
OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE) OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE)
.tokenType(OAuth2AccessToken.TokenType.BEARER).build(); .tokenType(OAuth2AccessToken.TokenType.BEARER)
.build();
// @formatter:on
long expiresIn = 30; long expiresIn = 30;
OAuth2AccessTokenResponse withResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse) OAuth2AccessTokenResponse withResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse)
.expiresIn(expiresIn).build(); .expiresIn(expiresIn).build();

View File

@ -50,63 +50,128 @@ public class OAuth2AuthorizationRequestTests {
@Test @Test
public void buildWhenAuthorizationUriIsNullThenThrowIllegalArgumentException() { public void buildWhenAuthorizationUriIsNullThenThrowIllegalArgumentException() {
// @formatter:off
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
.isThrownBy(() -> OAuth2AuthorizationRequest.authorizationCode().authorizationUri(null) .isThrownBy(() -> OAuth2AuthorizationRequest
.clientId(CLIENT_ID).redirectUri(REDIRECT_URI).scopes(SCOPES).state(STATE).build()); .authorizationCode()
.authorizationUri(null)
.clientId(CLIENT_ID)
.redirectUri(REDIRECT_URI)
.scopes(SCOPES)
.state(STATE)
.build()
);
// @formatter:on
} }
@Test @Test
public void buildWhenClientIdIsNullThenThrowIllegalArgumentException() { public void buildWhenClientIdIsNullThenThrowIllegalArgumentException() {
// @formatter:off
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
.isThrownBy(() -> OAuth2AuthorizationRequest.authorizationCode().authorizationUri(AUTHORIZATION_URI) .isThrownBy(() -> OAuth2AuthorizationRequest.authorizationCode()
.clientId(null).redirectUri(REDIRECT_URI).scopes(SCOPES).state(STATE).build()); .authorizationUri(AUTHORIZATION_URI)
.clientId(null)
.redirectUri(REDIRECT_URI)
.scopes(SCOPES)
.state(STATE)
.build()
);
// @formatter:on
} }
@Test @Test
public void buildWhenRedirectUriIsNullForImplicitThenThrowIllegalArgumentException() { public void buildWhenRedirectUriIsNullForImplicitThenThrowIllegalArgumentException() {
// @formatter:off
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
.isThrownBy(() -> OAuth2AuthorizationRequest.implicit().authorizationUri(AUTHORIZATION_URI) .isThrownBy(() -> OAuth2AuthorizationRequest.implicit()
.clientId(CLIENT_ID).redirectUri(null).scopes(SCOPES).state(STATE).build()); .authorizationUri(AUTHORIZATION_URI)
.clientId(CLIENT_ID)
.redirectUri(null)
.scopes(SCOPES)
.state(STATE).build()
);
// @formatter:on
} }
@Test @Test
public void buildWhenRedirectUriIsNullForAuthorizationCodeThenDoesNotThrowAnyException() { public void buildWhenRedirectUriIsNullForAuthorizationCodeThenDoesNotThrowAnyException() {
OAuth2AuthorizationRequest.authorizationCode().authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID) // @formatter:off
.redirectUri(null).scopes(SCOPES).state(STATE).build(); OAuth2AuthorizationRequest.authorizationCode()
.authorizationUri(AUTHORIZATION_URI)
.clientId(CLIENT_ID)
.redirectUri(null)
.scopes(SCOPES)
.state(STATE)
.build();
// @formatter:on
} }
@Test @Test
public void buildWhenScopesIsNullThenDoesNotThrowAnyException() { public void buildWhenScopesIsNullThenDoesNotThrowAnyException() {
OAuth2AuthorizationRequest.authorizationCode().authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID) // @formatter:off
.redirectUri(REDIRECT_URI).scopes(null).state(STATE).build(); OAuth2AuthorizationRequest.authorizationCode()
.authorizationUri(AUTHORIZATION_URI)
.clientId(CLIENT_ID)
.redirectUri(REDIRECT_URI)
.scopes(null)
.state(STATE)
.build();
// @formatter:on
} }
@Test @Test
public void buildWhenStateIsNullThenDoesNotThrowAnyException() { public void buildWhenStateIsNullThenDoesNotThrowAnyException() {
OAuth2AuthorizationRequest.authorizationCode().authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID) // @formatter:off
.redirectUri(REDIRECT_URI).scopes(SCOPES).state(null).build(); OAuth2AuthorizationRequest.authorizationCode()
.authorizationUri(AUTHORIZATION_URI)
.clientId(CLIENT_ID)
.redirectUri(REDIRECT_URI)
.scopes(SCOPES)
.state(null)
.build();
// @formatter:on
} }
@Test @Test
public void buildWhenAdditionalParametersEmptyThenDoesNotThrowAnyException() { public void buildWhenAdditionalParametersEmptyThenDoesNotThrowAnyException() {
OAuth2AuthorizationRequest.authorizationCode().authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID) // @formatter:off
.redirectUri(REDIRECT_URI).scopes(SCOPES).state(STATE).additionalParameters(Map::clear).build(); OAuth2AuthorizationRequest.authorizationCode()
.authorizationUri(AUTHORIZATION_URI)
.clientId(CLIENT_ID)
.redirectUri(REDIRECT_URI)
.scopes(SCOPES)
.state(STATE)
.additionalParameters(Map::clear)
.build();
// @formatter:on
} }
@Test @Test
public void buildWhenImplicitThenGrantTypeResponseTypeIsSet() { public void buildWhenImplicitThenGrantTypeResponseTypeIsSet() {
// @formatter:off
OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.implicit() OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.implicit()
.authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID).redirectUri(REDIRECT_URI).scopes(SCOPES) .authorizationUri(AUTHORIZATION_URI)
.state(STATE).build(); .clientId(CLIENT_ID)
.redirectUri(REDIRECT_URI)
.scopes(SCOPES)
.state(STATE)
.build();
// @formatter:on
assertThat(authorizationRequest.getGrantType()).isEqualTo(AuthorizationGrantType.IMPLICIT); assertThat(authorizationRequest.getGrantType()).isEqualTo(AuthorizationGrantType.IMPLICIT);
assertThat(authorizationRequest.getResponseType()).isEqualTo(OAuth2AuthorizationResponseType.TOKEN); assertThat(authorizationRequest.getResponseType()).isEqualTo(OAuth2AuthorizationResponseType.TOKEN);
} }
@Test @Test
public void buildWhenAuthorizationCodeThenGrantTypeResponseTypeIsSet() { public void buildWhenAuthorizationCodeThenGrantTypeResponseTypeIsSet() {
// @formatter:off
OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode() OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode()
.authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID).redirectUri(null).scopes(SCOPES).state(STATE) .authorizationUri(AUTHORIZATION_URI)
.clientId(CLIENT_ID)
.redirectUri(null)
.scopes(SCOPES)
.state(STATE)
.build(); .build();
// @formatter:on
assertThat(authorizationRequest.getGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE); assertThat(authorizationRequest.getGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
assertThat(authorizationRequest.getResponseType()).isEqualTo(OAuth2AuthorizationResponseType.CODE); assertThat(authorizationRequest.getResponseType()).isEqualTo(OAuth2AuthorizationResponseType.CODE);
} }
@ -119,10 +184,18 @@ public class OAuth2AuthorizationRequestTests {
Map<String, Object> attributes = new HashMap<>(); Map<String, Object> attributes = new HashMap<>();
attributes.put("attribute1", "value1"); attributes.put("attribute1", "value1");
attributes.put("attribute2", "value2"); attributes.put("attribute2", "value2");
// @formatter:off
OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode() OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode()
.authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID).redirectUri(REDIRECT_URI).scopes(SCOPES) .authorizationUri(AUTHORIZATION_URI)
.state(STATE).additionalParameters(additionalParameters).attributes(attributes) .clientId(CLIENT_ID)
.authorizationRequestUri(AUTHORIZATION_URI).build(); .redirectUri(REDIRECT_URI)
.scopes(SCOPES)
.state(STATE)
.additionalParameters(additionalParameters)
.attributes(attributes)
.authorizationRequestUri(AUTHORIZATION_URI)
.build();
// @formatter:on
assertThat(authorizationRequest.getAuthorizationUri()).isEqualTo(AUTHORIZATION_URI); assertThat(authorizationRequest.getAuthorizationUri()).isEqualTo(AUTHORIZATION_URI);
assertThat(authorizationRequest.getGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE); assertThat(authorizationRequest.getGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
assertThat(authorizationRequest.getResponseType()).isEqualTo(OAuth2AuthorizationResponseType.CODE); assertThat(authorizationRequest.getResponseType()).isEqualTo(OAuth2AuthorizationResponseType.CODE);
@ -137,9 +210,15 @@ public class OAuth2AuthorizationRequestTests {
@Test @Test
public void buildWhenScopesMultiThenSeparatedByEncodedSpace() { public void buildWhenScopesMultiThenSeparatedByEncodedSpace() {
// @formatter:off
OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.implicit() OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.implicit()
.authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID).redirectUri(REDIRECT_URI).scopes(SCOPES) .authorizationUri(AUTHORIZATION_URI)
.state(STATE).build(); .clientId(CLIENT_ID)
.redirectUri(REDIRECT_URI)
.scopes(SCOPES)
.state(STATE)
.build();
// @formatter:on
assertThat(authorizationRequest.getAuthorizationRequestUri()) assertThat(authorizationRequest.getAuthorizationRequestUri())
.isEqualTo("https://provider.com/oauth2/authorize?" + "response_type=token&client_id=client-id&" .isEqualTo("https://provider.com/oauth2/authorize?" + "response_type=token&client_id=client-id&"
+ "scope=scope1%20scope2&state=state&" + "redirect_uri=https://example.com"); + "scope=scope1%20scope2&state=state&" + "redirect_uri=https://example.com");
@ -147,17 +226,31 @@ public class OAuth2AuthorizationRequestTests {
@Test @Test
public void buildWhenAuthorizationRequestUriSetThenOverridesDefault() { public void buildWhenAuthorizationRequestUriSetThenOverridesDefault() {
// @formatter:off
OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode() OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode()
.authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID).redirectUri(REDIRECT_URI).scopes(SCOPES) .authorizationUri(AUTHORIZATION_URI)
.state(STATE).authorizationRequestUri(AUTHORIZATION_URI).build(); .clientId(CLIENT_ID)
.redirectUri(REDIRECT_URI)
.scopes(SCOPES)
.state(STATE)
.authorizationRequestUri(AUTHORIZATION_URI)
.build();
// @formatter:on
assertThat(authorizationRequest.getAuthorizationRequestUri()).isEqualTo(AUTHORIZATION_URI); assertThat(authorizationRequest.getAuthorizationRequestUri()).isEqualTo(AUTHORIZATION_URI);
} }
@Test @Test
public void buildWhenAuthorizationRequestUriFunctionSetThenOverridesDefault() { public void buildWhenAuthorizationRequestUriFunctionSetThenOverridesDefault() {
// @formatter:off
OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode() OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode()
.authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID).redirectUri(REDIRECT_URI).scopes(SCOPES) .authorizationUri(AUTHORIZATION_URI)
.state(STATE).authorizationRequestUri((uriBuilder) -> URI.create(AUTHORIZATION_URI)).build(); .clientId(CLIENT_ID)
.redirectUri(REDIRECT_URI)
.scopes(SCOPES)
.state(STATE)
.authorizationRequestUri((uriBuilder) -> URI.create(AUTHORIZATION_URI))
.build();
// @formatter:on
assertThat(authorizationRequest.getAuthorizationRequestUri()).isEqualTo(AUTHORIZATION_URI); assertThat(authorizationRequest.getAuthorizationRequestUri()).isEqualTo(AUTHORIZATION_URI);
} }
@ -196,11 +289,19 @@ public class OAuth2AuthorizationRequestTests {
Map<String, Object> attributes = new HashMap<>(); Map<String, Object> attributes = new HashMap<>();
attributes.put("attribute1", "value1"); attributes.put("attribute1", "value1");
attributes.put("attribute2", "value2"); attributes.put("attribute2", "value2");
// @formatter:off
OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode() OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode()
.authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID).redirectUri(REDIRECT_URI).scopes(SCOPES) .authorizationUri(AUTHORIZATION_URI)
.state(STATE).additionalParameters(additionalParameters).attributes(attributes).build(); .clientId(CLIENT_ID)
.redirectUri(REDIRECT_URI)
.scopes(SCOPES)
.state(STATE)
.additionalParameters(additionalParameters)
.attributes(attributes)
.build();
OAuth2AuthorizationRequest authorizationRequestCopy = OAuth2AuthorizationRequest.from(authorizationRequest) OAuth2AuthorizationRequest authorizationRequestCopy = OAuth2AuthorizationRequest.from(authorizationRequest)
.build(); .build();
// @formatter:on
assertThat(authorizationRequestCopy.getAuthorizationUri()) assertThat(authorizationRequestCopy.getAuthorizationUri())
.isEqualTo(authorizationRequest.getAuthorizationUri()); .isEqualTo(authorizationRequest.getAuthorizationUri());
assertThat(authorizationRequestCopy.getGrantType()).isEqualTo(authorizationRequest.getGrantType()); assertThat(authorizationRequestCopy.getGrantType()).isEqualTo(authorizationRequest.getGrantType());

View File

@ -41,63 +41,122 @@ public class OAuth2AuthorizationResponseTests {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void buildSuccessResponseWhenAuthCodeIsNullThenThrowIllegalArgumentException() { public void buildSuccessResponseWhenAuthCodeIsNullThenThrowIllegalArgumentException() {
OAuth2AuthorizationResponse.success(null).redirectUri(REDIRECT_URI).state(STATE).build(); // @formatter:off
OAuth2AuthorizationResponse.success(null)
.redirectUri(REDIRECT_URI)
.state(STATE)
.build();
// @formatter:on
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void buildSuccessResponseWhenRedirectUriIsNullThenThrowIllegalArgumentException() { public void buildSuccessResponseWhenRedirectUriIsNullThenThrowIllegalArgumentException() {
OAuth2AuthorizationResponse.success(AUTH_CODE).redirectUri(null).state(STATE).build(); // @formatter:off
OAuth2AuthorizationResponse.success(AUTH_CODE)
.redirectUri(null)
.state(STATE)
.build();
// @formatter:on
} }
@Test @Test
public void buildSuccessResponseWhenStateIsNullThenDoesNotThrowAnyException() { public void buildSuccessResponseWhenStateIsNullThenDoesNotThrowAnyException() {
OAuth2AuthorizationResponse.success(AUTH_CODE).redirectUri(REDIRECT_URI).state(null).build(); // @formatter:off
OAuth2AuthorizationResponse.success(AUTH_CODE)
.redirectUri(REDIRECT_URI)
.state(null)
.build();
// @formatter:on
} }
@Test @Test
public void buildSuccessResponseWhenAllAttributesProvidedThenAllAttributesAreSet() { public void buildSuccessResponseWhenAllAttributesProvidedThenAllAttributesAreSet() {
// @formatter:off
OAuth2AuthorizationResponse authorizationResponse = OAuth2AuthorizationResponse.success(AUTH_CODE) OAuth2AuthorizationResponse authorizationResponse = OAuth2AuthorizationResponse.success(AUTH_CODE)
.redirectUri(REDIRECT_URI).state(STATE).build(); .redirectUri(REDIRECT_URI)
assertThat(authorizationResponse.getCode()).isEqualTo(AUTH_CODE); .state(STATE)
assertThat(authorizationResponse.getRedirectUri()).isEqualTo(REDIRECT_URI); .build();
assertThat(authorizationResponse.getState()).isEqualTo(STATE); assertThat(authorizationResponse.getCode())
.isEqualTo(AUTH_CODE);
assertThat(authorizationResponse.getRedirectUri())
.isEqualTo(REDIRECT_URI);
assertThat(authorizationResponse.getState())
.isEqualTo(STATE);
// @formatter:on
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void buildSuccessResponseWhenErrorCodeIsSetThenThrowIllegalArgumentException() { public void buildSuccessResponseWhenErrorCodeIsSetThenThrowIllegalArgumentException() {
OAuth2AuthorizationResponse.success(AUTH_CODE).redirectUri(REDIRECT_URI).state(STATE).errorCode(ERROR_CODE) // @formatter:off
OAuth2AuthorizationResponse.success(AUTH_CODE)
.redirectUri(REDIRECT_URI)
.state(STATE)
.errorCode(ERROR_CODE)
.build(); .build();
// @formatter:on
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void buildErrorResponseWhenErrorCodeIsNullThenThrowIllegalArgumentException() { public void buildErrorResponseWhenErrorCodeIsNullThenThrowIllegalArgumentException() {
OAuth2AuthorizationResponse.error(null).redirectUri(REDIRECT_URI).state(STATE).build(); // @formatter:off
OAuth2AuthorizationResponse.error(null)
.redirectUri(REDIRECT_URI)
.state(STATE)
.build();
// @formatter:on
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void buildErrorResponseWhenRedirectUriIsNullThenThrowIllegalArgumentException() { public void buildErrorResponseWhenRedirectUriIsNullThenThrowIllegalArgumentException() {
OAuth2AuthorizationResponse.error(ERROR_CODE).redirectUri(null).state(STATE).build(); // @formatter:off
OAuth2AuthorizationResponse.error(ERROR_CODE)
.redirectUri(null)
.state(STATE)
.build();
// @formatter:on
} }
@Test @Test
public void buildErrorResponseWhenStateIsNullThenDoesNotThrowAnyException() { public void buildErrorResponseWhenStateIsNullThenDoesNotThrowAnyException() {
OAuth2AuthorizationResponse.error(ERROR_CODE).redirectUri(REDIRECT_URI).state(null).build(); // @formatter:off
OAuth2AuthorizationResponse.error(ERROR_CODE)
.redirectUri(REDIRECT_URI)
.state(null)
.build();
// @formatter:on
} }
@Test @Test
public void buildErrorResponseWhenAllAttributesProvidedThenAllAttributesAreSet() { public void buildErrorResponseWhenAllAttributesProvidedThenAllAttributesAreSet() {
// @formatter:off
OAuth2AuthorizationResponse authorizationResponse = OAuth2AuthorizationResponse.error(ERROR_CODE) OAuth2AuthorizationResponse authorizationResponse = OAuth2AuthorizationResponse.error(ERROR_CODE)
.errorDescription(ERROR_DESCRIPTION).errorUri(ERROR_URI).redirectUri(REDIRECT_URI).state(STATE).build(); .errorDescription(ERROR_DESCRIPTION)
assertThat(authorizationResponse.getError().getErrorCode()).isEqualTo(ERROR_CODE); .errorUri(ERROR_URI)
assertThat(authorizationResponse.getError().getDescription()).isEqualTo(ERROR_DESCRIPTION); .redirectUri(REDIRECT_URI)
assertThat(authorizationResponse.getError().getUri()).isEqualTo(ERROR_URI); .state(STATE)
assertThat(authorizationResponse.getRedirectUri()).isEqualTo(REDIRECT_URI); .build();
assertThat(authorizationResponse.getState()).isEqualTo(STATE); assertThat(authorizationResponse.getError().getErrorCode())
.isEqualTo(ERROR_CODE);
assertThat(authorizationResponse.getError().getDescription())
.isEqualTo(ERROR_DESCRIPTION);
assertThat(authorizationResponse.getError().getUri())
.isEqualTo(ERROR_URI);
assertThat(authorizationResponse.getRedirectUri())
.isEqualTo(REDIRECT_URI);
assertThat(authorizationResponse.getState())
.isEqualTo(STATE);
// @formatter:on
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void buildErrorResponseWhenAuthCodeIsSetThenThrowIllegalArgumentException() { public void buildErrorResponseWhenAuthCodeIsSetThenThrowIllegalArgumentException() {
OAuth2AuthorizationResponse.error(ERROR_CODE).redirectUri(REDIRECT_URI).state(STATE).code(AUTH_CODE).build(); // @formatter:off
OAuth2AuthorizationResponse.error(ERROR_CODE)
.redirectUri(REDIRECT_URI)
.state(STATE)
.code(AUTH_CODE)
.build();
// @formatter:on
} }
} }

View File

@ -32,7 +32,11 @@ public final class TestOAuth2AccessTokenResponses {
} }
public static OAuth2AccessTokenResponse.Builder accessTokenResponse() { public static OAuth2AccessTokenResponse.Builder accessTokenResponse() {
return OAuth2AccessTokenResponse.withToken("token").tokenType(OAuth2AccessToken.TokenType.BEARER); // @formatter:off
return OAuth2AccessTokenResponse
.withToken("token")
.tokenType(OAuth2AccessToken.TokenType.BEARER);
// @formatter:on
} }
public static OAuth2AccessTokenResponse.Builder oidcAccessTokenResponse() { public static OAuth2AccessTokenResponse.Builder oidcAccessTokenResponse() {

View File

@ -33,10 +33,14 @@ public final class TestOAuth2AuthorizationRequests {
String clientId = "client-id"; String clientId = "client-id";
Map<String, Object> attributes = new HashMap<>(); Map<String, Object> attributes = new HashMap<>();
attributes.put(OAuth2ParameterNames.REGISTRATION_ID, registrationId); attributes.put(OAuth2ParameterNames.REGISTRATION_ID, registrationId);
// @formatter:off
return OAuth2AuthorizationRequest.authorizationCode() return OAuth2AuthorizationRequest.authorizationCode()
.authorizationUri("https://example.com/login/oauth/authorize").clientId(clientId) .authorizationUri("https://example.com/login/oauth/authorize")
.redirectUri("https://example.com/authorize/oauth2/code/registration-id").state("state") .clientId(clientId)
.redirectUri("https://example.com/authorize/oauth2/code/registration-id")
.state("state")
.attributes(attributes); .attributes(attributes);
// @formatter:on
} }
public static OAuth2AuthorizationRequest.Builder oidcRequest() { public static OAuth2AuthorizationRequest.Builder oidcRequest() {

View File

@ -26,14 +26,19 @@ public final class TestOAuth2AuthorizationResponses {
} }
public static OAuth2AuthorizationResponse.Builder success() { public static OAuth2AuthorizationResponse.Builder success() {
return OAuth2AuthorizationResponse.success("authorization-code").state("state") // @formatter:off
return OAuth2AuthorizationResponse.success("authorization-code")
.state("state")
.redirectUri("https://example.com/authorize/oauth2/code/registration-id"); .redirectUri("https://example.com/authorize/oauth2/code/registration-id");
// @formatter:on
} }
public static OAuth2AuthorizationResponse.Builder error() { public static OAuth2AuthorizationResponse.Builder error() {
// @formatter:off
return OAuth2AuthorizationResponse.error("error") return OAuth2AuthorizationResponse.error("error")
.redirectUri("https://example.com/authorize/oauth2/code/registration-id") .redirectUri("https://example.com/authorize/oauth2/code/registration-id")
.errorUri("https://example.com/error"); .errorUri("https://example.com/error");
// @formatter:on
} }
} }

View File

@ -64,7 +64,8 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests {
@Test @Test
public void setTokenResponseConverterWhenConverterIsNullThenThrowIllegalArgumentException() { public void setTokenResponseConverterWhenConverterIsNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.messageConverter.setTokenResponseConverter(null)); assertThatIllegalArgumentException()
.isThrownBy(() -> this.messageConverter.setTokenResponseConverter(null));
} }
@Test @Test
@ -75,11 +76,17 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests {
@Test @Test
public void readInternalWhenSuccessfulTokenResponseThenReadOAuth2AccessTokenResponse() throws Exception { public void readInternalWhenSuccessfulTokenResponseThenReadOAuth2AccessTokenResponse() throws Exception {
String tokenResponse = "{\n" + " \"access_token\": \"access-token-1234\",\n" // @formatter:off
+ " \"token_type\": \"bearer\",\n" + " \"expires_in\": \"3600\",\n" String tokenResponse = "{\n"
+ " \"scope\": \"read write\",\n" + " \"refresh_token\": \"refresh-token-1234\",\n" + " \"access_token\": \"access-token-1234\",\n"
+ " \"custom_parameter_1\": \"custom-value-1\",\n" + " \"custom_parameter_2\": \"custom-value-2\"\n" + " \"token_type\": \"bearer\",\n"
+ " \"expires_in\": \"3600\",\n"
+ " \"scope\": \"read write\",\n"
+ " \"refresh_token\": \"refresh-token-1234\",\n"
+ " \"custom_parameter_1\": \"custom-value-1\",\n"
+ " \"custom_parameter_2\": \"custom-value-2\"\n"
+ "}\n"; + "}\n";
// @formatter:on
MockClientHttpResponse response = new MockClientHttpResponse(tokenResponse.getBytes(), HttpStatus.OK); MockClientHttpResponse response = new MockClientHttpResponse(tokenResponse.getBytes(), HttpStatus.OK);
OAuth2AccessTokenResponse accessTokenResponse = this.messageConverter OAuth2AccessTokenResponse accessTokenResponse = this.messageConverter
.readInternal(OAuth2AccessTokenResponse.class, response); .readInternal(OAuth2AccessTokenResponse.class, response);
@ -96,13 +103,19 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests {
// gh-6463 // gh-6463
@Test @Test
public void readInternalWhenSuccessfulTokenResponseWithObjectThenReadOAuth2AccessTokenResponse() { public void readInternalWhenSuccessfulTokenResponseWithObjectThenReadOAuth2AccessTokenResponse() {
String tokenResponse = "{\n" + " \"access_token\": \"access-token-1234\",\n" // @formatter:off
+ " \"token_type\": \"bearer\",\n" + " \"expires_in\": 3600,\n" + " \"scope\": \"read write\",\n" String tokenResponse = "{\n"
+ " \"access_token\": \"access-token-1234\",\n"
+ " \"token_type\": \"bearer\",\n"
+ " \"expires_in\": 3600,\n"
+ " \"scope\": \"read write\",\n"
+ " \"refresh_token\": \"refresh-token-1234\",\n" + " \"refresh_token\": \"refresh-token-1234\",\n"
+ " \"custom_object_1\": {\"name1\": \"value1\"},\n" + " \"custom_object_1\": {\"name1\": \"value1\"},\n"
+ " \"custom_object_2\": [\"value1\", \"value2\"],\n" + " \"custom_object_2\": [\"value1\", \"value2\"],\n"
+ " \"custom_parameter_1\": \"custom-value-1\",\n" + " \"custom_parameter_2\": \"custom-value-2\"\n" + " \"custom_parameter_1\": \"custom-value-1\",\n"
+ " \"custom_parameter_2\": \"custom-value-2\"\n"
+ "}\n"; + "}\n";
// @formatter:on
MockClientHttpResponse response = new MockClientHttpResponse(tokenResponse.getBytes(), HttpStatus.OK); MockClientHttpResponse response = new MockClientHttpResponse(tokenResponse.getBytes(), HttpStatus.OK);
OAuth2AccessTokenResponse accessTokenResponse = this.messageConverter OAuth2AccessTokenResponse accessTokenResponse = this.messageConverter
.readInternal(OAuth2AccessTokenResponse.class, response); .readInternal(OAuth2AccessTokenResponse.class, response);
@ -120,9 +133,15 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests {
// gh-8108 // gh-8108
@Test @Test
public void readInternalWhenSuccessfulTokenResponseWithNullValueThenReadOAuth2AccessTokenResponse() { public void readInternalWhenSuccessfulTokenResponseWithNullValueThenReadOAuth2AccessTokenResponse() {
String tokenResponse = "{\n" + " \"access_token\": \"access-token-1234\",\n" // @formatter:off
+ " \"token_type\": \"bearer\",\n" + " \"expires_in\": 3600,\n" + " \"scope\": null,\n" String tokenResponse = "{\n"
+ " \"refresh_token\": \"refresh-token-1234\"\n" + "}\n"; + " \"access_token\": \"access-token-1234\",\n"
+ " \"token_type\": \"bearer\",\n"
+ " \"expires_in\": 3600,\n"
+ " \"scope\": null,\n"
+ " \"refresh_token\": \"refresh-token-1234\"\n"
+ "}\n";
// @formatter:on
MockClientHttpResponse response = new MockClientHttpResponse(tokenResponse.getBytes(), HttpStatus.OK); MockClientHttpResponse response = new MockClientHttpResponse(tokenResponse.getBytes(), HttpStatus.OK);
OAuth2AccessTokenResponse accessTokenResponse = this.messageConverter OAuth2AccessTokenResponse accessTokenResponse = this.messageConverter
.readInternal(OAuth2AccessTokenResponse.class, response); .readInternal(OAuth2AccessTokenResponse.class, response);
@ -153,9 +172,15 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests {
Map<String, Object> additionalParameters = new HashMap<>(); Map<String, Object> additionalParameters = new HashMap<>();
additionalParameters.put("custom_parameter_1", "custom-value-1"); additionalParameters.put("custom_parameter_1", "custom-value-1");
additionalParameters.put("custom_parameter_2", "custom-value-2"); additionalParameters.put("custom_parameter_2", "custom-value-2");
// @formatter:off
OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234") OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234")
.tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(expiresAt.toEpochMilli()).scopes(scopes) .tokenType(OAuth2AccessToken.TokenType.BEARER)
.refreshToken("refresh-token-1234").additionalParameters(additionalParameters).build(); .expiresIn(expiresAt.toEpochMilli())
.scopes(scopes)
.refreshToken("refresh-token-1234")
.additionalParameters(additionalParameters)
.build();
// @formatter:on
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.messageConverter.writeInternal(accessTokenResponse, outputMessage); this.messageConverter.writeInternal(accessTokenResponse, outputMessage);
String tokenResponse = outputMessage.getBodyAsString(); String tokenResponse = outputMessage.getBodyAsString();
@ -173,9 +198,14 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests {
Converter tokenResponseParametersConverter = mock(Converter.class); Converter tokenResponseParametersConverter = mock(Converter.class);
given(tokenResponseParametersConverter.convert(any())).willThrow(RuntimeException.class); given(tokenResponseParametersConverter.convert(any())).willThrow(RuntimeException.class);
this.messageConverter.setTokenResponseParametersConverter(tokenResponseParametersConverter); this.messageConverter.setTokenResponseParametersConverter(tokenResponseParametersConverter);
OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234") // @formatter:off
.tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(Instant.now().plusSeconds(3600).toEpochMilli()) OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse
.withToken("access-token-1234")
.tokenType(OAuth2AccessToken.TokenType.BEARER)
.expiresIn(Instant.now().plusSeconds(3600)
.toEpochMilli())
.build(); .build();
// @formatter:on
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
assertThatExceptionOfType(HttpMessageNotWritableException.class) assertThatExceptionOfType(HttpMessageNotWritableException.class)
.isThrownBy(() -> this.messageConverter.writeInternal(accessTokenResponse, outputMessage)) .isThrownBy(() -> this.messageConverter.writeInternal(accessTokenResponse, outputMessage))

View File

@ -65,9 +65,13 @@ public class OAuth2ErrorHttpMessageConverterTests {
@Test @Test
public void readInternalWhenErrorResponseThenReadOAuth2Error() throws Exception { public void readInternalWhenErrorResponseThenReadOAuth2Error() throws Exception {
String errorResponse = "{\n" + " \"error\": \"unauthorized_client\",\n" // @formatter:off
String errorResponse = "{\n"
+ " \"error\": \"unauthorized_client\",\n"
+ " \"error_description\": \"The client is not authorized\",\n" + " \"error_description\": \"The client is not authorized\",\n"
+ " \"error_uri\": \"https://tools.ietf.org/html/rfc6749#section-5.2\"\n" + "}\n"; + " \"error_uri\": \"https://tools.ietf.org/html/rfc6749#section-5.2\"\n"
+ "}\n";
// @formatter:on
MockClientHttpResponse response = new MockClientHttpResponse(errorResponse.getBytes(), HttpStatus.BAD_REQUEST); MockClientHttpResponse response = new MockClientHttpResponse(errorResponse.getBytes(), HttpStatus.BAD_REQUEST);
OAuth2Error oauth2Error = this.messageConverter.readInternal(OAuth2Error.class, response); OAuth2Error oauth2Error = this.messageConverter.readInternal(OAuth2Error.class, response);
assertThat(oauth2Error.getErrorCode()).isEqualTo("unauthorized_client"); assertThat(oauth2Error.getErrorCode()).isEqualTo("unauthorized_client");
@ -78,9 +82,14 @@ public class OAuth2ErrorHttpMessageConverterTests {
// gh-8157 // gh-8157
@Test @Test
public void readInternalWhenErrorResponseWithObjectThenReadOAuth2Error() throws Exception { public void readInternalWhenErrorResponseWithObjectThenReadOAuth2Error() throws Exception {
String errorResponse = "{\n" + " \"error\": \"unauthorized_client\",\n" // @formatter:off
+ " \"error_description\": \"The client is not authorized\",\n" + " \"error_codes\": [65001],\n" String errorResponse = "{\n"
+ " \"error_uri\": \"https://tools.ietf.org/html/rfc6749#section-5.2\"\n" + "}\n"; + " \"error\": \"unauthorized_client\",\n"
+ " \"error_description\": \"The client is not authorized\",\n"
+ " \"error_codes\": [65001],\n"
+ " \"error_uri\": \"https://tools.ietf.org/html/rfc6749#section-5.2\"\n"
+ "}\n";
// @formatter:on
MockClientHttpResponse response = new MockClientHttpResponse(errorResponse.getBytes(), HttpStatus.BAD_REQUEST); MockClientHttpResponse response = new MockClientHttpResponse(errorResponse.getBytes(), HttpStatus.BAD_REQUEST);
OAuth2Error oauth2Error = this.messageConverter.readInternal(OAuth2Error.class, response); OAuth2Error oauth2Error = this.messageConverter.readInternal(OAuth2Error.class, response);
assertThat(oauth2Error.getErrorCode()).isEqualTo("unauthorized_client"); assertThat(oauth2Error.getErrorCode()).isEqualTo("unauthorized_client");

View File

@ -45,9 +45,16 @@ public class DefaultAddressStandardClaimTests {
@Test @Test
public void buildWhenAllAttributesProvidedThenAllAttributesAreSet() { public void buildWhenAllAttributesProvidedThenAllAttributesAreSet() {
AddressStandardClaim addressStandardClaim = new DefaultAddressStandardClaim.Builder().formatted(FORMATTED) // @formatter:off
.streetAddress(STREET_ADDRESS).locality(LOCALITY).region(REGION).postalCode(POSTAL_CODE) AddressStandardClaim addressStandardClaim = new DefaultAddressStandardClaim.Builder()
.country(COUNTRY).build(); .formatted(FORMATTED)
.streetAddress(STREET_ADDRESS)
.locality(LOCALITY)
.region(REGION)
.postalCode(POSTAL_CODE)
.country(COUNTRY)
.build();
// @formatter:on
assertThat(addressStandardClaim.getFormatted()).isEqualTo(FORMATTED); assertThat(addressStandardClaim.getFormatted()).isEqualTo(FORMATTED);
assertThat(addressStandardClaim.getStreetAddress()).isEqualTo(STREET_ADDRESS); assertThat(addressStandardClaim.getStreetAddress()).isEqualTo(STREET_ADDRESS);
assertThat(addressStandardClaim.getLocality()).isEqualTo(LOCALITY); assertThat(addressStandardClaim.getLocality()).isEqualTo(LOCALITY);

View File

@ -31,9 +31,15 @@ public class OidcIdTokenBuilderTests {
@Test @Test
public void buildWhenCalledTwiceThenGeneratesTwoOidcIdTokens() { public void buildWhenCalledTwiceThenGeneratesTwoOidcIdTokens() {
OidcIdToken.Builder idTokenBuilder = OidcIdToken.withTokenValue("token"); OidcIdToken.Builder idTokenBuilder = OidcIdToken.withTokenValue("token");
OidcIdToken first = idTokenBuilder.tokenValue("V1").claim("TEST_CLAIM_1", "C1").build(); // @formatter:off
OidcIdToken second = idTokenBuilder.tokenValue("V2").claim("TEST_CLAIM_1", "C2").claim("TEST_CLAIM_2", "C3") OidcIdToken first = idTokenBuilder.tokenValue("V1")
.claim("TEST_CLAIM_1", "C1")
.build(); .build();
OidcIdToken second = idTokenBuilder.tokenValue("V2")
.claim("TEST_CLAIM_1", "C2")
.claim("TEST_CLAIM_2", "C3")
.build();
// @formatter:on
assertThat(first.getClaims()).hasSize(1); assertThat(first.getClaims()).hasSize(1);
assertThat(first.getClaims().get("TEST_CLAIM_1")).isEqualTo("C1"); assertThat(first.getClaims().get("TEST_CLAIM_1")).isEqualTo("C1");
assertThat(first.getTokenValue()).isEqualTo("V1"); assertThat(first.getTokenValue()).isEqualTo("V1");
@ -72,7 +78,12 @@ public class OidcIdTokenBuilderTests {
OidcIdToken.Builder idTokenBuilder = OidcIdToken.withTokenValue("token"); OidcIdToken.Builder idTokenBuilder = OidcIdToken.withTokenValue("token");
String generic = new String("sub"); String generic = new String("sub");
String named = new String("sub"); String named = new String("sub");
OidcIdToken idToken = idTokenBuilder.subject(named).claim(IdTokenClaimNames.SUB, generic).build(); // @formatter:off
OidcIdToken idToken = idTokenBuilder
.subject(named)
.claim(IdTokenClaimNames.SUB, generic)
.build();
// @formatter:on
assertThat(idToken.getSubject()).isSameAs(generic); assertThat(idToken.getSubject()).isSameAs(generic);
idToken = idTokenBuilder.claim(IdTokenClaimNames.SUB, generic).subject(named).build(); idToken = idTokenBuilder.claim(IdTokenClaimNames.SUB, generic).subject(named).build();
assertThat(idToken.getSubject()).isSameAs(named); assertThat(idToken.getSubject()).isSameAs(named);
@ -80,9 +91,13 @@ public class OidcIdTokenBuilderTests {
@Test @Test
public void claimsWhenRemovingAClaimThenIsNotPresent() { public void claimsWhenRemovingAClaimThenIsNotPresent() {
OidcIdToken.Builder idTokenBuilder = OidcIdToken.withTokenValue("token").claim("needs", "a claim"); // @formatter:off
OidcIdToken idToken = idTokenBuilder.subject("sub").claims((claims) -> claims.remove(IdTokenClaimNames.SUB)) OidcIdToken.Builder idTokenBuilder = OidcIdToken.withTokenValue("token")
.claim("needs", "a claim");
OidcIdToken idToken = idTokenBuilder.subject("sub")
.claims((claims) -> claims.remove(IdTokenClaimNames.SUB))
.build(); .build();
// @formatter:on
assertThat(idToken.getSubject()).isNull(); assertThat(idToken.getSubject()).isNull();
} }
@ -91,7 +106,11 @@ public class OidcIdTokenBuilderTests {
OidcIdToken.Builder idTokenBuilder = OidcIdToken.withTokenValue("token"); OidcIdToken.Builder idTokenBuilder = OidcIdToken.withTokenValue("token");
String name = new String("name"); String name = new String("name");
String value = new String("value"); String value = new String("value");
OidcIdToken idToken = idTokenBuilder.claims((claims) -> claims.put(name, value)).build(); // @formatter:off
OidcIdToken idToken = idTokenBuilder
.claims((claims) -> claims.put(name, value))
.build();
// @formatter:on
assertThat(idToken.getClaims()).hasSize(1); assertThat(idToken.getClaims()).hasSize(1);
assertThat(idToken.getClaims().get(name)).isSameAs(value); assertThat(idToken.getClaims().get(name)).isSameAs(value);
} }

View File

@ -28,8 +28,15 @@ public class OidcUserInfoBuilderTests {
@Test @Test
public void buildWhenCalledTwiceThenGeneratesTwoOidcUserInfos() { public void buildWhenCalledTwiceThenGeneratesTwoOidcUserInfos() {
OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder(); OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder();
OidcUserInfo first = userInfoBuilder.claim("TEST_CLAIM_1", "C1").build(); // @formatter:off
OidcUserInfo second = userInfoBuilder.claim("TEST_CLAIM_1", "C2").claim("TEST_CLAIM_2", "C3").build(); OidcUserInfo first = userInfoBuilder
.claim("TEST_CLAIM_1", "C1")
.build();
OidcUserInfo second = userInfoBuilder
.claim("TEST_CLAIM_1", "C2")
.claim("TEST_CLAIM_2", "C3")
.build();
// @formatter:on
assertThat(first.getClaims()).hasSize(1); assertThat(first.getClaims()).hasSize(1);
assertThat(first.getClaims().get("TEST_CLAIM_1")).isEqualTo("C1"); assertThat(first.getClaims().get("TEST_CLAIM_1")).isEqualTo("C1");
assertThat(second.getClaims()).hasSize(2); assertThat(second.getClaims()).hasSize(2);
@ -42,17 +49,31 @@ public class OidcUserInfoBuilderTests {
OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder(); OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder();
String generic = new String("sub"); String generic = new String("sub");
String named = new String("sub"); String named = new String("sub");
OidcUserInfo userInfo = userInfoBuilder.subject(named).claim(IdTokenClaimNames.SUB, generic).build(); // @formatter:off
OidcUserInfo userInfo = userInfoBuilder
.subject(named)
.claim(IdTokenClaimNames.SUB, generic)
.build();
// @formatter:on
assertThat(userInfo.getSubject()).isSameAs(generic); assertThat(userInfo.getSubject()).isSameAs(generic);
userInfo = userInfoBuilder.claim(IdTokenClaimNames.SUB, generic).subject(named).build(); // @formatter:off
userInfo = userInfoBuilder
.claim(IdTokenClaimNames.SUB, generic)
.subject(named)
.build();
// @formatter:on
assertThat(userInfo.getSubject()).isSameAs(named); assertThat(userInfo.getSubject()).isSameAs(named);
} }
@Test @Test
public void claimsWhenRemovingAClaimThenIsNotPresent() { public void claimsWhenRemovingAClaimThenIsNotPresent() {
OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder().claim("needs", "a claim"); // @formatter:off
OidcUserInfo userInfo = userInfoBuilder.subject("sub").claims((claims) -> claims.remove(IdTokenClaimNames.SUB)) OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder()
.claim("needs", "a claim");
OidcUserInfo userInfo = userInfoBuilder.subject("sub")
.claims((claims) -> claims.remove(IdTokenClaimNames.SUB))
.build(); .build();
// @formatter:on
assertThat(userInfo.getSubject()).isNull(); assertThat(userInfo.getSubject()).isNull();
} }
@ -61,7 +82,11 @@ public class OidcUserInfoBuilderTests {
OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder(); OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder();
String name = new String("name"); String name = new String("name");
String value = new String("value"); String value = new String("value");
OidcUserInfo userInfo = userInfoBuilder.claims((claims) -> claims.put(name, value)).build(); // @formatter:off
OidcUserInfo userInfo = userInfoBuilder
.claims((claims) -> claims.put(name, value))
.build();
// @formatter:on
assertThat(userInfo.getClaims()).hasSize(1); assertThat(userInfo.getClaims()).hasSize(1);
assertThat(userInfo.getClaims().get(name)).isSameAs(value); assertThat(userInfo.getClaims().get(name)).isSameAs(value);
} }

View File

@ -29,8 +29,15 @@ public final class TestOidcIdTokens {
} }
public static OidcIdToken.Builder idToken() { public static OidcIdToken.Builder idToken() {
return OidcIdToken.withTokenValue("id-token").issuer("https://example.com").subject("subject") // @formatter:off
.issuedAt(Instant.now()).expiresAt(Instant.now().plusSeconds(86400)).claim("id", "id"); return OidcIdToken.withTokenValue("id-token")
.issuer("https://example.com")
.subject("subject")
.issuedAt(Instant.now())
.expiresAt(Instant.now()
.plusSeconds(86400))
.claim("id", "id");
// @formatter:on
} }
} }

View File

@ -44,10 +44,16 @@ public final class TestOidcUsers {
private static OidcIdToken idToken() { private static OidcIdToken idToken() {
Instant issuedAt = Instant.now(); Instant issuedAt = Instant.now();
Instant expiresAt = issuedAt.plusSeconds(3600); Instant expiresAt = issuedAt.plusSeconds(3600);
return OidcIdToken.withTokenValue("id-token").issuedAt(issuedAt).expiresAt(expiresAt).subject("subject") // @formatter:off
return OidcIdToken.withTokenValue("id-token")
.issuedAt(issuedAt)
.expiresAt(expiresAt)
.subject("subject")
.issuer("http://localhost/issuer") .issuer("http://localhost/issuer")
.audience(Collections.unmodifiableSet(new LinkedHashSet<>(Collections.singletonList("client")))) .audience(Collections.unmodifiableSet(new LinkedHashSet<>(Collections.singletonList("client"))))
.authorizedParty("client").build(); .authorizedParty("client")
.build();
// @formatter:on
} }
private static OidcUserInfo userInfo() { private static OidcUserInfo userInfo() {

View File

@ -90,8 +90,11 @@ public class OAuth2BodyExtractorsTests {
response.getHeaders().setContentType(MediaType.APPLICATION_JSON); response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
response.setBody("{"); response.setBody("{");
Mono<OAuth2AccessTokenResponse> result = extractor.extract(response, this.context); Mono<OAuth2AccessTokenResponse> result = extractor.extract(response, this.context);
assertThatExceptionOfType(OAuth2AuthorizationException.class).isThrownBy(result::block) // @formatter:off
assertThatExceptionOfType(OAuth2AuthorizationException.class)
.isThrownBy(result::block)
.withMessageContaining("An error occurred parsing the Access Token response"); .withMessageContaining("An error occurred parsing the Access Token response");
// @formatter:on
} }
@Test @Test
@ -100,8 +103,11 @@ public class OAuth2BodyExtractorsTests {
.oauth2AccessTokenResponse(); .oauth2AccessTokenResponse();
MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK); MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
Mono<OAuth2AccessTokenResponse> result = extractor.extract(response, this.context); Mono<OAuth2AccessTokenResponse> result = extractor.extract(response, this.context);
assertThatExceptionOfType(OAuth2AuthorizationException.class).isThrownBy(result::block) // @formatter:off
assertThatExceptionOfType(OAuth2AuthorizationException.class)
.isThrownBy(result::block)
.withMessageContaining("Empty OAuth 2.0 Access Token Response"); .withMessageContaining("Empty OAuth 2.0 Access Token Response");
// @formatter:on
} }
@Test @Test
@ -110,10 +116,16 @@ public class OAuth2BodyExtractorsTests {
.oauth2AccessTokenResponse(); .oauth2AccessTokenResponse();
MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK); MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON); response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
// @formatter:off
response.setBody( response.setBody(
"{\n" + " \"access_token\":\"2YotnFZFEjr1zCsicMWpAA\",\n" + " \"token_type\":\"Bearer\",\n" "{\n"
+ " \"expires_in\":3600,\n" + " \"refresh_token\":\"tGzv3JOkF0XG5Qx2TlKWIA\",\n" + " \"access_token\":\"2YotnFZFEjr1zCsicMWpAA\",\n"
+ " \"example_parameter\":\"example_value\"\n" + " }"); + " \"token_type\":\"Bearer\",\n"
+ " \"expires_in\":3600,\n"
+ " \"refresh_token\":\"tGzv3JOkF0XG5Qx2TlKWIA\",\n"
+ " \"example_parameter\":\"example_value\"\n"
+ " }");
// @formatter:on
Instant now = Instant.now(); Instant now = Instant.now();
OAuth2AccessTokenResponse result = extractor.extract(response, this.context).block(); OAuth2AccessTokenResponse result = extractor.extract(response, this.context).block();
assertThat(result.getAccessToken().getTokenValue()).isEqualTo("2YotnFZFEjr1zCsicMWpAA"); assertThat(result.getAccessToken().getTokenValue()).isEqualTo("2YotnFZFEjr1zCsicMWpAA");
@ -130,10 +142,17 @@ public class OAuth2BodyExtractorsTests {
.oauth2AccessTokenResponse(); .oauth2AccessTokenResponse();
MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK); MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON); response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
// @formatter:off
response.setBody( response.setBody(
"{\n" + " \"access_token\":\"2YotnFZFEjr1zCsicMWpAA\",\n" + " \"token_type\":\"Bearer\",\n" "{\n"
+ " \"expires_in\":3600,\n" + " \"refresh_token\":\"tGzv3JOkF0XG5Qx2TlKWIA\",\n" + " \"access_token\":\"2YotnFZFEjr1zCsicMWpAA\",\n"
+ " \"subjson\":{}, \n" + " \"list\":[] \n" + " }"); + " \"token_type\":\"Bearer\",\n"
+ " \"expires_in\":3600,\n"
+ " \"refresh_token\":\"tGzv3JOkF0XG5Qx2TlKWIA\",\n"
+ " \"subjson\":{}, \n"
+ " \"list\":[] \n"
+ " }");
// @formatter:on
Instant now = Instant.now(); Instant now = Instant.now();
OAuth2AccessTokenResponse result = extractor.extract(response, this.context).block(); OAuth2AccessTokenResponse result = extractor.extract(response, this.context).block();
assertThat(result.getAccessToken().getTokenValue()).isEqualTo("2YotnFZFEjr1zCsicMWpAA"); assertThat(result.getAccessToken().getTokenValue()).isEqualTo("2YotnFZFEjr1zCsicMWpAA");