Search in sources :

Example 16 with Builder

use of org.springframework.security.oauth2.client.registration.ClientRegistration.Builder in project spring-boot by spring-projects.

the class OAuth2ClientPropertiesRegistrationAdapter method getBuilder.

private static Builder getBuilder(String registrationId, String configuredProviderId, Map<String, Provider> providers) {
    String providerId = (configuredProviderId != null) ? configuredProviderId : registrationId;
    CommonOAuth2Provider provider = getCommonProvider(providerId);
    if (provider == null && !providers.containsKey(providerId)) {
        throw new IllegalStateException(getErrorMessage(configuredProviderId, registrationId));
    }
    Builder builder = (provider != null) ? provider.getBuilder(registrationId) : ClientRegistration.withRegistrationId(registrationId);
    if (providers.containsKey(providerId)) {
        return getBuilder(builder, providers.get(providerId));
    }
    return builder;
}
Also used : Builder(org.springframework.security.oauth2.client.registration.ClientRegistration.Builder) CommonOAuth2Provider(org.springframework.security.config.oauth2.client.CommonOAuth2Provider)

Example 17 with Builder

use of org.springframework.security.oauth2.client.registration.ClientRegistration.Builder 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 18 with Builder

use of org.springframework.security.oauth2.client.registration.ClientRegistration.Builder in project books by aidanwhiteley.

the class UserServiceTest method configureOauth.

private void configureOauth(String clientId, String name) {
    Map<String, Object> details = new LinkedHashMap<>();
    details.put("name", name);
    details.put(name, name);
    Set<GrantedAuthority> authorities = new HashSet<>();
    authorities.add(new SimpleGrantedAuthority("USER"));
    OAuth2User oauth2User = new DefaultOAuth2User(authorities, details, name);
    when(oauthToken.getName()).thenReturn(DUMMY);
    when(oauthToken.getAuthorizedClientRegistrationId()).thenReturn(DUMMY);
    when(oauthToken.getPrincipal()).thenReturn(oauth2User);
    OAuth2AuthorizedClient client = Mockito.mock(OAuth2AuthorizedClient.class);
    ClientRegistration.Builder builder = ClientRegistration.withRegistrationId(DUMMY);
    builder.clientId(clientId).authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE).clientSecret(DUMMY).redirectUri(DUMMY).scope(DUMMY).authorizationUri(DUMMY).tokenUri(DUMMY).clientName(DUMMY);
    ClientRegistration clientReg = builder.build();
    when(client.getClientRegistration()).thenReturn(clientReg);
    when(authorisedClientService.loadAuthorizedClient(any(String.class), any(String.class))).thenReturn(client);
}
Also used : DefaultOAuth2User(org.springframework.security.oauth2.core.user.DefaultOAuth2User) OAuth2User(org.springframework.security.oauth2.core.user.OAuth2User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) DefaultOAuth2User(org.springframework.security.oauth2.core.user.DefaultOAuth2User) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient)

Example 19 with Builder

use of org.springframework.security.oauth2.client.registration.ClientRegistration.Builder in project service-authorization by reportportal.

the class WebMvcConfig method configureHandlerExceptionResolvers.

@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    RestErrorDefinition<Exception> authErrorDefinition = new RestErrorDefinition<>(HttpStatus.BAD_REQUEST, ErrorType.ACCESS_DENIED, new DefaultExceptionMessageBuilder());
    Map<Class<? extends Throwable>, RestErrorDefinition> errorMappings = ImmutableMap.<Class<? extends Throwable>, RestErrorDefinition>builder().put(OAuth2Exception.class, authErrorDefinition).put(AuthenticationException.class, authErrorDefinition).put(UsernameNotFoundException.class, authErrorDefinition).putAll(ExceptionMappings.DEFAULT_MAPPING).build();
    RestExceptionHandler handler = new RestExceptionHandler();
    handler.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
    handler.setErrorResolver(new ReportPortalExceptionResolver(new DefaultErrorResolver(errorMappings)));
    handler.setMessageConverters(messageConverters.getConverters());
    exceptionResolvers.add(handler);
}
Also used : DefaultExceptionMessageBuilder(com.epam.ta.reportportal.commons.exception.message.DefaultExceptionMessageBuilder) ReportPortalExceptionResolver(com.epam.ta.reportportal.commons.exception.rest.ReportPortalExceptionResolver) RestExceptionHandler(com.epam.ta.reportportal.commons.exception.rest.RestExceptionHandler) RestErrorDefinition(com.epam.ta.reportportal.commons.exception.rest.RestErrorDefinition) AuthenticationException(org.springframework.security.core.AuthenticationException) DefaultErrorResolver(com.epam.ta.reportportal.commons.exception.rest.DefaultErrorResolver) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) OAuth2Exception(org.springframework.security.oauth2.common.exceptions.OAuth2Exception) AuthenticationException(org.springframework.security.core.AuthenticationException)

Example 20 with Builder

use of org.springframework.security.oauth2.client.registration.ClientRegistration.Builder in project spring-security by spring-projects.

the class CommonOAuth2ProviderTests method getBuilderWhenOktaShouldHaveOktaSettings.

@Test
public void getBuilderWhenOktaShouldHaveOktaSettings() {
    ClientRegistration registration = builder(CommonOAuth2Provider.OKTA).authorizationUri("https://example.com/auth").tokenUri("https://example.com/token").userInfoUri("https://example.com/info").jwkSetUri("https://example.com/jwkset").build();
    ProviderDetails providerDetails = registration.getProviderDetails();
    assertThat(providerDetails.getAuthorizationUri()).isEqualTo("https://example.com/auth");
    assertThat(providerDetails.getTokenUri()).isEqualTo("https://example.com/token");
    assertThat(providerDetails.getUserInfoEndpoint().getUri()).isEqualTo("https://example.com/info");
    assertThat(providerDetails.getUserInfoEndpoint().getUserNameAttributeName()).isEqualTo(IdTokenClaimNames.SUB);
    assertThat(providerDetails.getJwkSetUri()).isEqualTo("https://example.com/jwkset");
    assertThat(registration.getClientAuthenticationMethod()).isEqualTo(ClientAuthenticationMethod.CLIENT_SECRET_BASIC);
    assertThat(registration.getAuthorizationGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
    assertThat(registration.getRedirectUri()).isEqualTo(DEFAULT_REDIRECT_URL);
    assertThat(registration.getScopes()).containsOnly("openid", "profile", "email");
    assertThat(registration.getClientName()).isEqualTo("Okta");
    assertThat(registration.getRegistrationId()).isEqualTo("123");
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) ProviderDetails(org.springframework.security.oauth2.client.registration.ClientRegistration.ProviderDetails) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)9 Jwt (org.springframework.security.oauth2.jwt.Jwt)8 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)6 GrantedAuthority (org.springframework.security.core.GrantedAuthority)5 Authentication (org.springframework.security.core.Authentication)3 Builder (org.springframework.security.oauth2.client.registration.ClientRegistration.Builder)3 ClientRegistrationException (org.springframework.security.oauth2.provider.ClientRegistrationException)3 EncryptionException (com.evolveum.midpoint.prism.crypto.EncryptionException)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 HashMap (java.util.HashMap)2 Base64Exception (org.apache.cxf.common.util.Base64Exception)2 CommonOAuth2Provider (org.springframework.security.config.oauth2.client.CommonOAuth2Provider)2 AuthenticationException (org.springframework.security.core.AuthenticationException)2 OAuth2AuthorizeRequest (org.springframework.security.oauth2.client.OAuth2AuthorizeRequest)2 ClientDetails (org.springframework.security.oauth2.provider.ClientDetails)2 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)2 DefaultExceptionMessageBuilder (com.epam.ta.reportportal.commons.exception.message.DefaultExceptionMessageBuilder)1 DefaultErrorResolver (com.epam.ta.reportportal.commons.exception.rest.DefaultErrorResolver)1 ReportPortalExceptionResolver (com.epam.ta.reportportal.commons.exception.rest.ReportPortalExceptionResolver)1