Browse Source

Default provider to registration-id if not present

Closes gh-10671
pull/10708/merge
Madhura Bhave 8 years ago
parent
commit
eb446d07d9
  1. 3
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientProperties.java
  2. 10
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesRegistrationAdapter.java
  3. 45
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesRegistrationAdapterTests.java
  4. 11
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesTests.java

3
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())) { if (!StringUtils.hasText(registration.getClientSecret())) {
throw new IllegalStateException("Client secret must not be empty."); throw new IllegalStateException("Client secret must not be empty.");
} }
if (!StringUtils.hasText(registration.getProvider())) {
throw new IllegalStateException("Provider must not be empty.");
}
} }
/** /**

10
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(); return builder.build();
} }
private static Builder getBuilder(String registrationId, String providerId, private static Builder getBuilder(String registrationId, String configuredProviderId,
Map<String, Provider> providers) { Map<String, Provider> providers) {
String providerId = (configuredProviderId == null ? registrationId : configuredProviderId);
CommonOAuth2Provider provider = getCommonProvider(providerId); CommonOAuth2Provider provider = getCommonProvider(providerId);
if (provider == null && !providers.containsKey(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) Builder builder = (provider != null ? provider.getBuilder(registrationId)
: new Builder(registrationId)); : new Builder(registrationId));
@ -82,6 +83,11 @@ final class OAuth2ClientPropertiesRegistrationAdapter {
return builder; 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) { private static Builder getBuilder(Builder builder, Provider provider) {
copyIfNotNull(provider::getAuthorizationUri, builder::authorizationUri); copyIfNotNull(provider::getAuthorizationUri, builder::authorizationUri);
copyIfNotNull(provider::getTokenUri, builder::tokenUri); copyIfNotNull(provider::getTokenUri, builder::tokenUri);

45
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); 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<String, ClientRegistration> 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);
}
} }

11
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(); 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();
}
} }

Loading…
Cancel
Save