diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientProperties.java index 5467531b9e7..aebf1c6ddb9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientProperties.java @@ -64,9 +64,6 @@ public class OAuth2ClientProperties { if (!StringUtils.hasText(registration.getClientSecret())) { throw new IllegalStateException("Client secret must not be empty."); } - if (!StringUtils.hasText(registration.getProvider())) { - throw new IllegalStateException("Provider must not be empty."); - } } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesRegistrationAdapter.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesRegistrationAdapter.java index e991d254eed..6856672a4fd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesRegistrationAdapter.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesRegistrationAdapter.java @@ -68,11 +68,12 @@ final class OAuth2ClientPropertiesRegistrationAdapter { return builder.build(); } - private static Builder getBuilder(String registrationId, String providerId, + private static Builder getBuilder(String registrationId, String configuredProviderId, Map providers) { + String providerId = (configuredProviderId == null ? registrationId : configuredProviderId); CommonOAuth2Provider provider = getCommonProvider(providerId); if (provider == null && !providers.containsKey(providerId)) { - throw new IllegalStateException("Unknown provider ID '" + providerId + "'"); + throw new IllegalStateException(getErrorMessage(configuredProviderId, registrationId)); } Builder builder = (provider != null ? provider.getBuilder(registrationId) : new Builder(registrationId)); @@ -82,6 +83,11 @@ final class OAuth2ClientPropertiesRegistrationAdapter { return builder; } + private static String getErrorMessage(String configuredProviderId, String registrationId) { + return (configuredProviderId == null ? "Provider ID must be specified for client registration '" + registrationId + "'" : + "Unknown provider ID '" + configuredProviderId + "'"); + } + private static Builder getBuilder(Builder builder, Provider provider) { copyIfNotNull(provider::getAuthorizationUri, builder::authorizationUri); copyIfNotNull(provider::getTokenUri, builder::tokenUri); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesRegistrationAdapterTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesRegistrationAdapterTests.java index 3f03339c3f1..3874231f712 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesRegistrationAdapterTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesRegistrationAdapterTests.java @@ -167,4 +167,49 @@ public class OAuth2ClientPropertiesRegistrationAdapterTests { OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(properties); } + @Test + public void getClientRegistrationsWhenProviderNotSpecifiedShouldUseRegistrationId() + throws Exception { + OAuth2ClientProperties properties = new OAuth2ClientProperties(); + Registration registration = new Registration(); + registration.setClientId("clientId"); + registration.setClientSecret("clientSecret"); + properties.getRegistration().put("google", registration); + Map registrations = OAuth2ClientPropertiesRegistrationAdapter + .getClientRegistrations(properties); + ClientRegistration adapted = registrations.get("google"); + ProviderDetails adaptedProvider = adapted.getProviderDetails(); + assertThat(adaptedProvider.getAuthorizationUri()) + .isEqualTo("https://accounts.google.com/o/oauth2/v2/auth"); + assertThat(adaptedProvider.getTokenUri()) + .isEqualTo("https://www.googleapis.com/oauth2/v4/token"); + assertThat(adaptedProvider.getUserInfoEndpoint().getUri()) + .isEqualTo("https://www.googleapis.com/oauth2/v3/userinfo"); + assertThat(adaptedProvider.getJwkSetUri()) + .isEqualTo("https://www.googleapis.com/oauth2/v3/certs"); + assertThat(adapted.getRegistrationId()).isEqualTo("google"); + assertThat(adapted.getClientId()).isEqualTo("clientId"); + assertThat(adapted.getClientSecret()).isEqualTo("clientSecret"); + assertThat(adapted.getClientAuthenticationMethod()).isEqualTo( + org.springframework.security.oauth2.core.ClientAuthenticationMethod.BASIC); + assertThat(adapted.getAuthorizationGrantType()).isEqualTo( + org.springframework.security.oauth2.core.AuthorizationGrantType.AUTHORIZATION_CODE); + assertThat(adapted.getRedirectUri()).isEqualTo( + "{scheme}://{serverName}:{serverPort}{contextPath}/oauth2/authorize/code/{registrationId}"); + assertThat(adapted.getScope()).containsExactly("openid", "profile", "email", + "address", "phone"); + assertThat(adapted.getClientName()).isEqualTo("Google"); + } + + @Test + public void getClientRegistrationsWhenProviderNotSpecifiedAndUnknownProviderShouldThrowException() + throws Exception { + OAuth2ClientProperties properties = new OAuth2ClientProperties(); + Registration registration = new Registration(); + properties.getRegistration().put("missing", registration); + this.thrown.expect(IllegalStateException.class); + this.thrown.expectMessage("Provider ID must be specified for client registration 'missing'"); + OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(properties); + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesTests.java index 601491c047c..166fea47b6f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesTests.java @@ -54,15 +54,4 @@ public class OAuth2ClientPropertiesTests { this.properties.validate(); } - @Test - public void providerAbsentThrowsException() throws Exception { - OAuth2ClientProperties.Registration registration = new OAuth2ClientProperties.Registration(); - registration.setClientId("foo"); - registration.setClientSecret("secret"); - this.properties.getRegistration().put("foo", registration); - this.thrown.expect(IllegalStateException.class); - this.thrown.expectMessage("Provider must not be empty."); - this.properties.validate(); - } - }