|
|
|
@ -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()); |
|
|
|
|