-
Notifications
You must be signed in to change notification settings - Fork 738
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66e0ae2
commit fadbeb3
Showing
35 changed files
with
175 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,8 +36,8 @@ public class HelloSecurityExplicitITests { | |
|
||
@Test | ||
void login() { | ||
CustomUser result = this.rest.withBasicAuth("[email protected]", "password").getForObject("/user", | ||
CustomUser.class); | ||
CustomUser result = this.rest.withBasicAuth("[email protected]", "password") | ||
.getForObject("/user", CustomUser.class); | ||
assertThat(result.getEmail()).isEqualTo("[email protected]"); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,12 +143,12 @@ void requestAuthorizeGitHubClientWhenLinkClickedThenStatusRedirectForAuthorizati | |
Map<String, String> params = uriComponents.getQueryParams().toSingleValueMap(); | ||
|
||
assertThat(params.get(OAuth2ParameterNames.RESPONSE_TYPE)) | ||
.isEqualTo(OAuth2AuthorizationResponseType.CODE.getValue()); | ||
.isEqualTo(OAuth2AuthorizationResponseType.CODE.getValue()); | ||
assertThat(params.get(OAuth2ParameterNames.CLIENT_ID)).isEqualTo(clientRegistration.getClientId()); | ||
String redirectUri = AUTHORIZE_BASE_URL + "/" + clientRegistration.getRegistrationId(); | ||
assertThat(URLDecoder.decode(params.get(OAuth2ParameterNames.REDIRECT_URI), "UTF-8")).isEqualTo(redirectUri); | ||
assertThat(URLDecoder.decode(params.get(OAuth2ParameterNames.SCOPE), "UTF-8")) | ||
.isEqualTo(clientRegistration.getScopes().stream().collect(Collectors.joining(" "))); | ||
.isEqualTo(clientRegistration.getScopes().stream().collect(Collectors.joining(" "))); | ||
assertThat(params.get(OAuth2ParameterNames.STATE)).isNotNull(); | ||
} | ||
|
||
|
@@ -185,16 +185,20 @@ void requestAuthorizationCodeGrantWhenValidAuthorizationResponseThenDisplayIndex | |
WebResponse response = this.followLinkDisableRedirects(clientAnchorElement); | ||
|
||
UriComponents authorizeRequestUriComponents = UriComponentsBuilder | ||
.fromUri(URI.create(response.getResponseHeaderValue("Location"))).build(); | ||
.fromUri(URI.create(response.getResponseHeaderValue("Location"))) | ||
.build(); | ||
|
||
Map<String, String> params = authorizeRequestUriComponents.getQueryParams().toSingleValueMap(); | ||
String code = "auth-code"; | ||
String state = URLDecoder.decode(params.get(OAuth2ParameterNames.STATE), "UTF-8"); | ||
String redirectUri = URLDecoder.decode(params.get(OAuth2ParameterNames.REDIRECT_URI), "UTF-8"); | ||
|
||
String authorizationResponseUri = UriComponentsBuilder.fromHttpUrl(redirectUri) | ||
.queryParam(OAuth2ParameterNames.CODE, code).queryParam(OAuth2ParameterNames.STATE, state).build() | ||
.encode().toUriString(); | ||
.queryParam(OAuth2ParameterNames.CODE, code) | ||
.queryParam(OAuth2ParameterNames.STATE, state) | ||
.build() | ||
.encode() | ||
.toUriString(); | ||
|
||
page = this.webClient.getPage(new URL(authorizationResponseUri)); | ||
this.assertIndexPage(page); | ||
|
@@ -214,8 +218,11 @@ void requestAuthorizationCodeGrantWhenNoMatchingAuthorizationRequestThenDisplayL | |
String redirectUri = AUTHORIZE_BASE_URL + "/" + clientRegistration.getRegistrationId(); | ||
|
||
String authorizationResponseUri = UriComponentsBuilder.fromHttpUrl(redirectUri) | ||
.queryParam(OAuth2ParameterNames.CODE, code).queryParam(OAuth2ParameterNames.STATE, state).build() | ||
.encode().toUriString(); | ||
.queryParam(OAuth2ParameterNames.CODE, code) | ||
.queryParam(OAuth2ParameterNames.STATE, state) | ||
.build() | ||
.encode() | ||
.toUriString(); | ||
|
||
// Clear session cookie will ensure the 'session-saved' | ||
// Authorization Request (from previous request) is not found | ||
|
@@ -246,8 +253,11 @@ void requestAuthorizationCodeGrantWhenInvalidStateParamThenDisplayLoginPageWithE | |
String redirectUri = AUTHORIZE_BASE_URL + "/" + clientRegistration.getRegistrationId(); | ||
|
||
String authorizationResponseUri = UriComponentsBuilder.fromHttpUrl(redirectUri) | ||
.queryParam(OAuth2ParameterNames.CODE, code).queryParam(OAuth2ParameterNames.STATE, state).build() | ||
.encode().toUriString(); | ||
.queryParam(OAuth2ParameterNames.CODE, code) | ||
.queryParam(OAuth2ParameterNames.STATE, state) | ||
.build() | ||
.encode() | ||
.toUriString(); | ||
|
||
page = this.webClient.getPage(new URL(authorizationResponseUri)); | ||
assertThat(page.getBaseURL()).isEqualTo(loginErrorPageUrl); | ||
|
@@ -261,8 +271,9 @@ void requestAuthorizationCodeGrantWhenInvalidStateParamThenDisplayLoginPageWithE | |
void requestWhenMockOAuth2LoginThenIndex() throws Exception { | ||
ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId("github"); | ||
this.mvc.perform(get("/").with(oauth2Login().clientRegistration(clientRegistration))) | ||
.andExpect(model().attribute("userName", "user")).andExpect(model().attribute("clientName", "GitHub")) | ||
.andExpect(model().attribute("userAttributes", Collections.singletonMap("sub", "user"))); | ||
.andExpect(model().attribute("userName", "user")) | ||
.andExpect(model().attribute("clientName", "GitHub")) | ||
.andExpect(model().attribute("userAttributes", Collections.singletonMap("sub", "user"))); | ||
} | ||
|
||
private void assertLoginPage(HtmlPage page) { | ||
|
@@ -276,10 +287,10 @@ private void assertLoginPage(HtmlPage page) { | |
ClientRegistration googleClientRegistration = this.clientRegistrationRepository.findByRegistrationId("google"); | ||
ClientRegistration githubClientRegistration = this.clientRegistrationRepository.findByRegistrationId("github"); | ||
ClientRegistration facebookClientRegistration = this.clientRegistrationRepository | ||
.findByRegistrationId("facebook"); | ||
.findByRegistrationId("facebook"); | ||
ClientRegistration oktaClientRegistration = this.clientRegistrationRepository.findByRegistrationId("okta"); | ||
ClientRegistration springClientRegistration = this.clientRegistrationRepository | ||
.findByRegistrationId("login-client"); | ||
.findByRegistrationId("login-client"); | ||
|
||
String baseAuthorizeUri = AUTHORIZATION_BASE_URI + "/"; | ||
String googleClientAuthorizeUri = baseAuthorizeUri + googleClientRegistration.getRegistrationId(); | ||
|
@@ -304,12 +315,14 @@ private void assertIndexPage(HtmlPage page) { | |
DomNodeList<HtmlElement> divElements = page.getBody().getElementsByTagName("div"); | ||
assertThat(divElements.get(1).asNormalizedText()).contains("User: [email protected]"); | ||
assertThat(divElements.get(4).asNormalizedText()) | ||
.contains("You are successfully logged in [email protected]"); | ||
.contains("You are successfully logged in [email protected]"); | ||
} | ||
|
||
private HtmlAnchor getClientAnchorElement(HtmlPage page, ClientRegistration clientRegistration) { | ||
Optional<HtmlAnchor> clientAnchorElement = page.getAnchors().stream() | ||
.filter((e) -> e.asNormalizedText().equals(clientRegistration.getClientName())).findFirst(); | ||
Optional<HtmlAnchor> clientAnchorElement = page.getAnchors() | ||
.stream() | ||
.filter((e) -> e.asNormalizedText().equals(clientRegistration.getClientName())) | ||
.findFirst(); | ||
|
||
return (clientAnchorElement.orElse(null)); | ||
} | ||
|
@@ -350,7 +363,9 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti | |
|
||
private OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> mockAccessTokenResponseClient() { | ||
OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234") | ||
.tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(60 * 1000).build(); | ||
.tokenType(OAuth2AccessToken.TokenType.BEARER) | ||
.expiresIn(60 * 1000) | ||
.build(); | ||
|
||
OAuth2AccessTokenResponseClient tokenResponseClient = mock(OAuth2AccessTokenResponseClient.class); | ||
when(tokenResponseClient.getTokenResponse(any())).thenReturn(accessTokenResponse); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.