use of org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties in project spring-boot by spring-projects.
the class OAuth2ClientPropertiesRegistrationAdapterTests method getClientRegistrationsWhenUsingCommonProviderShouldAdapt.
@Test
void getClientRegistrationsWhenUsingCommonProviderShouldAdapt() {
OAuth2ClientProperties properties = new OAuth2ClientProperties();
OAuth2ClientProperties.Registration registration = new OAuth2ClientProperties.Registration();
registration.setProvider("google");
registration.setClientId("clientId");
registration.setClientSecret("clientSecret");
properties.getRegistration().put("registration", registration);
Map<String, ClientRegistration> registrations = OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(properties);
ClientRegistration adapted = registrations.get("registration");
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");
UserInfoEndpoint userInfoEndpoint = adaptedProvider.getUserInfoEndpoint();
assertThat(userInfoEndpoint.getUri()).isEqualTo("https://www.googleapis.com/oauth2/v3/userinfo");
assertThat(userInfoEndpoint.getUserNameAttributeName()).isEqualTo(IdTokenClaimNames.SUB);
assertThat(adaptedProvider.getJwkSetUri()).isEqualTo("https://www.googleapis.com/oauth2/v3/certs");
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_BASIC);
assertThat(adapted.getAuthorizationGrantType()).isEqualTo(org.springframework.security.oauth2.core.AuthorizationGrantType.AUTHORIZATION_CODE);
assertThat(adapted.getRedirectUri()).isEqualTo("{baseUrl}/{action}/oauth2/code/{registrationId}");
assertThat(adapted.getScopes()).containsExactly("openid", "profile", "email");
assertThat(adapted.getClientName()).isEqualTo("Google");
}
use of org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties in project ANNIS by korpling.
the class CommonUI method redirectToLogin.
public void redirectToLogin() {
OAuth2ClientProperties oauth2Clients = getOauth2ClientProperties();
if (oauth2Clients != null) {
// Store the current fragment so it can be restored after login was successful
String oldFragment = Page.getCurrent().getUriFragment();
VaadinSession.getCurrent().setAttribute(SecurityConfiguration.FRAGMENT_TO_RESTORE, oldFragment);
VaadinRequest currentRequest = VaadinRequest.getCurrent();
final String contextPath = currentRequest == null ? "" : currentRequest.getContextPath();
// Determine if there is only one or several clients
Collection<String> providers = oauth2Clients.getProvider().keySet();
if (providers.size() == 1) {
// Directly login with the single provider
Page.getCurrent().setLocation(contextPath + "/oauth2/authorization/" + providers.iterator().next());
} else {
// Show general login selection page
Page.getCurrent().setLocation(contextPath + "/login");
}
}
}
use of org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties 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");
}
use of org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties 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);
}
use of org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties 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'");
}
Aggregations