mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Default provider to registration-id if not present
Closes gh-10671
This commit is contained in:
parent
df30d2fc10
commit
eb446d07d9
@ -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.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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<String, Provider> 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);
|
||||
|
@ -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<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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user