Search in sources :

Example 6 with OAuth2AuthorizedClientProvider

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

the class DelegatingOAuth2AuthorizedClientProviderTests method authorizeWhenProviderCanAuthorizeThenReturnAuthorizedClient.

@Test
public void authorizeWhenProviderCanAuthorizeThenReturnAuthorizedClient() {
    Authentication principal = new TestingAuthenticationToken("principal", "password");
    ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration().build();
    OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(clientRegistration, principal.getName(), TestOAuth2AccessTokens.noScopes());
    OAuth2AuthorizedClientProvider authorizedClientProvider = mock(OAuth2AuthorizedClientProvider.class);
    given(authorizedClientProvider.authorize(any())).willReturn(authorizedClient);
    DelegatingOAuth2AuthorizedClientProvider delegate = new DelegatingOAuth2AuthorizedClientProvider(mock(OAuth2AuthorizedClientProvider.class), mock(OAuth2AuthorizedClientProvider.class), authorizedClientProvider);
    OAuth2AuthorizationContext context = OAuth2AuthorizationContext.withClientRegistration(clientRegistration).principal(principal).build();
    OAuth2AuthorizedClient reauthorizedClient = delegate.authorize(context);
    assertThat(reauthorizedClient).isSameAs(authorizedClient);
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) Authentication(org.springframework.security.core.Authentication) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.jupiter.api.Test)

Aggregations

Authentication (org.springframework.security.core.Authentication)5 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)4 OAuth2AuthorizedClientProvider (org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider)4 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)4 DefaultOAuth2AuthorizedClientManager (org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager)4 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)3 Duration (java.time.Duration)3 Test (org.junit.jupiter.api.Test)3 HttpStatus (org.springframework.http.HttpStatus)3 ClientCredentialsOAuth2AuthorizedClientProvider (org.springframework.security.oauth2.client.ClientCredentialsOAuth2AuthorizedClientProvider)3 OAuth2AuthorizedClient (org.springframework.security.oauth2.client.OAuth2AuthorizedClient)3 OAuth2AuthorizedClientRepository (org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository)3 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Consumer (java.util.function.Consumer)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 HttpHeaders (org.springframework.http.HttpHeaders)2 AuthorityUtils (org.springframework.security.core.authority.AuthorityUtils)2