Search in sources :

Example 1 with PasswordOAuth2AuthorizedClientProvider

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

the class PasswordOAuth2AuthorizedClientProviderTests method setup.

@BeforeEach
public void setup() {
    this.authorizedClientProvider = new PasswordOAuth2AuthorizedClientProvider();
    this.accessTokenResponseClient = mock(OAuth2AccessTokenResponseClient.class);
    this.authorizedClientProvider.setAccessTokenResponseClient(this.accessTokenResponseClient);
    this.clientRegistration = TestClientRegistrations.password().build();
    this.principal = new TestingAuthenticationToken("principal", "password");
}
Also used : OAuth2AccessTokenResponseClient(org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with PasswordOAuth2AuthorizedClientProvider

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

the class OAuth2AuthorizedClientArgumentResolverTests method resolveArgumentWhenAuthorizedClientNotFoundForPasswordClientThenResolvesFromTokenResponseClient.

@SuppressWarnings("unchecked")
@Test
public void resolveArgumentWhenAuthorizedClientNotFoundForPasswordClientThenResolvesFromTokenResponseClient() throws Exception {
    OAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> passwordTokenResponseClient = mock(OAuth2AccessTokenResponseClient.class);
    PasswordOAuth2AuthorizedClientProvider passwordAuthorizedClientProvider = new PasswordOAuth2AuthorizedClientProvider();
    passwordAuthorizedClientProvider.setAccessTokenResponseClient(passwordTokenResponseClient);
    DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager(this.clientRegistrationRepository, this.authorizedClientRepository);
    authorizedClientManager.setAuthorizedClientProvider(passwordAuthorizedClientProvider);
    // Set custom contextAttributesMapper
    authorizedClientManager.setContextAttributesMapper((authorizeRequest) -> {
        Map<String, Object> contextAttributes = new HashMap<>();
        HttpServletRequest servletRequest = authorizeRequest.getAttribute(HttpServletRequest.class.getName());
        String username = servletRequest.getParameter(OAuth2ParameterNames.USERNAME);
        String password = servletRequest.getParameter(OAuth2ParameterNames.PASSWORD);
        if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
            contextAttributes.put(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username);
            contextAttributes.put(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password);
        }
        return contextAttributes;
    });
    this.argumentResolver = new OAuth2AuthorizedClientArgumentResolver(authorizedClientManager);
    OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234").tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(3600).build();
    given(passwordTokenResponseClient.getTokenResponse(any())).willReturn(accessTokenResponse);
    given(this.authorizedClientRepository.loadAuthorizedClient(anyString(), any(), any(HttpServletRequest.class))).willReturn(null);
    MethodParameter methodParameter = this.getMethodParameter("passwordClient", OAuth2AuthorizedClient.class);
    this.request.setParameter(OAuth2ParameterNames.USERNAME, "username");
    this.request.setParameter(OAuth2ParameterNames.PASSWORD, "password");
    OAuth2AuthorizedClient authorizedClient = (OAuth2AuthorizedClient) this.argumentResolver.resolveArgument(methodParameter, null, new ServletWebRequest(this.request, this.response), null);
    assertThat(authorizedClient).isNotNull();
    assertThat(authorizedClient.getClientRegistration()).isSameAs(this.registration3);
    assertThat(authorizedClient.getPrincipalName()).isEqualTo(this.principalName);
    assertThat(authorizedClient.getAccessToken()).isSameAs(accessTokenResponse.getAccessToken());
    verify(this.authorizedClientRepository).saveAuthorizedClient(eq(authorizedClient), eq(this.authentication), any(HttpServletRequest.class), any(HttpServletResponse.class));
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) HashMap(java.util.HashMap) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) OAuth2PasswordGrantRequest(org.springframework.security.oauth2.client.endpoint.OAuth2PasswordGrantRequest) RegisteredOAuth2AuthorizedClient(org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) PasswordOAuth2AuthorizedClientProvider(org.springframework.security.oauth2.client.PasswordOAuth2AuthorizedClientProvider) MethodParameter(org.springframework.core.MethodParameter) DefaultOAuth2AuthorizedClientManager(org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Aggregations

HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 HashMap (java.util.HashMap)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 MethodParameter (org.springframework.core.MethodParameter)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)1 OAuth2AuthorizedClient (org.springframework.security.oauth2.client.OAuth2AuthorizedClient)1 PasswordOAuth2AuthorizedClientProvider (org.springframework.security.oauth2.client.PasswordOAuth2AuthorizedClientProvider)1 RegisteredOAuth2AuthorizedClient (org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient)1 OAuth2AccessTokenResponseClient (org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient)1 OAuth2PasswordGrantRequest (org.springframework.security.oauth2.client.endpoint.OAuth2PasswordGrantRequest)1 DefaultOAuth2AuthorizedClientManager (org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager)1 OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1