Search in sources :

Example 96 with OAuth2AuthorizedClient

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

the class R2dbcReactiveOAuth2AuthorizedClientServiceTests method saveAuthorizedClientWhenSaveThenLoadReturnsSaved.

@Test
public void saveAuthorizedClientWhenSaveThenLoadReturnsSaved() {
    Authentication principal = createPrincipal();
    final OAuth2AuthorizedClient expected = createAuthorizedClient(principal, this.clientRegistration);
    this.authorizedClientService.saveAuthorizedClient(expected, principal).as(StepVerifier::create).verifyComplete();
    this.authorizedClientService.loadAuthorizedClient(this.clientRegistration.getRegistrationId(), principal.getName()).as(StepVerifier::create).assertNext((authorizedClient) -> {
        assertThat(authorizedClient).isNotNull();
        assertThat(authorizedClient.getClientRegistration()).isEqualTo(expected.getClientRegistration());
        assertThat(authorizedClient.getPrincipalName()).isEqualTo(expected.getPrincipalName());
        assertThat(authorizedClient.getAccessToken().getTokenType()).isEqualTo(expected.getAccessToken().getTokenType());
        assertThat(authorizedClient.getAccessToken().getTokenValue()).isEqualTo(expected.getAccessToken().getTokenValue());
        assertThat(authorizedClient.getAccessToken().getIssuedAt()).isEqualTo(expected.getAccessToken().getIssuedAt());
        assertThat(authorizedClient.getAccessToken().getExpiresAt()).isEqualTo(expected.getAccessToken().getExpiresAt());
        assertThat(authorizedClient.getAccessToken().getScopes()).isEqualTo(expected.getAccessToken().getScopes());
        assertThat(authorizedClient.getRefreshToken().getTokenValue()).isEqualTo(expected.getRefreshToken().getTokenValue());
        assertThat(authorizedClient.getRefreshToken().getIssuedAt()).isEqualTo(expected.getRefreshToken().getIssuedAt());
    }).verifyComplete();
    // Test save/load of NOT NULL attributes only
    principal = createPrincipal();
    OAuth2AuthorizedClient updatedExpectedPrincipal = createAuthorizedClient(principal, this.clientRegistration, true);
    this.authorizedClientService.saveAuthorizedClient(updatedExpectedPrincipal, principal).as(StepVerifier::create).verifyComplete();
    this.authorizedClientService.loadAuthorizedClient(this.clientRegistration.getRegistrationId(), principal.getName()).as(StepVerifier::create).assertNext((authorizedClient) -> {
        assertThat(authorizedClient).isNotNull();
        assertThat(authorizedClient.getClientRegistration()).isEqualTo(updatedExpectedPrincipal.getClientRegistration());
        assertThat(authorizedClient.getPrincipalName()).isEqualTo(updatedExpectedPrincipal.getPrincipalName());
        assertThat(authorizedClient.getAccessToken().getTokenType()).isEqualTo(updatedExpectedPrincipal.getAccessToken().getTokenType());
        assertThat(authorizedClient.getAccessToken().getTokenValue()).isEqualTo(updatedExpectedPrincipal.getAccessToken().getTokenValue());
        assertThat(authorizedClient.getAccessToken().getIssuedAt()).isEqualTo(updatedExpectedPrincipal.getAccessToken().getIssuedAt());
        assertThat(authorizedClient.getAccessToken().getExpiresAt()).isEqualTo(updatedExpectedPrincipal.getAccessToken().getExpiresAt());
        assertThat(authorizedClient.getAccessToken().getScopes()).isEmpty();
        assertThat(authorizedClient.getRefreshToken()).isNull();
    }).verifyComplete();
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ClassPathResource(org.springframework.core.io.ClassPathResource) ConnectionFactoryInitializer(org.springframework.r2dbc.connection.init.ConnectionFactoryInitializer) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) DatabaseClient(org.springframework.r2dbc.core.DatabaseClient) HashSet(java.util.HashSet) BDDMockito.given(org.mockito.BDDMockito.given) Duration(java.time.Duration) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Result(io.r2dbc.spi.Result) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) TestClientRegistrations(org.springframework.security.oauth2.client.registration.TestClientRegistrations) ReactiveClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository) Mono(reactor.core.publisher.Mono) CompositeDatabasePopulator(org.springframework.r2dbc.connection.init.CompositeDatabasePopulator) Instant(java.time.Instant) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) ResourceDatabasePopulator(org.springframework.r2dbc.connection.init.ResourceDatabasePopulator) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) H2ConnectionFactory(io.r2dbc.h2.H2ConnectionFactory) Authentication(org.springframework.security.core.Authentication) OAuth2RefreshToken(org.springframework.security.oauth2.core.OAuth2RefreshToken) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Authentication(org.springframework.security.core.Authentication) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 97 with OAuth2AuthorizedClient

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

the class R2dbcReactiveOAuth2AuthorizedClientServiceTests method loadAuthorizedClientWhenExistsThenReturnAuthorizedClient.

@Test
public void loadAuthorizedClientWhenExistsThenReturnAuthorizedClient() {
    Authentication principal = createPrincipal();
    OAuth2AuthorizedClient expected = createAuthorizedClient(principal, this.clientRegistration);
    this.authorizedClientService.saveAuthorizedClient(expected, principal).as(StepVerifier::create).verifyComplete();
    this.authorizedClientService.loadAuthorizedClient(this.clientRegistration.getRegistrationId(), principal.getName()).as(StepVerifier::create).assertNext((authorizedClient) -> {
        assertThat(authorizedClient).isNotNull();
        assertThat(authorizedClient.getClientRegistration()).isEqualTo(expected.getClientRegistration());
        assertThat(authorizedClient.getPrincipalName()).isEqualTo(expected.getPrincipalName());
        assertThat(authorizedClient.getAccessToken().getTokenType()).isEqualTo(expected.getAccessToken().getTokenType());
        assertThat(authorizedClient.getAccessToken().getTokenValue()).isEqualTo(expected.getAccessToken().getTokenValue());
        assertThat(authorizedClient.getAccessToken().getIssuedAt()).isEqualTo(expected.getAccessToken().getIssuedAt());
        assertThat(authorizedClient.getAccessToken().getExpiresAt()).isEqualTo(expected.getAccessToken().getExpiresAt());
        assertThat(authorizedClient.getAccessToken().getScopes()).isEqualTo(expected.getAccessToken().getScopes());
        assertThat(authorizedClient.getRefreshToken().getTokenValue()).isEqualTo(expected.getRefreshToken().getTokenValue());
        assertThat(authorizedClient.getRefreshToken().getIssuedAt()).isEqualTo(expected.getRefreshToken().getIssuedAt());
    }).verifyComplete();
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ClassPathResource(org.springframework.core.io.ClassPathResource) ConnectionFactoryInitializer(org.springframework.r2dbc.connection.init.ConnectionFactoryInitializer) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) DatabaseClient(org.springframework.r2dbc.core.DatabaseClient) HashSet(java.util.HashSet) BDDMockito.given(org.mockito.BDDMockito.given) Duration(java.time.Duration) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Result(io.r2dbc.spi.Result) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) TestClientRegistrations(org.springframework.security.oauth2.client.registration.TestClientRegistrations) ReactiveClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository) Mono(reactor.core.publisher.Mono) CompositeDatabasePopulator(org.springframework.r2dbc.connection.init.CompositeDatabasePopulator) Instant(java.time.Instant) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) ResourceDatabasePopulator(org.springframework.r2dbc.connection.init.ResourceDatabasePopulator) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) H2ConnectionFactory(io.r2dbc.h2.H2ConnectionFactory) Authentication(org.springframework.security.core.Authentication) OAuth2RefreshToken(org.springframework.security.oauth2.core.OAuth2RefreshToken) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Authentication(org.springframework.security.core.Authentication) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 98 with OAuth2AuthorizedClient

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

the class RefreshTokenReactiveOAuth2AuthorizedClientProviderTests method authorizeWhenAuthorizedAndAccessTokenExpiredThenReauthorize.

@Test
public void authorizeWhenAuthorizedAndAccessTokenExpiredThenReauthorize() {
    OAuth2AccessTokenResponse accessTokenResponse = TestOAuth2AccessTokenResponses.accessTokenResponse().refreshToken("new-refresh-token").build();
    given(this.accessTokenResponseClient.getTokenResponse(any())).willReturn(Mono.just(accessTokenResponse));
    OAuth2AuthorizationContext authorizationContext = OAuth2AuthorizationContext.withAuthorizedClient(this.authorizedClient).principal(this.principal).build();
    OAuth2AuthorizedClient reauthorizedClient = this.authorizedClientProvider.authorize(authorizationContext).block();
    assertThat(reauthorizedClient.getClientRegistration()).isSameAs(this.clientRegistration);
    assertThat(reauthorizedClient.getPrincipalName()).isEqualTo(this.principal.getName());
    assertThat(reauthorizedClient.getAccessToken()).isEqualTo(accessTokenResponse.getAccessToken());
    assertThat(reauthorizedClient.getRefreshToken()).isEqualTo(accessTokenResponse.getRefreshToken());
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) Test(org.junit.jupiter.api.Test)

Example 99 with OAuth2AuthorizedClient

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

the class RefreshTokenReactiveOAuth2AuthorizedClientProviderTests method setup.

@BeforeEach
public void setup() {
    this.authorizedClientProvider = new RefreshTokenReactiveOAuth2AuthorizedClientProvider();
    this.accessTokenResponseClient = mock(ReactiveOAuth2AccessTokenResponseClient.class);
    this.authorizedClientProvider.setAccessTokenResponseClient(this.accessTokenResponseClient);
    this.clientRegistration = TestClientRegistrations.clientRegistration().build();
    this.principal = new TestingAuthenticationToken("principal", "password");
    Instant issuedAt = Instant.now().minus(Duration.ofDays(1));
    Instant expiresAt = issuedAt.plus(Duration.ofMinutes(60));
    OAuth2AccessToken expiredAccessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "access-token-1234", issuedAt, expiresAt);
    this.authorizedClient = new OAuth2AuthorizedClient(this.clientRegistration, this.principal.getName(), expiredAccessToken, TestOAuth2RefreshTokens.refreshToken());
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) Instant(java.time.Instant) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) ReactiveOAuth2AccessTokenResponseClient(org.springframework.security.oauth2.client.endpoint.ReactiveOAuth2AccessTokenResponseClient) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 100 with OAuth2AuthorizedClient

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

the class RefreshTokenOAuth2AuthorizedClientProviderTests method authorizeWhenAuthorizedAndAccessTokenNotExpiredButClockSkewForcesExpiryThenReauthorize.

// gh-7511
@Test
public void authorizeWhenAuthorizedAndAccessTokenNotExpiredButClockSkewForcesExpiryThenReauthorize() {
    OAuth2AccessTokenResponse accessTokenResponse = TestOAuth2AccessTokenResponses.accessTokenResponse().refreshToken("new-refresh-token").build();
    given(this.accessTokenResponseClient.getTokenResponse(any())).willReturn(accessTokenResponse);
    Instant now = Instant.now();
    Instant issuedAt = now.minus(Duration.ofMinutes(60));
    Instant expiresAt = now.minus(Duration.ofMinutes(1));
    OAuth2AccessToken expiresInOneMinAccessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "access-token-1234", issuedAt, expiresAt);
    OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.clientRegistration, this.principal.getName(), expiresInOneMinAccessToken, this.authorizedClient.getRefreshToken());
    // Shorten the lifespan of the access token by 90 seconds, which will ultimately
    // force it to expire on the client
    this.authorizedClientProvider.setClockSkew(Duration.ofSeconds(90));
    // @formatter:off
    OAuth2AuthorizationContext authorizationContext = OAuth2AuthorizationContext.withAuthorizedClient(authorizedClient).principal(this.principal).build();
    // @formatter:on
    OAuth2AuthorizedClient reauthorizedClient = this.authorizedClientProvider.authorize(authorizationContext);
    assertThat(reauthorizedClient.getClientRegistration()).isSameAs(this.clientRegistration);
    assertThat(reauthorizedClient.getPrincipalName()).isEqualTo(this.principal.getName());
    assertThat(reauthorizedClient.getAccessToken()).isEqualTo(accessTokenResponse.getAccessToken());
    assertThat(reauthorizedClient.getRefreshToken()).isEqualTo(accessTokenResponse.getRefreshToken());
}
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