Search in sources :

Example 1 with AuthenticatedPrincipalOAuth2AuthorizedClientRepository

use of org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository in project spring-security by spring-projects.

the class OAuth2ClientConfigurerUtils method getAuthorizedClientRepository.

static <B extends HttpSecurityBuilder<B>> OAuth2AuthorizedClientRepository getAuthorizedClientRepository(B builder) {
    OAuth2AuthorizedClientRepository authorizedClientRepository = builder.getSharedObject(OAuth2AuthorizedClientRepository.class);
    if (authorizedClientRepository == null) {
        authorizedClientRepository = getAuthorizedClientRepositoryBean(builder);
        if (authorizedClientRepository == null) {
            authorizedClientRepository = new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(getAuthorizedClientService((builder)));
        }
        builder.setSharedObject(OAuth2AuthorizedClientRepository.class, authorizedClientRepository);
    }
    return authorizedClientRepository;
}
Also used : AuthenticatedPrincipalOAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository) OAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository) AuthenticatedPrincipalOAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository)

Example 2 with AuthenticatedPrincipalOAuth2AuthorizedClientRepository

use of org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository in project spring-security by spring-projects.

the class OAuth2LoginAuthenticationFilterTests method setUp.

@BeforeEach
public void setUp() {
    this.registration1 = TestClientRegistrations.clientRegistration().build();
    this.registration2 = TestClientRegistrations.clientRegistration2().build();
    this.clientRegistrationRepository = new InMemoryClientRegistrationRepository(this.registration1, this.registration2);
    this.authorizedClientService = new InMemoryOAuth2AuthorizedClientService(this.clientRegistrationRepository);
    this.authorizedClientRepository = new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(this.authorizedClientService);
    this.authorizationRequestRepository = new HttpSessionOAuth2AuthorizationRequestRepository();
    this.failureHandler = mock(AuthenticationFailureHandler.class);
    this.authenticationManager = mock(AuthenticationManager.class);
    this.authenticationDetailsSource = mock(AuthenticationDetailsSource.class);
    this.filter = spy(new OAuth2LoginAuthenticationFilter(this.clientRegistrationRepository, this.authorizedClientRepository, OAuth2LoginAuthenticationFilter.DEFAULT_FILTER_PROCESSES_URI));
    this.filter.setAuthorizationRequestRepository(this.authorizationRequestRepository);
    this.filter.setAuthenticationFailureHandler(this.failureHandler);
    this.filter.setAuthenticationManager(this.authenticationManager);
    this.filter.setAuthenticationDetailsSource(this.authenticationDetailsSource);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) AuthenticationDetailsSource(org.springframework.security.authentication.AuthenticationDetailsSource) InMemoryOAuth2AuthorizedClientService(org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService) InMemoryClientRegistrationRepository(org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository) AuthenticationFailureHandler(org.springframework.security.web.authentication.AuthenticationFailureHandler) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with AuthenticatedPrincipalOAuth2AuthorizedClientRepository

use of org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository in project spring-security by spring-projects.

the class OAuth2ClientConfigurerTests method setup.

@BeforeEach
public void setup() {
    // @formatter:off
    this.registration1 = TestClientRegistrations.clientRegistration().registrationId("registration-1").clientId("client-1").clientSecret("secret").clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC).authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE).redirectUri("{baseUrl}/client-1").scope("user").authorizationUri("https://provider.com/oauth2/authorize").tokenUri("https://provider.com/oauth2/token").userInfoUri("https://provider.com/oauth2/user").userNameAttributeName("id").clientName("client-1").build();
    // @formatter:on
    clientRegistrationRepository = new InMemoryClientRegistrationRepository(this.registration1);
    authorizedClientService = new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
    authorizedClientRepository = new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(authorizedClientService);
    authorizationRequestResolver = new DefaultOAuth2AuthorizationRequestResolver(clientRegistrationRepository, "/oauth2/authorization");
    OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234").tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(300).build();
    accessTokenResponseClient = mock(OAuth2AccessTokenResponseClient.class);
    given(accessTokenResponseClient.getTokenResponse(any(OAuth2AuthorizationCodeGrantRequest.class))).willReturn(accessTokenResponse);
    requestCache = mock(RequestCache.class);
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) RequestCache(org.springframework.security.web.savedrequest.RequestCache) OAuth2AuthorizationCodeGrantRequest(org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest) InMemoryOAuth2AuthorizedClientService(org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService) InMemoryClientRegistrationRepository(org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository) AuthenticatedPrincipalOAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository) OAuth2AccessTokenResponseClient(org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient) DefaultOAuth2AuthorizationRequestResolver(org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizationRequestResolver) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with AuthenticatedPrincipalOAuth2AuthorizedClientRepository

use of org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository in project spring-security by spring-projects.

the class AuthenticatedPrincipalOAuth2AuthorizedClientRepositoryTests method setup.

@BeforeEach
public void setup() {
    this.authorizedClientService = mock(OAuth2AuthorizedClientService.class);
    this.anonymousAuthorizedClientRepository = mock(OAuth2AuthorizedClientRepository.class);
    this.authorizedClientRepository = new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(this.authorizedClientService);
    this.authorizedClientRepository.setAnonymousAuthorizedClientRepository(this.anonymousAuthorizedClientRepository);
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OAuth2AuthorizedClientService(org.springframework.security.oauth2.client.OAuth2AuthorizedClientService) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with AuthenticatedPrincipalOAuth2AuthorizedClientRepository

use of org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository in project spring-security by spring-projects.

the class OAuth2ClientConfigurer method authorizedClientService.

/**
 * Sets the service for authorized client(s).
 * @param authorizedClientService the authorized client service
 * @return the {@link OAuth2ClientConfigurer} for further configuration
 */
public OAuth2ClientConfigurer<B> authorizedClientService(OAuth2AuthorizedClientService authorizedClientService) {
    Assert.notNull(authorizedClientService, "authorizedClientService cannot be null");
    this.authorizedClientRepository(new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(authorizedClientService));
    return this;
}
Also used : AuthenticatedPrincipalOAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository)

Aggregations

BeforeEach (org.junit.jupiter.api.BeforeEach)5 AuthenticatedPrincipalOAuth2AuthorizedClientRepository (org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository)5 InMemoryOAuth2AuthorizedClientService (org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService)4 InMemoryClientRegistrationRepository (org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)2 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)2 OAuth2AuthorizedClientRepository (org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1 AuthenticationDetailsSource (org.springframework.security.authentication.AuthenticationDetailsSource)1 Authentication (org.springframework.security.core.Authentication)1 SecurityContext (org.springframework.security.core.context.SecurityContext)1 OAuth2AuthorizedClient (org.springframework.security.oauth2.client.OAuth2AuthorizedClient)1 OAuth2AuthorizedClientService (org.springframework.security.oauth2.client.OAuth2AuthorizedClientService)1 OAuth2AccessTokenResponseClient (org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient)1 OAuth2AuthorizationCodeGrantRequest (org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest)1 ClientRegistrationRepository (org.springframework.security.oauth2.client.registration.ClientRegistrationRepository)1