Java Code Examples for org.springframework.security.oauth2.client.registration.ClientRegistration#getRegistrationId()
The following examples show how to use
org.springframework.security.oauth2.client.registration.ClientRegistration#getRegistrationId() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DemoApplicationTests.java From keycloak-springsecurity5-sample with GNU General Public License v3.0 | 5 votes |
@Test public void requestAuthorizeGitHubClientWhenLinkClickedThenStatusRedirectForAuthorization() throws Exception { HtmlPage page = this.webClient.getPage("/"); ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId("github"); HtmlAnchor clientAnchorElement = this.getClientAnchorElement(page, clientRegistration); assertThat(clientAnchorElement).isNotNull(); WebResponse response = this.followLinkDisableRedirects(clientAnchorElement); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.MOVED_PERMANENTLY.value()); String authorizeRedirectUri = response.getResponseHeaderValue("Location"); assertThat(authorizeRedirectUri).isNotNull(); UriComponents uriComponents = UriComponentsBuilder.fromUri(URI.create(authorizeRedirectUri)).build(); String requestUri = uriComponents.getScheme() + "://" + uriComponents.getHost() + uriComponents.getPath(); assertThat(requestUri).isEqualTo(clientRegistration.getProviderDetails().getAuthorizationUri()); Map<String, String> params = uriComponents.getQueryParams().toSingleValueMap(); assertThat(params.get(OAuth2ParameterNames.RESPONSE_TYPE)).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(" "))); assertThat(params.get(OAuth2ParameterNames.STATE)).isNotNull(); }
Example 2
Source File: DemoApplicationTests.java From keycloak-springsecurity5-sample with GNU General Public License v3.0 | 5 votes |
@Test public void requestAuthorizationCodeGrantWhenNoMatchingAuthorizationRequestThenDisplayLoginPageWithError() throws Exception { HtmlPage page = this.webClient.getPage("/"); URL loginPageUrl = page.getBaseURL(); URL loginErrorPageUrl = new URL(loginPageUrl.toString() + "?error"); ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId("google"); String code = "auth-code"; String state = "state"; String redirectUri = AUTHORIZE_BASE_URL + "/" + clientRegistration.getRegistrationId(); String authorizationResponseUri = UriComponentsBuilder.fromHttpUrl(redirectUri) .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 this.webClient.getCookieManager().clearCookies(); page = this.webClient.getPage(new URL(authorizationResponseUri)); assertThat(page.getBaseURL()).isEqualTo(loginErrorPageUrl); HtmlElement errorElement = page.getBody().getFirstByXPath("p"); assertThat(errorElement).isNotNull(); assertThat(errorElement.asText()).contains("authorization_request_not_found"); }
Example 3
Source File: DemoApplicationTests.java From keycloak-springsecurity5-sample with GNU General Public License v3.0 | 5 votes |
@Test public void requestAuthorizationCodeGrantWhenInvalidStateParamThenDisplayLoginPageWithError() throws Exception { HtmlPage page = this.webClient.getPage("/"); URL loginPageUrl = page.getBaseURL(); URL loginErrorPageUrl = new URL(loginPageUrl.toString() + "?error"); ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId("google"); HtmlAnchor clientAnchorElement = this.getClientAnchorElement(page, clientRegistration); assertThat(clientAnchorElement).isNotNull(); this.followLinkDisableRedirects(clientAnchorElement); String code = "auth-code"; String state = "invalid-state"; String redirectUri = AUTHORIZE_BASE_URL + "/" + clientRegistration.getRegistrationId(); String authorizationResponseUri = UriComponentsBuilder.fromHttpUrl(redirectUri) .queryParam(OAuth2ParameterNames.CODE, code) .queryParam(OAuth2ParameterNames.STATE, state) .build().encode().toUriString(); page = this.webClient.getPage(new URL(authorizationResponseUri)); assertThat(page.getBaseURL()).isEqualTo(loginErrorPageUrl); HtmlElement errorElement = page.getBody().getFirstByXPath("p"); assertThat(errorElement).isNotNull(); assertThat(errorElement.asText()).contains("invalid_state_parameter"); }
Example 4
Source File: DemoApplicationTests.java From keycloak-springsecurity5-sample with GNU General Public License v3.0 | 5 votes |
private void assertLoginPage(HtmlPage page) throws Exception { assertThat(page.getTitleText()).isEqualTo("Login Page"); int expectedClients = 5; List<HtmlAnchor> clientAnchorElements = page.getAnchors(); assertThat(clientAnchorElements.size()).isEqualTo(expectedClients); ClientRegistration googleClientRegistration = this.clientRegistrationRepository.findByRegistrationId("google"); ClientRegistration githubClientRegistration = this.clientRegistrationRepository.findByRegistrationId("github"); ClientRegistration facebookClientRegistration = this.clientRegistrationRepository.findByRegistrationId("facebook"); ClientRegistration oktaClientRegistration = this.clientRegistrationRepository.findByRegistrationId("okta"); ClientRegistration keycloakClientRegistration = this.clientRegistrationRepository.findByRegistrationId("keycloak"); String baseAuthorizeUri = AUTHORIZATION_BASE_URI + "/"; String googleClientAuthorizeUri = baseAuthorizeUri + googleClientRegistration.getRegistrationId(); String githubClientAuthorizeUri = baseAuthorizeUri + githubClientRegistration.getRegistrationId(); String facebookClientAuthorizeUri = baseAuthorizeUri + facebookClientRegistration.getRegistrationId(); String oktaClientAuthorizeUri = baseAuthorizeUri + oktaClientRegistration.getRegistrationId(); String keycloakClientAuthorizeUri = baseAuthorizeUri + keycloakClientRegistration.getRegistrationId(); for (int i=0; i<expectedClients; i++) { assertThat(clientAnchorElements.get(i).getAttribute("href")).isIn( googleClientAuthorizeUri, githubClientAuthorizeUri, facebookClientAuthorizeUri, oktaClientAuthorizeUri, keycloakClientAuthorizeUri); assertThat(clientAnchorElements.get(i).asText()).isIn( googleClientRegistration.getClientName(), githubClientRegistration.getClientName(), facebookClientRegistration.getClientName(), oktaClientRegistration.getClientName(), keycloakClientRegistration.getClientName()); } }
Example 5
Source File: ResettableOAuth2AuthorizedClientService.java From molgenis with GNU Lesser General Public License v3.0 | 4 votes |
/** Copy of {@link InMemoryOAuth2AuthorizedClientService} getIdentifier */ private String getIdentifier(ClientRegistration registration, String principalName) { String identifier = "[" + registration.getRegistrationId() + "][" + principalName + "]"; return Base64.getEncoder().encodeToString(identifier.getBytes(UTF_8)); }