diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/MapOAuth2AccessTokenResponseConverter.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/MapOAuth2AccessTokenResponseConverter.java index fefa9eb7c3..ef5e138232 100644 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/MapOAuth2AccessTokenResponseConverter.java +++ b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/MapOAuth2AccessTokenResponseConverter.java @@ -55,8 +55,15 @@ public final class MapOAuth2AccessTokenResponseConverter additionalParameters.put(entry.getKey(), entry.getValue()); } } - return OAuth2AccessTokenResponse.withToken(accessToken).tokenType(accessTokenType).expiresIn(expiresIn) - .scopes(scopes).refreshToken(refreshToken).additionalParameters(additionalParameters).build(); + // @formatter:off + return OAuth2AccessTokenResponse.withToken(accessToken) + .tokenType(accessTokenType) + .expiresIn(expiresIn) + .scopes(scopes) + .refreshToken(refreshToken) + .additionalParameters(additionalParameters) + .build(); + // @formatter:on } private OAuth2AccessToken.TokenType getAccessTokenType(Map tokenResponseParameters) { diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java index ec04c68a55..9809ea6c1f 100644 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java +++ b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java @@ -215,12 +215,16 @@ public final class OAuth2AuthorizationRequest implements Serializable { */ public static Builder from(OAuth2AuthorizationRequest authorizationRequest) { Assert.notNull(authorizationRequest, "authorizationRequest cannot be null"); + // @formatter:off return new Builder(authorizationRequest.getGrantType()) .authorizationUri(authorizationRequest.getAuthorizationUri()) - .clientId(authorizationRequest.getClientId()).redirectUri(authorizationRequest.getRedirectUri()) - .scopes(authorizationRequest.getScopes()).state(authorizationRequest.getState()) + .clientId(authorizationRequest.getClientId()) + .redirectUri(authorizationRequest.getRedirectUri()) + .scopes(authorizationRequest.getScopes()) + .state(authorizationRequest.getState()) .additionalParameters(authorizationRequest.getAdditionalParameters()) .attributes(authorizationRequest.getAttributes()); + // @formatter:on } /** diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverter.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverter.java index 5203ce484c..513a14fc82 100644 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverter.java +++ b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverter.java @@ -77,8 +77,12 @@ public class OAuth2AccessTokenResponseHttpMessageConverter // Object and then convert values to String Map tokenResponseParameters = (Map) this.jsonMessageConverter .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())))); + // @formatter:on } catch (Exception ex) { throw new HttpMessageNotReadableException( diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/web/reactive/function/OAuth2AccessTokenResponseBodyExtractor.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/web/reactive/function/OAuth2AccessTokenResponseBodyExtractor.java index 5154cb07a8..6c3d93c1c0 100644 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/web/reactive/function/OAuth2AccessTokenResponseBodyExtractor.java +++ b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/web/reactive/function/OAuth2AccessTokenResponseBodyExtractor.java @@ -123,9 +123,15 @@ class OAuth2AccessTokenResponseBodyExtractor refreshToken = accessTokenResponse.getTokens().getRefreshToken().getValue(); } Map additionalParameters = new LinkedHashMap<>(accessTokenResponse.getCustomParameters()); - return OAuth2AccessTokenResponse.withToken(accessToken.getValue()).tokenType(accessTokenType) - .expiresIn(expiresIn).scopes(scopes).refreshToken(refreshToken) - .additionalParameters(additionalParameters).build(); + // @formatter:off + return OAuth2AccessTokenResponse.withToken(accessToken.getValue()) + .tokenType(accessTokenType) + .expiresIn(expiresIn) + .scopes(scopes) + .refreshToken(refreshToken) + .additionalParameters(additionalParameters) + .build(); + // @formatter:on } } diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AccessTokenResponseMapConverterTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AccessTokenResponseMapConverterTests.java index 1fb64a63f6..ae4f4117b4 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AccessTokenResponseMapConverterTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AccessTokenResponseMapConverterTests.java @@ -49,9 +49,15 @@ public class OAuth2AccessTokenResponseMapConverterTests { Set scopes = new HashSet<>(); scopes.add("read"); scopes.add("write"); - OAuth2AccessTokenResponse build = OAuth2AccessTokenResponse.withToken("access-token-value-1234").expiresIn(3699) - .additionalParameters(additionalParameters).refreshToken("refresh-token-value-1234").scopes(scopes) - .tokenType(OAuth2AccessToken.TokenType.BEARER).build(); + // @formatter:off + OAuth2AccessTokenResponse build = OAuth2AccessTokenResponse.withToken("access-token-value-1234") + .expiresIn(3699) + .additionalParameters(additionalParameters) + .refreshToken("refresh-token-value-1234") + .scopes(scopes) + .tokenType(OAuth2AccessToken.TokenType.BEARER) + .build(); + // @formatter:on Map result = this.messageConverter.convert(build); Assert.assertEquals(7, result.size()); Assert.assertEquals("access-token-value-1234", result.get("access_token")); @@ -65,8 +71,11 @@ public class OAuth2AccessTokenResponseMapConverterTests { @Test public void convertMinimal() { + // @formatter:off OAuth2AccessTokenResponse build = OAuth2AccessTokenResponse.withToken("access-token-value-1234") - .tokenType(OAuth2AccessToken.TokenType.BEARER).build(); + .tokenType(OAuth2AccessToken.TokenType.BEARER) + .build(); + // @formatter:on Map result = this.messageConverter.convert(build); Assert.assertEquals(3, result.size()); Assert.assertEquals("access-token-value-1234", result.get("access_token")); diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AccessTokenResponseTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AccessTokenResponseTests.java index a9934d43b1..1d1974f8e4 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AccessTokenResponseTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AccessTokenResponseTests.java @@ -45,27 +45,44 @@ public class OAuth2AccessTokenResponseTests { @Test(expected = IllegalArgumentException.class) 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(); + // @formatter:on } @Test(expected = IllegalArgumentException.class) 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 public void buildWhenExpiresInIsZeroThenExpiresAtOneSecondAfterIssueAt() { + // @formatter:off 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()) .isEqualTo(tokenResponse.getAccessToken().getIssuedAt().plusSeconds(1)); } @Test public void buildWhenExpiresInIsNegativeThenExpiresAtOneSecondAfterIssueAt() { + // @formatter:off 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()) .isEqualTo(tokenResponse.getAccessToken().getIssuedAt().plusSeconds(1)); } @@ -77,9 +94,15 @@ public class OAuth2AccessTokenResponseTests { Map additionalParameters = new HashMap<>(); additionalParameters.put("param1", "value1"); additionalParameters.put("param2", "value2"); + // @formatter:off OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE) - .tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(expiresAt.toEpochMilli()).scopes(scopes) - .refreshToken(REFRESH_TOKEN_VALUE).additionalParameters(additionalParameters).build(); + .tokenType(OAuth2AccessToken.TokenType.BEARER) + .expiresIn(expiresAt.toEpochMilli()) + .scopes(scopes) + .refreshToken(REFRESH_TOKEN_VALUE) + .additionalParameters(additionalParameters) + .build(); + // @formatter:on assertThat(tokenResponse.getAccessToken()).isNotNull(); assertThat(tokenResponse.getAccessToken().getTokenValue()).isEqualTo(TOKEN_VALUE); assertThat(tokenResponse.getAccessToken().getTokenType()).isEqualTo(OAuth2AccessToken.TokenType.BEARER); @@ -97,9 +120,15 @@ public class OAuth2AccessTokenResponseTests { Map additionalParameters = new HashMap<>(); additionalParameters.put("param1", "value1"); additionalParameters.put("param2", "value2"); + // @formatter:off OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE) - .tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(expiresAt.toEpochMilli()).scopes(scopes) - .refreshToken(REFRESH_TOKEN_VALUE).additionalParameters(additionalParameters).build(); + .tokenType(OAuth2AccessToken.TokenType.BEARER) + .expiresIn(expiresAt.toEpochMilli()) + .scopes(scopes) + .refreshToken(REFRESH_TOKEN_VALUE) + .additionalParameters(additionalParameters) + .build(); + // @formatter:on OAuth2AccessTokenResponse withResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse).build(); assertThat(withResponse.getAccessToken().getTokenValue()) .isEqualTo(tokenResponse.getAccessToken().getTokenValue()); @@ -120,17 +149,25 @@ public class OAuth2AccessTokenResponseTests { Map additionalParameters = new HashMap<>(); additionalParameters.put("param1", "value1"); additionalParameters.put("param2", "value2"); + // @formatter:off OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE) - .tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(expiresAt.toEpochMilli()).scopes(scopes) - .additionalParameters(additionalParameters).build(); + .tokenType(OAuth2AccessToken.TokenType.BEARER) + .expiresIn(expiresAt.toEpochMilli()) + .scopes(scopes) + .additionalParameters(additionalParameters) + .build(); + // @formatter:on OAuth2AccessTokenResponse withResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse).build(); assertThat(withResponse.getRefreshToken()).isNull(); } @Test public void buildWhenResponseAndExpiresInThenExpiresAtEqualToIssuedAtPlusExpiresIn() { + // @formatter:off OAuth2AccessTokenResponse tokenResponse = OAuth2AccessTokenResponse.withToken(TOKEN_VALUE) - .tokenType(OAuth2AccessToken.TokenType.BEARER).build(); + .tokenType(OAuth2AccessToken.TokenType.BEARER) + .build(); + // @formatter:on long expiresIn = 30; OAuth2AccessTokenResponse withResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse) .expiresIn(expiresIn).build(); diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java index c823500f34..ddb69f6527 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java @@ -50,63 +50,128 @@ public class OAuth2AuthorizationRequestTests { @Test public void buildWhenAuthorizationUriIsNullThenThrowIllegalArgumentException() { + // @formatter:off assertThatIllegalArgumentException() - .isThrownBy(() -> OAuth2AuthorizationRequest.authorizationCode().authorizationUri(null) - .clientId(CLIENT_ID).redirectUri(REDIRECT_URI).scopes(SCOPES).state(STATE).build()); + .isThrownBy(() -> OAuth2AuthorizationRequest + .authorizationCode() + .authorizationUri(null) + .clientId(CLIENT_ID) + .redirectUri(REDIRECT_URI) + .scopes(SCOPES) + .state(STATE) + .build() + ); + // @formatter:on } @Test public void buildWhenClientIdIsNullThenThrowIllegalArgumentException() { + // @formatter:off assertThatIllegalArgumentException() - .isThrownBy(() -> OAuth2AuthorizationRequest.authorizationCode().authorizationUri(AUTHORIZATION_URI) - .clientId(null).redirectUri(REDIRECT_URI).scopes(SCOPES).state(STATE).build()); + .isThrownBy(() -> OAuth2AuthorizationRequest.authorizationCode() + .authorizationUri(AUTHORIZATION_URI) + .clientId(null) + .redirectUri(REDIRECT_URI) + .scopes(SCOPES) + .state(STATE) + .build() + ); + // @formatter:on } @Test public void buildWhenRedirectUriIsNullForImplicitThenThrowIllegalArgumentException() { + // @formatter:off assertThatIllegalArgumentException() - .isThrownBy(() -> OAuth2AuthorizationRequest.implicit().authorizationUri(AUTHORIZATION_URI) - .clientId(CLIENT_ID).redirectUri(null).scopes(SCOPES).state(STATE).build()); + .isThrownBy(() -> OAuth2AuthorizationRequest.implicit() + .authorizationUri(AUTHORIZATION_URI) + .clientId(CLIENT_ID) + .redirectUri(null) + .scopes(SCOPES) + .state(STATE).build() + ); + // @formatter:on } @Test public void buildWhenRedirectUriIsNullForAuthorizationCodeThenDoesNotThrowAnyException() { - OAuth2AuthorizationRequest.authorizationCode().authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID) - .redirectUri(null).scopes(SCOPES).state(STATE).build(); + // @formatter:off + OAuth2AuthorizationRequest.authorizationCode() + .authorizationUri(AUTHORIZATION_URI) + .clientId(CLIENT_ID) + .redirectUri(null) + .scopes(SCOPES) + .state(STATE) + .build(); + // @formatter:on } @Test public void buildWhenScopesIsNullThenDoesNotThrowAnyException() { - OAuth2AuthorizationRequest.authorizationCode().authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID) - .redirectUri(REDIRECT_URI).scopes(null).state(STATE).build(); + // @formatter:off + OAuth2AuthorizationRequest.authorizationCode() + .authorizationUri(AUTHORIZATION_URI) + .clientId(CLIENT_ID) + .redirectUri(REDIRECT_URI) + .scopes(null) + .state(STATE) + .build(); + // @formatter:on } @Test public void buildWhenStateIsNullThenDoesNotThrowAnyException() { - OAuth2AuthorizationRequest.authorizationCode().authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID) - .redirectUri(REDIRECT_URI).scopes(SCOPES).state(null).build(); + // @formatter:off + OAuth2AuthorizationRequest.authorizationCode() + .authorizationUri(AUTHORIZATION_URI) + .clientId(CLIENT_ID) + .redirectUri(REDIRECT_URI) + .scopes(SCOPES) + .state(null) + .build(); + // @formatter:on } @Test public void buildWhenAdditionalParametersEmptyThenDoesNotThrowAnyException() { - OAuth2AuthorizationRequest.authorizationCode().authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID) - .redirectUri(REDIRECT_URI).scopes(SCOPES).state(STATE).additionalParameters(Map::clear).build(); + // @formatter:off + OAuth2AuthorizationRequest.authorizationCode() + .authorizationUri(AUTHORIZATION_URI) + .clientId(CLIENT_ID) + .redirectUri(REDIRECT_URI) + .scopes(SCOPES) + .state(STATE) + .additionalParameters(Map::clear) + .build(); + // @formatter:on } @Test public void buildWhenImplicitThenGrantTypeResponseTypeIsSet() { + // @formatter:off OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.implicit() - .authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID).redirectUri(REDIRECT_URI).scopes(SCOPES) - .state(STATE).build(); + .authorizationUri(AUTHORIZATION_URI) + .clientId(CLIENT_ID) + .redirectUri(REDIRECT_URI) + .scopes(SCOPES) + .state(STATE) + .build(); + // @formatter:on assertThat(authorizationRequest.getGrantType()).isEqualTo(AuthorizationGrantType.IMPLICIT); assertThat(authorizationRequest.getResponseType()).isEqualTo(OAuth2AuthorizationResponseType.TOKEN); } @Test public void buildWhenAuthorizationCodeThenGrantTypeResponseTypeIsSet() { + // @formatter:off 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(); + // @formatter:on assertThat(authorizationRequest.getGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE); assertThat(authorizationRequest.getResponseType()).isEqualTo(OAuth2AuthorizationResponseType.CODE); } @@ -119,10 +184,18 @@ public class OAuth2AuthorizationRequestTests { Map attributes = new HashMap<>(); attributes.put("attribute1", "value1"); attributes.put("attribute2", "value2"); + // @formatter:off OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode() - .authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID).redirectUri(REDIRECT_URI).scopes(SCOPES) - .state(STATE).additionalParameters(additionalParameters).attributes(attributes) - .authorizationRequestUri(AUTHORIZATION_URI).build(); + .authorizationUri(AUTHORIZATION_URI) + .clientId(CLIENT_ID) + .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.getGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE); assertThat(authorizationRequest.getResponseType()).isEqualTo(OAuth2AuthorizationResponseType.CODE); @@ -137,9 +210,15 @@ public class OAuth2AuthorizationRequestTests { @Test public void buildWhenScopesMultiThenSeparatedByEncodedSpace() { + // @formatter:off OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.implicit() - .authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID).redirectUri(REDIRECT_URI).scopes(SCOPES) - .state(STATE).build(); + .authorizationUri(AUTHORIZATION_URI) + .clientId(CLIENT_ID) + .redirectUri(REDIRECT_URI) + .scopes(SCOPES) + .state(STATE) + .build(); + // @formatter:on assertThat(authorizationRequest.getAuthorizationRequestUri()) .isEqualTo("https://provider.com/oauth2/authorize?" + "response_type=token&client_id=client-id&" + "scope=scope1%20scope2&state=state&" + "redirect_uri=https://example.com"); @@ -147,17 +226,31 @@ public class OAuth2AuthorizationRequestTests { @Test public void buildWhenAuthorizationRequestUriSetThenOverridesDefault() { + // @formatter:off OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode() - .authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID).redirectUri(REDIRECT_URI).scopes(SCOPES) - .state(STATE).authorizationRequestUri(AUTHORIZATION_URI).build(); + .authorizationUri(AUTHORIZATION_URI) + .clientId(CLIENT_ID) + .redirectUri(REDIRECT_URI) + .scopes(SCOPES) + .state(STATE) + .authorizationRequestUri(AUTHORIZATION_URI) + .build(); + // @formatter:on assertThat(authorizationRequest.getAuthorizationRequestUri()).isEqualTo(AUTHORIZATION_URI); } @Test public void buildWhenAuthorizationRequestUriFunctionSetThenOverridesDefault() { + // @formatter:off OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode() - .authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID).redirectUri(REDIRECT_URI).scopes(SCOPES) - .state(STATE).authorizationRequestUri((uriBuilder) -> URI.create(AUTHORIZATION_URI)).build(); + .authorizationUri(AUTHORIZATION_URI) + .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); } @@ -196,11 +289,19 @@ public class OAuth2AuthorizationRequestTests { Map attributes = new HashMap<>(); attributes.put("attribute1", "value1"); attributes.put("attribute2", "value2"); + // @formatter:off OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode() - .authorizationUri(AUTHORIZATION_URI).clientId(CLIENT_ID).redirectUri(REDIRECT_URI).scopes(SCOPES) - .state(STATE).additionalParameters(additionalParameters).attributes(attributes).build(); + .authorizationUri(AUTHORIZATION_URI) + .clientId(CLIENT_ID) + .redirectUri(REDIRECT_URI) + .scopes(SCOPES) + .state(STATE) + .additionalParameters(additionalParameters) + .attributes(attributes) + .build(); OAuth2AuthorizationRequest authorizationRequestCopy = OAuth2AuthorizationRequest.from(authorizationRequest) .build(); + // @formatter:on assertThat(authorizationRequestCopy.getAuthorizationUri()) .isEqualTo(authorizationRequest.getAuthorizationUri()); assertThat(authorizationRequestCopy.getGrantType()).isEqualTo(authorizationRequest.getGrantType()); diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationResponseTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationResponseTests.java index dd7c5e1d9d..413fb236cd 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationResponseTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationResponseTests.java @@ -41,63 +41,122 @@ public class OAuth2AuthorizationResponseTests { @Test(expected = IllegalArgumentException.class) 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) 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 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 public void buildSuccessResponseWhenAllAttributesProvidedThenAllAttributesAreSet() { + // @formatter:off OAuth2AuthorizationResponse authorizationResponse = OAuth2AuthorizationResponse.success(AUTH_CODE) - .redirectUri(REDIRECT_URI).state(STATE).build(); - assertThat(authorizationResponse.getCode()).isEqualTo(AUTH_CODE); - assertThat(authorizationResponse.getRedirectUri()).isEqualTo(REDIRECT_URI); - assertThat(authorizationResponse.getState()).isEqualTo(STATE); + .redirectUri(REDIRECT_URI) + .state(STATE) + .build(); + assertThat(authorizationResponse.getCode()) + .isEqualTo(AUTH_CODE); + assertThat(authorizationResponse.getRedirectUri()) + .isEqualTo(REDIRECT_URI); + assertThat(authorizationResponse.getState()) + .isEqualTo(STATE); + // @formatter:on } @Test(expected = IllegalArgumentException.class) 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(); + // @formatter:on } @Test(expected = IllegalArgumentException.class) 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) 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 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 public void buildErrorResponseWhenAllAttributesProvidedThenAllAttributesAreSet() { + // @formatter:off OAuth2AuthorizationResponse authorizationResponse = OAuth2AuthorizationResponse.error(ERROR_CODE) - .errorDescription(ERROR_DESCRIPTION).errorUri(ERROR_URI).redirectUri(REDIRECT_URI).state(STATE).build(); - 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); + .errorDescription(ERROR_DESCRIPTION) + .errorUri(ERROR_URI) + .redirectUri(REDIRECT_URI) + .state(STATE) + .build(); + 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) 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 } } diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AccessTokenResponses.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AccessTokenResponses.java index a2eebd66bd..f952ff4bd5 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AccessTokenResponses.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AccessTokenResponses.java @@ -32,7 +32,11 @@ public final class TestOAuth2AccessTokenResponses { } 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() { diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationRequests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationRequests.java index 41123de992..eaf559a28d 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationRequests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationRequests.java @@ -33,10 +33,14 @@ public final class TestOAuth2AuthorizationRequests { String clientId = "client-id"; Map attributes = new HashMap<>(); attributes.put(OAuth2ParameterNames.REGISTRATION_ID, registrationId); + // @formatter:off return OAuth2AuthorizationRequest.authorizationCode() - .authorizationUri("https://example.com/login/oauth/authorize").clientId(clientId) - .redirectUri("https://example.com/authorize/oauth2/code/registration-id").state("state") + .authorizationUri("https://example.com/login/oauth/authorize") + .clientId(clientId) + .redirectUri("https://example.com/authorize/oauth2/code/registration-id") + .state("state") .attributes(attributes); + // @formatter:on } public static OAuth2AuthorizationRequest.Builder oidcRequest() { diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationResponses.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationResponses.java index 5dec72377f..b4c6db04bb 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationResponses.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/TestOAuth2AuthorizationResponses.java @@ -26,14 +26,19 @@ public final class TestOAuth2AuthorizationResponses { } 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"); + // @formatter:on } public static OAuth2AuthorizationResponse.Builder error() { + // @formatter:off return OAuth2AuthorizationResponse.error("error") .redirectUri("https://example.com/authorize/oauth2/code/registration-id") .errorUri("https://example.com/error"); + // @formatter:on } } diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverterTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverterTests.java index 0fc466a072..8a714ad03b 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverterTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverterTests.java @@ -64,7 +64,8 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests { @Test public void setTokenResponseConverterWhenConverterIsNullThenThrowIllegalArgumentException() { - assertThatIllegalArgumentException().isThrownBy(() -> this.messageConverter.setTokenResponseConverter(null)); + assertThatIllegalArgumentException() + .isThrownBy(() -> this.messageConverter.setTokenResponseConverter(null)); } @Test @@ -75,11 +76,17 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests { @Test public void readInternalWhenSuccessfulTokenResponseThenReadOAuth2AccessTokenResponse() throws Exception { - 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" - + " \"custom_parameter_1\": \"custom-value-1\",\n" + " \"custom_parameter_2\": \"custom-value-2\"\n" - + "}\n"; + // @formatter:off + 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" + + " \"custom_parameter_1\": \"custom-value-1\",\n" + + " \"custom_parameter_2\": \"custom-value-2\"\n" + + "}\n"; + // @formatter:on MockClientHttpResponse response = new MockClientHttpResponse(tokenResponse.getBytes(), HttpStatus.OK); OAuth2AccessTokenResponse accessTokenResponse = this.messageConverter .readInternal(OAuth2AccessTokenResponse.class, response); @@ -96,13 +103,19 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests { // gh-6463 @Test public void readInternalWhenSuccessfulTokenResponseWithObjectThenReadOAuth2AccessTokenResponse() { - 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" - + " \"custom_object_1\": {\"name1\": \"value1\"},\n" - + " \"custom_object_2\": [\"value1\", \"value2\"],\n" - + " \"custom_parameter_1\": \"custom-value-1\",\n" + " \"custom_parameter_2\": \"custom-value-2\"\n" - + "}\n"; + // @formatter:off + 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" + + " \"custom_object_1\": {\"name1\": \"value1\"},\n" + + " \"custom_object_2\": [\"value1\", \"value2\"],\n" + + " \"custom_parameter_1\": \"custom-value-1\",\n" + + " \"custom_parameter_2\": \"custom-value-2\"\n" + + "}\n"; + // @formatter:on MockClientHttpResponse response = new MockClientHttpResponse(tokenResponse.getBytes(), HttpStatus.OK); OAuth2AccessTokenResponse accessTokenResponse = this.messageConverter .readInternal(OAuth2AccessTokenResponse.class, response); @@ -120,9 +133,15 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests { // gh-8108 @Test public void readInternalWhenSuccessfulTokenResponseWithNullValueThenReadOAuth2AccessTokenResponse() { - String tokenResponse = "{\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:off + String tokenResponse = "{\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); OAuth2AccessTokenResponse accessTokenResponse = this.messageConverter .readInternal(OAuth2AccessTokenResponse.class, response); @@ -153,9 +172,15 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests { Map additionalParameters = new HashMap<>(); additionalParameters.put("custom_parameter_1", "custom-value-1"); additionalParameters.put("custom_parameter_2", "custom-value-2"); + // @formatter:off OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234") - .tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(expiresAt.toEpochMilli()).scopes(scopes) - .refreshToken("refresh-token-1234").additionalParameters(additionalParameters).build(); + .tokenType(OAuth2AccessToken.TokenType.BEARER) + .expiresIn(expiresAt.toEpochMilli()) + .scopes(scopes) + .refreshToken("refresh-token-1234") + .additionalParameters(additionalParameters) + .build(); + // @formatter:on MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); this.messageConverter.writeInternal(accessTokenResponse, outputMessage); String tokenResponse = outputMessage.getBodyAsString(); @@ -173,9 +198,14 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests { Converter tokenResponseParametersConverter = mock(Converter.class); given(tokenResponseParametersConverter.convert(any())).willThrow(RuntimeException.class); this.messageConverter.setTokenResponseParametersConverter(tokenResponseParametersConverter); - OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234") - .tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(Instant.now().plusSeconds(3600).toEpochMilli()) + // @formatter:off + OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse + .withToken("access-token-1234") + .tokenType(OAuth2AccessToken.TokenType.BEARER) + .expiresIn(Instant.now().plusSeconds(3600) + .toEpochMilli()) .build(); + // @formatter:on MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); assertThatExceptionOfType(HttpMessageNotWritableException.class) .isThrownBy(() -> this.messageConverter.writeInternal(accessTokenResponse, outputMessage)) diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/http/converter/OAuth2ErrorHttpMessageConverterTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/http/converter/OAuth2ErrorHttpMessageConverterTests.java index a28b1d890e..35e697fdf6 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/http/converter/OAuth2ErrorHttpMessageConverterTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/http/converter/OAuth2ErrorHttpMessageConverterTests.java @@ -65,9 +65,13 @@ public class OAuth2ErrorHttpMessageConverterTests { @Test public void readInternalWhenErrorResponseThenReadOAuth2Error() throws Exception { - String errorResponse = "{\n" + " \"error\": \"unauthorized_client\",\n" - + " \"error_description\": \"The client is not authorized\",\n" - + " \"error_uri\": \"https://tools.ietf.org/html/rfc6749#section-5.2\"\n" + "}\n"; + // @formatter:off + String errorResponse = "{\n" + + " \"error\": \"unauthorized_client\",\n" + + " \"error_description\": \"The client is not authorized\",\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); OAuth2Error oauth2Error = this.messageConverter.readInternal(OAuth2Error.class, response); assertThat(oauth2Error.getErrorCode()).isEqualTo("unauthorized_client"); @@ -78,9 +82,14 @@ public class OAuth2ErrorHttpMessageConverterTests { // gh-8157 @Test public void readInternalWhenErrorResponseWithObjectThenReadOAuth2Error() throws Exception { - String errorResponse = "{\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:off + String errorResponse = "{\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); OAuth2Error oauth2Error = this.messageConverter.readInternal(OAuth2Error.class, response); assertThat(oauth2Error.getErrorCode()).isEqualTo("unauthorized_client"); diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/DefaultAddressStandardClaimTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/DefaultAddressStandardClaimTests.java index ebe7aa9919..d85fd854af 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/DefaultAddressStandardClaimTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/DefaultAddressStandardClaimTests.java @@ -45,9 +45,16 @@ public class DefaultAddressStandardClaimTests { @Test public void buildWhenAllAttributesProvidedThenAllAttributesAreSet() { - AddressStandardClaim addressStandardClaim = new DefaultAddressStandardClaim.Builder().formatted(FORMATTED) - .streetAddress(STREET_ADDRESS).locality(LOCALITY).region(REGION).postalCode(POSTAL_CODE) - .country(COUNTRY).build(); + // @formatter:off + AddressStandardClaim addressStandardClaim = new DefaultAddressStandardClaim.Builder() + .formatted(FORMATTED) + .streetAddress(STREET_ADDRESS) + .locality(LOCALITY) + .region(REGION) + .postalCode(POSTAL_CODE) + .country(COUNTRY) + .build(); + // @formatter:on assertThat(addressStandardClaim.getFormatted()).isEqualTo(FORMATTED); assertThat(addressStandardClaim.getStreetAddress()).isEqualTo(STREET_ADDRESS); assertThat(addressStandardClaim.getLocality()).isEqualTo(LOCALITY); diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/OidcIdTokenBuilderTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/OidcIdTokenBuilderTests.java index 0b62a74f9b..56d8f7e48c 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/OidcIdTokenBuilderTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/OidcIdTokenBuilderTests.java @@ -31,9 +31,15 @@ public class OidcIdTokenBuilderTests { @Test public void buildWhenCalledTwiceThenGeneratesTwoOidcIdTokens() { OidcIdToken.Builder idTokenBuilder = OidcIdToken.withTokenValue("token"); - OidcIdToken first = idTokenBuilder.tokenValue("V1").claim("TEST_CLAIM_1", "C1").build(); - OidcIdToken second = idTokenBuilder.tokenValue("V2").claim("TEST_CLAIM_1", "C2").claim("TEST_CLAIM_2", "C3") + // @formatter:off + OidcIdToken first = idTokenBuilder.tokenValue("V1") + .claim("TEST_CLAIM_1", "C1") .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().get("TEST_CLAIM_1")).isEqualTo("C1"); assertThat(first.getTokenValue()).isEqualTo("V1"); @@ -72,7 +78,12 @@ public class OidcIdTokenBuilderTests { OidcIdToken.Builder idTokenBuilder = OidcIdToken.withTokenValue("token"); String generic = 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); idToken = idTokenBuilder.claim(IdTokenClaimNames.SUB, generic).subject(named).build(); assertThat(idToken.getSubject()).isSameAs(named); @@ -80,9 +91,13 @@ public class OidcIdTokenBuilderTests { @Test public void claimsWhenRemovingAClaimThenIsNotPresent() { - OidcIdToken.Builder idTokenBuilder = OidcIdToken.withTokenValue("token").claim("needs", "a claim"); - OidcIdToken idToken = idTokenBuilder.subject("sub").claims((claims) -> claims.remove(IdTokenClaimNames.SUB)) + // @formatter:off + OidcIdToken.Builder idTokenBuilder = OidcIdToken.withTokenValue("token") + .claim("needs", "a claim"); + OidcIdToken idToken = idTokenBuilder.subject("sub") + .claims((claims) -> claims.remove(IdTokenClaimNames.SUB)) .build(); + // @formatter:on assertThat(idToken.getSubject()).isNull(); } @@ -91,7 +106,11 @@ public class OidcIdTokenBuilderTests { OidcIdToken.Builder idTokenBuilder = OidcIdToken.withTokenValue("token"); String name = new String("name"); 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().get(name)).isSameAs(value); } diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/OidcUserInfoBuilderTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/OidcUserInfoBuilderTests.java index 8877139e0f..b7e02d0ed8 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/OidcUserInfoBuilderTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/OidcUserInfoBuilderTests.java @@ -28,8 +28,15 @@ public class OidcUserInfoBuilderTests { @Test public void buildWhenCalledTwiceThenGeneratesTwoOidcUserInfos() { OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder(); - OidcUserInfo first = userInfoBuilder.claim("TEST_CLAIM_1", "C1").build(); - OidcUserInfo second = userInfoBuilder.claim("TEST_CLAIM_1", "C2").claim("TEST_CLAIM_2", "C3").build(); + // @formatter:off + 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().get("TEST_CLAIM_1")).isEqualTo("C1"); assertThat(second.getClaims()).hasSize(2); @@ -42,17 +49,31 @@ public class OidcUserInfoBuilderTests { OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder(); String generic = 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); - 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); } @Test public void claimsWhenRemovingAClaimThenIsNotPresent() { - OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder().claim("needs", "a claim"); - OidcUserInfo userInfo = userInfoBuilder.subject("sub").claims((claims) -> claims.remove(IdTokenClaimNames.SUB)) + // @formatter:off + OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder() + .claim("needs", "a claim"); + OidcUserInfo userInfo = userInfoBuilder.subject("sub") + .claims((claims) -> claims.remove(IdTokenClaimNames.SUB)) .build(); + // @formatter:on assertThat(userInfo.getSubject()).isNull(); } @@ -61,7 +82,11 @@ public class OidcUserInfoBuilderTests { OidcUserInfo.Builder userInfoBuilder = OidcUserInfo.builder(); String name = new String("name"); 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().get(name)).isSameAs(value); } diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/TestOidcIdTokens.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/TestOidcIdTokens.java index 0bcdcb48c8..ca859473d1 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/TestOidcIdTokens.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/TestOidcIdTokens.java @@ -29,8 +29,15 @@ public final class TestOidcIdTokens { } public static OidcIdToken.Builder idToken() { - return OidcIdToken.withTokenValue("id-token").issuer("https://example.com").subject("subject") - .issuedAt(Instant.now()).expiresAt(Instant.now().plusSeconds(86400)).claim("id", "id"); + // @formatter:off + return OidcIdToken.withTokenValue("id-token") + .issuer("https://example.com") + .subject("subject") + .issuedAt(Instant.now()) + .expiresAt(Instant.now() + .plusSeconds(86400)) + .claim("id", "id"); + // @formatter:on } } diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/user/TestOidcUsers.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/user/TestOidcUsers.java index e7d872a6ad..3bda7ec32d 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/user/TestOidcUsers.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/user/TestOidcUsers.java @@ -44,10 +44,16 @@ public final class TestOidcUsers { private static OidcIdToken idToken() { Instant issuedAt = Instant.now(); 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") .audience(Collections.unmodifiableSet(new LinkedHashSet<>(Collections.singletonList("client")))) - .authorizedParty("client").build(); + .authorizedParty("client") + .build(); + // @formatter:on } private static OidcUserInfo userInfo() { diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/web/reactive/function/OAuth2BodyExtractorsTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/web/reactive/function/OAuth2BodyExtractorsTests.java index 754592cf41..baf82a016b 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/web/reactive/function/OAuth2BodyExtractorsTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/web/reactive/function/OAuth2BodyExtractorsTests.java @@ -90,8 +90,11 @@ public class OAuth2BodyExtractorsTests { response.getHeaders().setContentType(MediaType.APPLICATION_JSON); response.setBody("{"); Mono 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"); + // @formatter:on } @Test @@ -100,8 +103,11 @@ public class OAuth2BodyExtractorsTests { .oauth2AccessTokenResponse(); MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK); Mono 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"); + // @formatter:on } @Test @@ -110,10 +116,16 @@ public class OAuth2BodyExtractorsTests { .oauth2AccessTokenResponse(); MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK); response.getHeaders().setContentType(MediaType.APPLICATION_JSON); + // @formatter:off response.setBody( - "{\n" + " \"access_token\":\"2YotnFZFEjr1zCsicMWpAA\",\n" + " \"token_type\":\"Bearer\",\n" - + " \"expires_in\":3600,\n" + " \"refresh_token\":\"tGzv3JOkF0XG5Qx2TlKWIA\",\n" - + " \"example_parameter\":\"example_value\"\n" + " }"); + "{\n" + + " \"access_token\":\"2YotnFZFEjr1zCsicMWpAA\",\n" + + " \"token_type\":\"Bearer\",\n" + + " \"expires_in\":3600,\n" + + " \"refresh_token\":\"tGzv3JOkF0XG5Qx2TlKWIA\",\n" + + " \"example_parameter\":\"example_value\"\n" + + " }"); + // @formatter:on Instant now = Instant.now(); OAuth2AccessTokenResponse result = extractor.extract(response, this.context).block(); assertThat(result.getAccessToken().getTokenValue()).isEqualTo("2YotnFZFEjr1zCsicMWpAA"); @@ -130,10 +142,17 @@ public class OAuth2BodyExtractorsTests { .oauth2AccessTokenResponse(); MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK); response.getHeaders().setContentType(MediaType.APPLICATION_JSON); + // @formatter:off response.setBody( - "{\n" + " \"access_token\":\"2YotnFZFEjr1zCsicMWpAA\",\n" + " \"token_type\":\"Bearer\",\n" - + " \"expires_in\":3600,\n" + " \"refresh_token\":\"tGzv3JOkF0XG5Qx2TlKWIA\",\n" - + " \"subjson\":{}, \n" + " \"list\":[] \n" + " }"); + "{\n" + + " \"access_token\":\"2YotnFZFEjr1zCsicMWpAA\",\n" + + " \"token_type\":\"Bearer\",\n" + + " \"expires_in\":3600,\n" + + " \"refresh_token\":\"tGzv3JOkF0XG5Qx2TlKWIA\",\n" + + " \"subjson\":{}, \n" + + " \"list\":[] \n" + + " }"); + // @formatter:on Instant now = Instant.now(); OAuth2AccessTokenResponse result = extractor.extract(response, this.context).block(); assertThat(result.getAccessToken().getTokenValue()).isEqualTo("2YotnFZFEjr1zCsicMWpAA");