Search in sources :

Example 1 with OAuth2AuthorizedClientRepository

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

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

the class SecurityMockMvcRequestPostProcessorsOAuth2ClientTests method oauth2ClientWhenUsedOnceThenDoesNotAffectRemainingTests.

@Test
public void oauth2ClientWhenUsedOnceThenDoesNotAffectRemainingTests() throws Exception {
    this.mvc.perform(get("/client-id").with(oauth2Client("registration-id"))).andExpect(content().string("test-client"));
    OAuth2AuthorizedClient client = new OAuth2AuthorizedClient(TestClientRegistrations.clientRegistration().build(), "sub", TestOAuth2AccessTokens.noScopes());
    OAuth2AuthorizedClientRepository repository = this.context.getBean(OAuth2AuthorizedClientRepository.class);
    given(repository.loadAuthorizedClient(eq("registration-id"), any(Authentication.class), any(HttpServletRequest.class))).willReturn(client);
    this.mvc.perform(get("/client-id")).andExpect(content().string("client-id"));
    verify(repository).loadAuthorizedClient(eq("registration-id"), any(Authentication.class), any(HttpServletRequest.class));
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) RegisteredOAuth2AuthorizedClient(org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) OAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository) Test(org.junit.jupiter.api.Test)

Example 3 with OAuth2AuthorizedClientRepository

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

the class OAuth2ClientConfigurationTests method requestWhenAuthorizedClientNotFoundAndClientCredentialsThenTokenResponseClientIsUsed.

@Test
public void requestWhenAuthorizedClientNotFoundAndClientCredentialsThenTokenResponseClientIsUsed() throws Exception {
    String clientRegistrationId = "client1";
    String principalName = "user1";
    TestingAuthenticationToken authentication = new TestingAuthenticationToken(principalName, "password");
    ClientRegistrationRepository clientRegistrationRepository = mock(ClientRegistrationRepository.class);
    OAuth2AuthorizedClientRepository authorizedClientRepository = mock(OAuth2AuthorizedClientRepository.class);
    OAuth2AccessTokenResponseClient accessTokenResponseClient = mock(OAuth2AccessTokenResponseClient.class);
    ClientRegistration clientRegistration = TestClientRegistrations.clientCredentials().registrationId(clientRegistrationId).build();
    given(clientRegistrationRepository.findByRegistrationId(clientRegistrationId)).willReturn(clientRegistration);
    // @formatter:off
    OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234").tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(300).build();
    // @formatter:on
    given(accessTokenResponseClient.getTokenResponse(any(OAuth2ClientCredentialsGrantRequest.class))).willReturn(accessTokenResponse);
    OAuth2AuthorizedClientArgumentResolverConfig.CLIENT_REGISTRATION_REPOSITORY = clientRegistrationRepository;
    OAuth2AuthorizedClientArgumentResolverConfig.AUTHORIZED_CLIENT_REPOSITORY = authorizedClientRepository;
    OAuth2AuthorizedClientArgumentResolverConfig.ACCESS_TOKEN_RESPONSE_CLIENT = accessTokenResponseClient;
    this.spring.register(OAuth2AuthorizedClientArgumentResolverConfig.class).autowire();
    MockHttpServletRequestBuilder authenticatedRequest = get("/authorized-client").with(authentication(authentication));
    // @formatter:off
    this.mockMvc.perform(authenticatedRequest).andExpect(status().isOk()).andExpect(content().string("resolved"));
    // @formatter:on
    verify(accessTokenResponseClient, times(1)).getTokenResponse(any(OAuth2ClientCredentialsGrantRequest.class));
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ClientRegistrationRepository) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) OAuth2AccessTokenResponseClient(org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient) OAuth2ClientCredentialsGrantRequest(org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequest) OAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository) Test(org.junit.jupiter.api.Test)

Example 4 with OAuth2AuthorizedClientRepository

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

the class OAuth2ClientConfigurationTests method requestWhenAuthorizedClientManagerConfiguredThenUsed.

// gh-8700
@Test
public void requestWhenAuthorizedClientManagerConfiguredThenUsed() throws Exception {
    String clientRegistrationId = "client1";
    String principalName = "user1";
    TestingAuthenticationToken authentication = new TestingAuthenticationToken(principalName, "password");
    ClientRegistrationRepository clientRegistrationRepository = mock(ClientRegistrationRepository.class);
    OAuth2AuthorizedClientRepository authorizedClientRepository = mock(OAuth2AuthorizedClientRepository.class);
    OAuth2AuthorizedClientManager authorizedClientManager = mock(OAuth2AuthorizedClientManager.class);
    ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration().registrationId(clientRegistrationId).build();
    OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(clientRegistration, principalName, TestOAuth2AccessTokens.noScopes());
    given(authorizedClientManager.authorize(any())).willReturn(authorizedClient);
    OAuth2AuthorizedClientManagerRegisteredConfig.CLIENT_REGISTRATION_REPOSITORY = clientRegistrationRepository;
    OAuth2AuthorizedClientManagerRegisteredConfig.AUTHORIZED_CLIENT_REPOSITORY = authorizedClientRepository;
    OAuth2AuthorizedClientManagerRegisteredConfig.AUTHORIZED_CLIENT_MANAGER = authorizedClientManager;
    this.spring.register(OAuth2AuthorizedClientManagerRegisteredConfig.class).autowire();
    MockHttpServletRequestBuilder authenticatedRequest = get("/authorized-client").with(authentication(authentication));
    // @formatter:off
    this.mockMvc.perform(authenticatedRequest).andExpect(status().isOk()).andExpect(content().string("resolved"));
    // @formatter:on
    verify(authorizedClientManager).authorize(any());
    verifyNoInteractions(clientRegistrationRepository);
    verifyNoInteractions(authorizedClientRepository);
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ClientRegistrationRepository) RegisteredOAuth2AuthorizedClient(org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) OAuth2AuthorizedClientManager(org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager) OAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository) Test(org.junit.jupiter.api.Test)

Example 5 with OAuth2AuthorizedClientRepository

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

the class OAuth2ClientConfigurationTests method requestWhenAuthorizedClientFoundThenMethodArgumentResolved.

@Test
public void requestWhenAuthorizedClientFoundThenMethodArgumentResolved() throws Exception {
    String clientRegistrationId = "client1";
    String principalName = "user1";
    TestingAuthenticationToken authentication = new TestingAuthenticationToken(principalName, "password");
    ClientRegistrationRepository clientRegistrationRepository = mock(ClientRegistrationRepository.class);
    ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration().registrationId(clientRegistrationId).build();
    given(clientRegistrationRepository.findByRegistrationId(eq(clientRegistrationId))).willReturn(clientRegistration);
    OAuth2AuthorizedClientRepository authorizedClientRepository = mock(OAuth2AuthorizedClientRepository.class);
    OAuth2AuthorizedClient authorizedClient = mock(OAuth2AuthorizedClient.class);
    given(authorizedClient.getClientRegistration()).willReturn(clientRegistration);
    given(authorizedClientRepository.loadAuthorizedClient(eq(clientRegistrationId), eq(authentication), any(HttpServletRequest.class))).willReturn(authorizedClient);
    OAuth2AccessToken accessToken = mock(OAuth2AccessToken.class);
    given(authorizedClient.getAccessToken()).willReturn(accessToken);
    OAuth2AccessTokenResponseClient accessTokenResponseClient = mock(OAuth2AccessTokenResponseClient.class);
    OAuth2AuthorizedClientArgumentResolverConfig.CLIENT_REGISTRATION_REPOSITORY = clientRegistrationRepository;
    OAuth2AuthorizedClientArgumentResolverConfig.AUTHORIZED_CLIENT_REPOSITORY = authorizedClientRepository;
    OAuth2AuthorizedClientArgumentResolverConfig.ACCESS_TOKEN_RESPONSE_CLIENT = accessTokenResponseClient;
    this.spring.register(OAuth2AuthorizedClientArgumentResolverConfig.class).autowire();
    // @formatter:off
    this.mockMvc.perform(get("/authorized-client").with(authentication(authentication))).andExpect(status().isOk()).andExpect(content().string("resolved"));
    // @formatter:on
    verifyZeroInteractions(accessTokenResponseClient);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) ClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ClientRegistrationRepository) RegisteredOAuth2AuthorizedClient(org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) OAuth2AccessTokenResponseClient(org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient) OAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository) Test(org.junit.jupiter.api.Test)

Aggregations

OAuth2AuthorizedClientRepository (org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository)6 Test (org.junit.jupiter.api.Test)4 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)4 OAuth2AuthorizedClient (org.springframework.security.oauth2.client.OAuth2AuthorizedClient)4 ClientRegistrationRepository (org.springframework.security.oauth2.client.registration.ClientRegistrationRepository)4 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)3 RegisteredOAuth2AuthorizedClient (org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient)3 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 Authentication (org.springframework.security.core.Authentication)2 OAuth2AccessTokenResponseClient (org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient)2 AuthenticatedPrincipalOAuth2AuthorizedClientRepository (org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository)2 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)2 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 InMemoryOAuth2AuthorizedClientService (org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService)1 OAuth2AuthorizedClientManager (org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager)1 OAuth2ClientCredentialsGrantRequest (org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequest)1