Search in sources :

Example 6 with Provider

use of org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Provider in project spring-boot by spring-projects.

the class OAuth2ClientPropertiesRegistrationAdapterTests method createRegistration.

private OAuth2ClientProperties.Registration createRegistration(String provider) {
    OAuth2ClientProperties.Registration registration = new OAuth2ClientProperties.Registration();
    registration.setProvider(provider);
    registration.setClientId("clientId");
    registration.setClientSecret("clientSecret");
    registration.setClientAuthenticationMethod("client_secret_post");
    registration.setRedirectUri("https://example.com/redirect");
    registration.setScope(Collections.singleton("user"));
    registration.setAuthorizationGrantType("authorization_code");
    return registration;
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) Registration(org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Registration) Registration(org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Registration)

Example 7 with Provider

use of org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Provider in project spring-boot by spring-projects.

the class OAuth2ClientPropertiesRegistrationAdapterTests method oidcProviderConfigurationWithCustomConfigurationOverridesProviderDefaults.

@Test
void oidcProviderConfigurationWithCustomConfigurationOverridesProviderDefaults() throws Exception {
    this.server = new MockWebServer();
    this.server.start();
    String issuer = this.server.url("").toString();
    setupMockResponse(issuer);
    OAuth2ClientProperties.Registration registration = createRegistration("okta-oidc");
    Provider provider = createProvider();
    provider.setIssuerUri(issuer);
    OAuth2ClientProperties properties = new OAuth2ClientProperties();
    properties.getProvider().put("okta-oidc", provider);
    properties.getRegistration().put("okta", registration);
    Map<String, ClientRegistration> registrations = OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(properties);
    ClientRegistration adapted = registrations.get("okta");
    ProviderDetails providerDetails = adapted.getProviderDetails();
    assertThat(adapted.getClientAuthenticationMethod()).isEqualTo(ClientAuthenticationMethod.CLIENT_SECRET_POST);
    assertThat(adapted.getAuthorizationGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
    assertThat(adapted.getRegistrationId()).isEqualTo("okta");
    assertThat(adapted.getClientName()).isEqualTo(issuer);
    assertThat(adapted.getScopes()).containsOnly("user");
    assertThat(adapted.getRedirectUri()).isEqualTo("https://example.com/redirect");
    assertThat(providerDetails.getAuthorizationUri()).isEqualTo("https://example.com/auth");
    assertThat(providerDetails.getTokenUri()).isEqualTo("https://example.com/token");
    assertThat(providerDetails.getJwkSetUri()).isEqualTo("https://example.com/jwk");
    UserInfoEndpoint userInfoEndpoint = providerDetails.getUserInfoEndpoint();
    assertThat(userInfoEndpoint.getUri()).isEqualTo("https://example.com/info");
    assertThat(userInfoEndpoint.getUserNameAttributeName()).isEqualTo("sub");
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) MockWebServer(okhttp3.mockwebserver.MockWebServer) Registration(org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Registration) UserInfoEndpoint(org.springframework.security.oauth2.client.registration.ClientRegistration.ProviderDetails.UserInfoEndpoint) ProviderDetails(org.springframework.security.oauth2.client.registration.ClientRegistration.ProviderDetails) Provider(org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Provider) Test(org.junit.jupiter.api.Test)

Example 8 with Provider

use of org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Provider in project spring-boot by spring-projects.

the class OAuth2ClientPropertiesRegistrationAdapterTests method testIssuerConfiguration.

private void testIssuerConfiguration(OAuth2ClientProperties.Registration registration, String providerId, int errorResponseCount, int numberOfRequests) throws Exception {
    this.server = new MockWebServer();
    this.server.start();
    String issuer = this.server.url("").toString();
    setupMockResponsesWithErrors(issuer, errorResponseCount);
    OAuth2ClientProperties properties = new OAuth2ClientProperties();
    Provider provider = new Provider();
    provider.setIssuerUri(issuer);
    properties.getProvider().put(providerId, provider);
    properties.getRegistration().put("okta", registration);
    Map<String, ClientRegistration> registrations = OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(properties);
    ClientRegistration adapted = registrations.get("okta");
    ProviderDetails providerDetails = adapted.getProviderDetails();
    assertThat(adapted.getClientAuthenticationMethod()).isEqualTo(ClientAuthenticationMethod.CLIENT_SECRET_BASIC);
    assertThat(adapted.getAuthorizationGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
    assertThat(adapted.getRegistrationId()).isEqualTo("okta");
    assertThat(adapted.getClientName()).isEqualTo(issuer);
    assertThat(adapted.getScopes()).isNull();
    assertThat(providerDetails.getAuthorizationUri()).isEqualTo("https://example.com/o/oauth2/v2/auth");
    assertThat(providerDetails.getTokenUri()).isEqualTo("https://example.com/oauth2/v4/token");
    assertThat(providerDetails.getJwkSetUri()).isEqualTo("https://example.com/oauth2/v3/certs");
    UserInfoEndpoint userInfoEndpoint = providerDetails.getUserInfoEndpoint();
    assertThat(userInfoEndpoint.getUri()).isEqualTo("https://example.com/oauth2/v3/userinfo");
    assertThat(userInfoEndpoint.getAuthenticationMethod()).isEqualTo(org.springframework.security.oauth2.core.AuthenticationMethod.HEADER);
    assertThat(this.server.getRequestCount()).isEqualTo(numberOfRequests);
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) MockWebServer(okhttp3.mockwebserver.MockWebServer) UserInfoEndpoint(org.springframework.security.oauth2.client.registration.ClientRegistration.ProviderDetails.UserInfoEndpoint) ProviderDetails(org.springframework.security.oauth2.client.registration.ClientRegistration.ProviderDetails) Provider(org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Provider)

Aggregations

ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)6 Provider (org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Provider)5 Registration (org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Registration)5 Test (org.junit.jupiter.api.Test)4 ProviderDetails (org.springframework.security.oauth2.client.registration.ClientRegistration.ProviderDetails)3 UserInfoEndpoint (org.springframework.security.oauth2.client.registration.ClientRegistration.ProviderDetails.UserInfoEndpoint)3 MockWebServer (okhttp3.mockwebserver.MockWebServer)2 CommonOAuth2Provider (org.springframework.security.config.oauth2.client.CommonOAuth2Provider)1 Builder (org.springframework.security.oauth2.client.registration.ClientRegistration.Builder)1