Search in sources :

Example 1 with Provider

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

the class OAuth2ClientPropertiesRegistrationAdapterTests method getClientRegistrationsWhenUsingDefinedProviderShouldAdapt.

@Test
void getClientRegistrationsWhenUsingDefinedProviderShouldAdapt() {
    OAuth2ClientProperties properties = new OAuth2ClientProperties();
    Provider provider = createProvider();
    provider.setUserInfoAuthenticationMethod("form");
    OAuth2ClientProperties.Registration registration = createRegistration("provider");
    registration.setClientName("clientName");
    properties.getRegistration().put("registration", registration);
    properties.getProvider().put("provider", provider);
    Map<String, ClientRegistration> registrations = OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(properties);
    ClientRegistration adapted = registrations.get("registration");
    ProviderDetails adaptedProvider = adapted.getProviderDetails();
    assertThat(adaptedProvider.getAuthorizationUri()).isEqualTo("https://example.com/auth");
    assertThat(adaptedProvider.getTokenUri()).isEqualTo("https://example.com/token");
    UserInfoEndpoint userInfoEndpoint = adaptedProvider.getUserInfoEndpoint();
    assertThat(userInfoEndpoint.getUri()).isEqualTo("https://example.com/info");
    assertThat(userInfoEndpoint.getAuthenticationMethod()).isEqualTo(org.springframework.security.oauth2.core.AuthenticationMethod.FORM);
    assertThat(userInfoEndpoint.getUserNameAttributeName()).isEqualTo("sub");
    assertThat(adaptedProvider.getJwkSetUri()).isEqualTo("https://example.com/jwk");
    assertThat(adapted.getRegistrationId()).isEqualTo("registration");
    assertThat(adapted.getClientId()).isEqualTo("clientId");
    assertThat(adapted.getClientSecret()).isEqualTo("clientSecret");
    assertThat(adapted.getClientAuthenticationMethod()).isEqualTo(org.springframework.security.oauth2.core.ClientAuthenticationMethod.CLIENT_SECRET_POST);
    assertThat(adapted.getAuthorizationGrantType()).isEqualTo(org.springframework.security.oauth2.core.AuthorizationGrantType.AUTHORIZATION_CODE);
    assertThat(adapted.getRedirectUri()).isEqualTo("https://example.com/redirect");
    assertThat(adapted.getScopes()).containsExactly("user");
    assertThat(adapted.getClientName()).isEqualTo("clientName");
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) 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 2 with Provider

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

the class OAuth2ClientPropertiesRegistrationAdapterTests method getClientRegistrationsWhenUnknownProviderShouldThrowException.

@Test
void getClientRegistrationsWhenUnknownProviderShouldThrowException() {
    OAuth2ClientProperties properties = new OAuth2ClientProperties();
    OAuth2ClientProperties.Registration registration = new OAuth2ClientProperties.Registration();
    registration.setProvider("missing");
    properties.getRegistration().put("registration", registration);
    assertThatIllegalStateException().isThrownBy(() -> OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(properties)).withMessageContaining("Unknown provider ID 'missing'");
}
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) Test(org.junit.jupiter.api.Test)

Example 3 with Provider

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

the class OAuth2ClientPropertiesRegistrationAdapterTests method createProvider.

private Provider createProvider() {
    Provider provider = new Provider();
    provider.setAuthorizationUri("https://example.com/auth");
    provider.setTokenUri("https://example.com/token");
    provider.setUserInfoUri("https://example.com/info");
    provider.setUserNameAttribute("sub");
    provider.setJwkSetUri("https://example.com/jwk");
    return provider;
}
Also used : Provider(org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Provider)

Example 4 with Provider

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

the class OAuth2ClientPropertiesRegistrationAdapter method getBuilderFromIssuerIfPossible.

private static Builder getBuilderFromIssuerIfPossible(String registrationId, String configuredProviderId, Map<String, Provider> providers) {
    String providerId = (configuredProviderId != null) ? configuredProviderId : registrationId;
    if (providers.containsKey(providerId)) {
        Provider provider = providers.get(providerId);
        String issuer = provider.getIssuerUri();
        if (issuer != null) {
            Builder builder = ClientRegistrations.fromIssuerLocation(issuer).registrationId(registrationId);
            return getBuilder(builder, provider);
        }
    }
    return null;
}
Also used : Builder(org.springframework.security.oauth2.client.registration.ClientRegistration.Builder) Provider(org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Provider) CommonOAuth2Provider(org.springframework.security.config.oauth2.client.CommonOAuth2Provider)

Example 5 with Provider

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

the class OAuth2ClientPropertiesRegistrationAdapterTests method getClientRegistrationsWhenProviderNotSpecifiedAndUnknownProviderShouldThrowException.

@Test
void getClientRegistrationsWhenProviderNotSpecifiedAndUnknownProviderShouldThrowException() {
    OAuth2ClientProperties properties = new OAuth2ClientProperties();
    OAuth2ClientProperties.Registration registration = new OAuth2ClientProperties.Registration();
    properties.getRegistration().put("missing", registration);
    assertThatIllegalStateException().isThrownBy(() -> OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(properties)).withMessageContaining("Provider ID must be specified for client registration 'missing'");
}
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) Test(org.junit.jupiter.api.Test)

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