Search in sources :

Example 21 with Builder

use of org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest.Builder in project spring-security by spring-projects.

the class ClientRegistrationsBeanDefinitionParser method getClientRegistrations.

private List<ClientRegistration> getClientRegistrations(Element element, ParserContext parserContext, Map<String, Map<String, String>> providers) {
    List<Element> clientRegistrationElts = DomUtils.getChildElementsByTagName(element, ELT_CLIENT_REGISTRATION);
    List<ClientRegistration> clientRegistrations = new ArrayList<>();
    for (Element clientRegistrationElt : clientRegistrationElts) {
        String registrationId = clientRegistrationElt.getAttribute(ATT_REGISTRATION_ID);
        String providerId = clientRegistrationElt.getAttribute(ATT_PROVIDER_ID);
        ClientRegistration.Builder builder = getBuilderFromIssuerIfPossible(parserContext, registrationId, providerId, providers);
        if (builder == null) {
            builder = getBuilder(parserContext, registrationId, providerId, providers);
            if (builder == null) {
                Object source = parserContext.extractSource(element);
                parserContext.getReaderContext().error(getErrorMessage(providerId, registrationId), source);
                // error on the config skip to next element
                continue;
            }
        }
        getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_CLIENT_ID)).ifPresent(builder::clientId);
        getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_CLIENT_SECRET)).ifPresent(builder::clientSecret);
        getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_CLIENT_AUTHENTICATION_METHOD)).map(ClientAuthenticationMethod::new).ifPresent(builder::clientAuthenticationMethod);
        getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_AUTHORIZATION_GRANT_TYPE)).map(AuthorizationGrantType::new).ifPresent(builder::authorizationGrantType);
        getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_REDIRECT_URI)).ifPresent(builder::redirectUri);
        getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_SCOPE)).map(StringUtils::commaDelimitedListToSet).ifPresent(builder::scope);
        getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_CLIENT_NAME)).ifPresent(builder::clientName);
        clientRegistrations.add(builder.build());
    }
    return clientRegistrations;
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList)

Example 22 with Builder

use of org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest.Builder in project spring-security by spring-projects.

the class DefaultOAuth2AuthorizationRequestResolver method resolve.

private OAuth2AuthorizationRequest resolve(HttpServletRequest request, String registrationId, String redirectUriAction) {
    if (registrationId == null) {
        return null;
    }
    ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId(registrationId);
    if (clientRegistration == null) {
        throw new IllegalArgumentException("Invalid Client Registration with Id: " + registrationId);
    }
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(OAuth2ParameterNames.REGISTRATION_ID, clientRegistration.getRegistrationId());
    OAuth2AuthorizationRequest.Builder builder = getBuilder(clientRegistration, attributes);
    String redirectUriStr = expandRedirectUri(request, clientRegistration, redirectUriAction);
    // @formatter:off
    builder.clientId(clientRegistration.getClientId()).authorizationUri(clientRegistration.getProviderDetails().getAuthorizationUri()).redirectUri(redirectUriStr).scopes(clientRegistration.getScopes()).state(this.stateGenerator.generateKey()).attributes(attributes);
    // @formatter:on
    this.authorizationRequestCustomizer.accept(builder);
    return builder.build();
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) HashMap(java.util.HashMap) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)

Example 23 with Builder

use of org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest.Builder in project spring-security by spring-projects.

the class ServletOAuth2AuthorizedClientExchangeFilterFunction method reauthorizeClient.

private Mono<OAuth2AuthorizedClient> reauthorizeClient(OAuth2AuthorizedClient authorizedClient, ClientRequest request) {
    if (this.authorizedClientManager == null) {
        return Mono.just(authorizedClient);
    }
    Map<String, Object> attrs = request.attributes();
    Authentication authentication = getAuthentication(attrs);
    if (authentication == null) {
        authentication = createAuthentication(authorizedClient.getPrincipalName());
    }
    HttpServletRequest servletRequest = getRequest(attrs);
    HttpServletResponse servletResponse = getResponse(attrs);
    OAuth2AuthorizeRequest.Builder builder = OAuth2AuthorizeRequest.withAuthorizedClient(authorizedClient).principal(authentication);
    builder.attributes((attributes) -> addToAttributes(attributes, servletRequest, servletResponse));
    OAuth2AuthorizeRequest reauthorizeRequest = builder.build();
    // blocking I/O operation using RestTemplate internally
    return Mono.fromSupplier(() -> this.authorizedClientManager.authorize(reauthorizeRequest)).subscribeOn(Schedulers.boundedElastic());
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Authentication(org.springframework.security.core.Authentication) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) OAuth2AuthorizeRequest(org.springframework.security.oauth2.client.OAuth2AuthorizeRequest)

Example 24 with Builder

use of org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest.Builder in project spring-security by spring-projects.

the class OAuth2AuthorizationRequestDeserializer method deserialize.

private OAuth2AuthorizationRequest deserialize(JsonParser parser, ObjectMapper mapper, JsonNode root) throws JsonParseException {
    AuthorizationGrantType authorizationGrantType = AUTHORIZATION_GRANT_TYPE_CONVERTER.convert(JsonNodeUtils.findObjectNode(root, "authorizationGrantType"));
    Builder builder = getBuilder(parser, authorizationGrantType);
    builder.authorizationUri(JsonNodeUtils.findStringValue(root, "authorizationUri"));
    builder.clientId(JsonNodeUtils.findStringValue(root, "clientId"));
    builder.redirectUri(JsonNodeUtils.findStringValue(root, "redirectUri"));
    builder.scopes(JsonNodeUtils.findValue(root, "scopes", JsonNodeUtils.STRING_SET, mapper));
    builder.state(JsonNodeUtils.findStringValue(root, "state"));
    builder.additionalParameters(JsonNodeUtils.findValue(root, "additionalParameters", JsonNodeUtils.STRING_OBJECT_MAP, mapper));
    builder.authorizationRequestUri(JsonNodeUtils.findStringValue(root, "authorizationRequestUri"));
    builder.attributes(JsonNodeUtils.findValue(root, "attributes", JsonNodeUtils.STRING_OBJECT_MAP, mapper));
    return builder.build();
}
Also used : AuthorizationGrantType(org.springframework.security.oauth2.core.AuthorizationGrantType) Builder(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest.Builder)

Example 25 with Builder

use of org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest.Builder in project spring-security by spring-projects.

the class JwtAuthenticationTokenTests method getNameWhenConstructedWithJwtAndAuthoritiesThenReturnsSubject.

@Test
public void getNameWhenConstructedWithJwtAndAuthoritiesThenReturnsSubject() {
    Collection<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("test");
    Jwt jwt = builder().subject("Hayden").build();
    JwtAuthenticationToken token = new JwtAuthenticationToken(jwt, authorities);
    assertThat(token.getName()).isEqualTo("Hayden");
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) GrantedAuthority(org.springframework.security.core.GrantedAuthority) 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