Search in sources :

Example 41 with OAuth2AuthorizedClient

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

the class InMemoryOAuth2AuthorizedClientServiceTests method removeAuthorizedClientWhenSavedThenRemoved.

@Test
public void removeAuthorizedClientWhenSavedThenRemoved() {
    Authentication authentication = mock(Authentication.class);
    given(authentication.getName()).willReturn(this.principalName2);
    OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration2, this.principalName2, mock(OAuth2AccessToken.class));
    this.authorizedClientService.saveAuthorizedClient(authorizedClient, authentication);
    OAuth2AuthorizedClient loadedAuthorizedClient = this.authorizedClientService.loadAuthorizedClient(this.registration2.getRegistrationId(), this.principalName2);
    assertThat(loadedAuthorizedClient).isNotNull();
    this.authorizedClientService.removeAuthorizedClient(this.registration2.getRegistrationId(), this.principalName2);
    loadedAuthorizedClient = this.authorizedClientService.loadAuthorizedClient(this.registration2.getRegistrationId(), this.principalName2);
    assertThat(loadedAuthorizedClient).isNull();
}
Also used : Authentication(org.springframework.security.core.Authentication) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) Test(org.junit.jupiter.api.Test)

Example 42 with OAuth2AuthorizedClient

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

the class InMemoryOAuth2AuthorizedClientServiceTests method loadAuthorizedClientWhenClientRegistrationFoundAndAssociatedToPrincipalThenReturnAuthorizedClient.

@Test
public void loadAuthorizedClientWhenClientRegistrationFoundAndAssociatedToPrincipalThenReturnAuthorizedClient() {
    Authentication authentication = mock(Authentication.class);
    given(authentication.getName()).willReturn(this.principalName1);
    OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration1, this.principalName1, mock(OAuth2AccessToken.class));
    this.authorizedClientService.saveAuthorizedClient(authorizedClient, authentication);
    OAuth2AuthorizedClient loadedAuthorizedClient = this.authorizedClientService.loadAuthorizedClient(this.registration1.getRegistrationId(), this.principalName1);
    assertThat(loadedAuthorizedClient).isEqualTo(authorizedClient);
}
Also used : Authentication(org.springframework.security.core.Authentication) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) Test(org.junit.jupiter.api.Test)

Example 43 with OAuth2AuthorizedClient

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

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

the class JdbcOAuth2AuthorizedClientServiceTests method createAuthorizedClient.

private static OAuth2AuthorizedClient createAuthorizedClient(Authentication principal, ClientRegistration clientRegistration, boolean requiredAttributesOnly) {
    OAuth2AccessToken accessToken;
    if (!requiredAttributesOnly) {
        accessToken = TestOAuth2AccessTokens.scopes("read", "write");
    } else {
        accessToken = TestOAuth2AccessTokens.noScopes();
    }
    OAuth2RefreshToken refreshToken = null;
    if (!requiredAttributesOnly) {
        refreshToken = TestOAuth2RefreshTokens.refreshToken();
    }
    return new OAuth2AuthorizedClient(clientRegistration, principal.getName(), accessToken, refreshToken);
}
Also used : OAuth2RefreshToken(org.springframework.security.oauth2.core.OAuth2RefreshToken) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken)

Example 45 with OAuth2AuthorizedClient

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

the class JwtBearerReactiveOAuth2AuthorizedClientProviderTests method authorizeWhenJwtBearerAndTokenExpiredThenReauthorize.

@Test
public void authorizeWhenJwtBearerAndTokenExpiredThenReauthorize() {
    Instant now = Instant.now();
    Instant issuedAt = now.minus(Duration.ofMinutes(60));
    Instant expiresAt = now.minus(Duration.ofMinutes(30));
    OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "access-token-1234", issuedAt, expiresAt);
    OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.clientRegistration, this.principal.getName(), accessToken);
    OAuth2AccessTokenResponse accessTokenResponse = TestOAuth2AccessTokenResponses.accessTokenResponse().build();
    given(this.accessTokenResponseClient.getTokenResponse(any())).willReturn(Mono.just(accessTokenResponse));
    // @formatter:off
    OAuth2AuthorizationContext authorizationContext = OAuth2AuthorizationContext.withAuthorizedClient(authorizedClient).principal(this.principal).build();
    // @formatter:on
    authorizedClient = this.authorizedClientProvider.authorize(authorizationContext).block();
    assertThat(authorizedClient.getClientRegistration()).isSameAs(this.clientRegistration);
    assertThat(authorizedClient.getPrincipalName()).isEqualTo(this.principal.getName());
    assertThat(authorizedClient.getAccessToken()).isEqualTo(accessTokenResponse.getAccessToken());
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)140 OAuth2AuthorizedClient (org.springframework.security.oauth2.client.OAuth2AuthorizedClient)123 OAuth2AccessToken (org.springframework.security.oauth2.core.OAuth2AccessToken)66 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)51 OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)45 Instant (java.time.Instant)43 Authentication (org.springframework.security.core.Authentication)41 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)36 ClientRequest (org.springframework.web.reactive.function.client.ClientRequest)34 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)32 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)31 OAuth2RefreshToken (org.springframework.security.oauth2.core.OAuth2RefreshToken)31 BeforeEach (org.junit.jupiter.api.BeforeEach)28 OAuth2AuthorizationContext (org.springframework.security.oauth2.client.OAuth2AuthorizationContext)23 Map (java.util.Map)21 HashMap (java.util.HashMap)20 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)19 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)17 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)17 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)17