Search in sources :

Example 1 with InMemoryOAuth2AuthorizedClientService

use of org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService 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 2 with InMemoryOAuth2AuthorizedClientService

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

the class InMemoryOAuth2AuthorizedClientServiceTests method constructorWhenAuthorizedClientsProvidedThenUseProvidedAuthorizedClients.

@Test
public void constructorWhenAuthorizedClientsProvidedThenUseProvidedAuthorizedClients() {
    String registrationId = this.registration3.getRegistrationId();
    Map<OAuth2AuthorizedClientId, OAuth2AuthorizedClient> authorizedClients = Collections.singletonMap(new OAuth2AuthorizedClientId(this.registration3.getRegistrationId(), this.principalName1), mock(OAuth2AuthorizedClient.class));
    ClientRegistrationRepository clientRegistrationRepository = mock(ClientRegistrationRepository.class);
    given(clientRegistrationRepository.findByRegistrationId(eq(registrationId))).willReturn(this.registration3);
    InMemoryOAuth2AuthorizedClientService authorizedClientService = new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository, authorizedClients);
    assertThatObject(authorizedClientService.loadAuthorizedClient(registrationId, this.principalName1)).isNotNull();
}
Also used : InMemoryClientRegistrationRepository(org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository) ClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ClientRegistrationRepository) Test(org.junit.jupiter.api.Test)

Example 3 with InMemoryOAuth2AuthorizedClientService

use of org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService 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 InMemoryOAuth2AuthorizedClientService

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

the class ServletOAuth2AuthorizedClientExchangeFilterFunctionITests method setUp.

@BeforeEach
public void setUp() throws Exception {
    this.clientRegistrationRepository = mock(ClientRegistrationRepository.class);
    final OAuth2AuthorizedClientRepository delegate = new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(new InMemoryOAuth2AuthorizedClientService(this.clientRegistrationRepository));
    this.authorizedClientRepository = spy(new OAuth2AuthorizedClientRepository() {

        @Override
        public <T extends OAuth2AuthorizedClient> T loadAuthorizedClient(String clientRegistrationId, Authentication principal, HttpServletRequest request) {
            return delegate.loadAuthorizedClient(clientRegistrationId, principal, request);
        }

        @Override
        public void saveAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal, HttpServletRequest request, HttpServletResponse response) {
            delegate.saveAuthorizedClient(authorizedClient, principal, request, response);
        }

        @Override
        public void removeAuthorizedClient(String clientRegistrationId, Authentication principal, HttpServletRequest request, HttpServletResponse response) {
            delegate.removeAuthorizedClient(clientRegistrationId, principal, request, response);
        }
    });
    this.authorizedClientFilter = new ServletOAuth2AuthorizedClientExchangeFilterFunction(this.clientRegistrationRepository, this.authorizedClientRepository);
    this.server = new MockWebServer();
    this.server.start();
    this.serverUrl = this.server.url("/").toString();
    this.webClient = WebClient.builder().apply(this.authorizedClientFilter.oauth2Configuration()).build();
    this.authentication = new TestingAuthenticationToken("principal", "password");
    SecurityContextHolder.getContext().setAuthentication(this.authentication);
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.request, this.response));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) InMemoryOAuth2AuthorizedClientService(org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ClientRegistrationRepository) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) AuthenticatedPrincipalOAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository) OAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) AuthenticatedPrincipalOAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository) MockWebServer(okhttp3.mockwebserver.MockWebServer) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with InMemoryOAuth2AuthorizedClientService

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

the class OAuth2AuthorizationCodeGrantFilterTests method setup.

@BeforeEach
public void setup() {
    this.registration1 = TestClientRegistrations.clientRegistration().build();
    this.clientRegistrationRepository = new InMemoryClientRegistrationRepository(this.registration1);
    this.authorizedClientService = new InMemoryOAuth2AuthorizedClientService(this.clientRegistrationRepository);
    this.authorizedClientRepository = new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(this.authorizedClientService);
    this.authorizationRequestRepository = new HttpSessionOAuth2AuthorizationRequestRepository();
    this.authenticationManager = mock(AuthenticationManager.class);
    this.filter = spy(new OAuth2AuthorizationCodeGrantFilter(this.clientRegistrationRepository, this.authorizedClientRepository, this.authenticationManager));
    this.filter.setAuthorizationRequestRepository(this.authorizationRequestRepository);
    TestingAuthenticationToken authentication = new TestingAuthenticationToken(this.principalName1, "password");
    authentication.setAuthenticated(true);
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    securityContext.setAuthentication(authentication);
    SecurityContextHolder.setContext(securityContext);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) InMemoryOAuth2AuthorizedClientService(org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService) InMemoryClientRegistrationRepository(org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository) SecurityContext(org.springframework.security.core.context.SecurityContext) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

BeforeEach (org.junit.jupiter.api.BeforeEach)4 InMemoryOAuth2AuthorizedClientService (org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService)4 InMemoryClientRegistrationRepository (org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository)4 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)2 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)2 ClientRegistrationRepository (org.springframework.security.oauth2.client.registration.ClientRegistrationRepository)2 AuthenticatedPrincipalOAuth2AuthorizedClientRepository (org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1 Test (org.junit.jupiter.api.Test)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)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 OAuth2AccessTokenResponseClient (org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient)1 OAuth2AuthorizationCodeGrantRequest (org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest)1 DefaultOAuth2AuthorizationRequestResolver (org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizationRequestResolver)1