Search in sources :

Example 46 with ClientRegistration

use of org.springframework.security.oauth2.client.registration.ClientRegistration in project spring-security by spring-projects.

the class CommonOAuth2ProviderTests method getBuilderWhenGoogleShouldHaveGoogleSettings.

@Test
public void getBuilderWhenGoogleShouldHaveGoogleSettings() {
    ClientRegistration registration = build(CommonOAuth2Provider.GOOGLE);
    ProviderDetails providerDetails = registration.getProviderDetails();
    assertThat(providerDetails.getAuthorizationUri()).isEqualTo("https://accounts.google.com/o/oauth2/v2/auth");
    assertThat(providerDetails.getTokenUri()).isEqualTo("https://www.googleapis.com/oauth2/v4/token");
    assertThat(providerDetails.getUserInfoEndpoint().getUri()).isEqualTo("https://www.googleapis.com/oauth2/v3/userinfo");
    assertThat(providerDetails.getUserInfoEndpoint().getUserNameAttributeName()).isEqualTo(IdTokenClaimNames.SUB);
    assertThat(providerDetails.getJwkSetUri()).isEqualTo("https://www.googleapis.com/oauth2/v3/certs");
    assertThat(providerDetails.getIssuerUri()).isEqualTo("https://accounts.google.com");
    assertThat(registration.getClientAuthenticationMethod()).isEqualTo(ClientAuthenticationMethod.CLIENT_SECRET_BASIC);
    assertThat(registration.getAuthorizationGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
    assertThat(registration.getRedirectUri()).isEqualTo(DEFAULT_REDIRECT_URL);
    assertThat(registration.getScopes()).containsOnly("openid", "profile", "email");
    assertThat(registration.getClientName()).isEqualTo("Google");
    assertThat(registration.getRegistrationId()).isEqualTo("123");
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) ProviderDetails(org.springframework.security.oauth2.client.registration.ClientRegistration.ProviderDetails) Test(org.junit.jupiter.api.Test)

Example 47 with ClientRegistration

use of org.springframework.security.oauth2.client.registration.ClientRegistration in project spring-security by spring-projects.

the class ClientRegistrationsBeanDefinitionParserTests method parseWhenClientPlaceholdersThenResolvePlaceholders.

@Test
public void parseWhenClientPlaceholdersThenResolvePlaceholders() {
    System.setProperty("oauth2.client.id", "github-client-id");
    System.setProperty("oauth2.client.secret", "github-client-secret");
    this.spring.configLocations(xml("ClientPlaceholders")).autowire();
    assertThat(this.clientRegistrationRepository).isInstanceOf(InMemoryClientRegistrationRepository.class);
    ClientRegistration githubRegistration = this.clientRegistrationRepository.findByRegistrationId("github");
    assertThat(githubRegistration.getClientId()).isEqualTo("github-client-id");
    assertThat(githubRegistration.getClientSecret()).isEqualTo("github-client-secret");
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) Test(org.junit.jupiter.api.Test)

Example 48 with ClientRegistration

use of org.springframework.security.oauth2.client.registration.ClientRegistration in project spring-security by spring-projects.

the class DefaultOAuth2AuthorizationRequestResolverTests method resolveWhenAuthorizationRequestCustomizerOverridesParameterThenQueryIncludesParameter.

@Test
public void resolveWhenAuthorizationRequestCustomizerOverridesParameterThenQueryIncludesParameter() {
    ClientRegistration clientRegistration = this.oidcRegistration;
    String requestUri = this.authorizationRequestBaseUri + "/" + clientRegistration.getRegistrationId();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri);
    request.setServletPath(requestUri);
    this.resolver.setAuthorizationRequestCustomizer((customizer) -> customizer.parameters((params) -> {
        params.put("appid", params.get("client_id"));
        params.remove("client_id");
    }));
    OAuth2AuthorizationRequest authorizationRequest = this.resolver.resolve(request);
    assertThat(authorizationRequest.getAuthorizationRequestUri()).matches("https://example.com/login/oauth/authorize\\?" + "response_type=code&" + "scope=openid&state=.{15,}&" + "redirect_uri=http://localhost/login/oauth2/code/oidc-registration-id&" + "nonce=([a-zA-Z0-9\\-\\.\\_\\~]){43}&" + "appid=client-id");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) OAuth2ParameterNames(org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames) ArgumentMatchers(org.mockito.ArgumentMatchers) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) PkceParameterNames(org.springframework.security.oauth2.core.endpoint.PkceParameterNames) OAuth2AuthorizationResponseType(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponseType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) OidcParameterNames(org.springframework.security.oauth2.core.oidc.endpoint.OidcParameterNames) IOException(java.io.IOException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) Assertions.entry(org.assertj.core.api.Assertions.entry) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ClientAuthenticationMethod(org.springframework.security.oauth2.core.ClientAuthenticationMethod) OidcScopes(org.springframework.security.oauth2.core.oidc.OidcScopes) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) InMemoryClientRegistrationRepository(org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository) AuthorizationGrantType(org.springframework.security.oauth2.core.AuthorizationGrantType) ClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ClientRegistrationRepository) TestClientRegistrations(org.springframework.security.oauth2.client.registration.TestClientRegistrations) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) Test(org.junit.jupiter.api.Test)

Example 49 with ClientRegistration

use of org.springframework.security.oauth2.client.registration.ClientRegistration in project spring-security by spring-projects.

the class DefaultOAuth2AuthorizationRequestResolverTests method resolveWhenAuthorizationRequestHasNoPortThenExpandedRedirectUriWithExtraVarsExcludesPort.

@Test
public void resolveWhenAuthorizationRequestHasNoPortThenExpandedRedirectUriWithExtraVarsExcludesPort() {
    ClientRegistration clientRegistration = this.fineRedirectUriTemplateRegistration;
    String requestUri = this.authorizationRequestBaseUri + "/" + clientRegistration.getRegistrationId();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri);
    request.setScheme("https");
    request.setServerPort(-1);
    request.setServletPath(requestUri);
    OAuth2AuthorizationRequest authorizationRequest = this.resolver.resolve(request);
    assertThat(authorizationRequest.getRedirectUri()).isNotEqualTo(clientRegistration.getRedirectUri());
    assertThat(authorizationRequest.getRedirectUri()).isEqualTo("https://localhost/login/oauth2/code/" + clientRegistration.getRegistrationId());
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) Test(org.junit.jupiter.api.Test)

Example 50 with ClientRegistration

use of org.springframework.security.oauth2.client.registration.ClientRegistration in project spring-security by spring-projects.

the class DefaultOAuth2AuthorizationRequestResolverTests method resolveWhenAuthenticationRequestWithValidOidcClientThenResolves.

@Test
public void resolveWhenAuthenticationRequestWithValidOidcClientThenResolves() {
    ClientRegistration clientRegistration = this.oidcRegistration;
    String requestUri = this.authorizationRequestBaseUri + "/" + clientRegistration.getRegistrationId();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri);
    request.setServletPath(requestUri);
    OAuth2AuthorizationRequest authorizationRequest = this.resolver.resolve(request);
    assertThat(authorizationRequest).isNotNull();
    assertThat(authorizationRequest.getAuthorizationUri()).isEqualTo(clientRegistration.getProviderDetails().getAuthorizationUri());
    assertThat(authorizationRequest.getGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
    assertThat(authorizationRequest.getResponseType()).isEqualTo(OAuth2AuthorizationResponseType.CODE);
    assertThat(authorizationRequest.getClientId()).isEqualTo(clientRegistration.getClientId());
    assertThat(authorizationRequest.getRedirectUri()).isEqualTo("http://localhost/login/oauth2/code/" + clientRegistration.getRegistrationId());
    assertThat(authorizationRequest.getScopes()).isEqualTo(clientRegistration.getScopes());
    assertThat(authorizationRequest.getState()).isNotNull();
    assertThat(authorizationRequest.getAdditionalParameters()).doesNotContainKey(OAuth2ParameterNames.REGISTRATION_ID);
    assertThat(authorizationRequest.getAdditionalParameters()).containsKey(OidcParameterNames.NONCE);
    assertThat(authorizationRequest.getAttributes()).contains(entry(OAuth2ParameterNames.REGISTRATION_ID, clientRegistration.getRegistrationId()));
    assertThat(authorizationRequest.getAttributes()).containsKey(OidcParameterNames.NONCE);
    assertThat((String) authorizationRequest.getAttribute(OidcParameterNames.NONCE)).matches("^([a-zA-Z0-9\\-\\.\\_\\~]){128}$");
    assertThat(authorizationRequest.getAuthorizationRequestUri()).matches("https://example.com/login/oauth/authorize\\?" + "response_type=code&client_id=client-id&" + "scope=openid&state=.{15,}&" + "redirect_uri=http://localhost/login/oauth2/code/oidc-registration-id&" + "nonce=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) Test(org.junit.jupiter.api.Test)

Aggregations

ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)259 Test (org.junit.jupiter.api.Test)214 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)56 OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)52 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)44 HttpHeaders (org.springframework.http.HttpHeaders)42 OAuth2AccessToken (org.springframework.security.oauth2.core.OAuth2AccessToken)36 OAuth2AuthorizationException (org.springframework.security.oauth2.core.OAuth2AuthorizationException)32 Instant (java.time.Instant)28 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)27 BeforeEach (org.junit.jupiter.api.BeforeEach)27 TestClientRegistrations (org.springframework.security.oauth2.client.registration.TestClientRegistrations)27 HashMap (java.util.HashMap)26 MockResponse (okhttp3.mockwebserver.MockResponse)26 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)26 OAuth2AuthorizedClient (org.springframework.security.oauth2.client.OAuth2AuthorizedClient)26 MultiValueMap (org.springframework.util.MultiValueMap)26 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)25 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)25 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)24