use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.
the class DefaultReactiveOAuth2AuthorizedClientManagerTests method reauthorizeWhenUnsupportedProviderThenNotReauthorized.
@SuppressWarnings("unchecked")
@Test
public void reauthorizeWhenUnsupportedProviderThenNotReauthorized() {
OAuth2AuthorizeRequest reauthorizeRequest = OAuth2AuthorizeRequest.withAuthorizedClient(this.authorizedClient).principal(this.principal).build();
OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(reauthorizeRequest).subscriberContext(this.context).block();
verify(this.authorizedClientProvider).authorize(this.authorizationContextCaptor.capture());
verify(this.contextAttributesMapper).apply(eq(reauthorizeRequest));
OAuth2AuthorizationContext authorizationContext = this.authorizationContextCaptor.getValue();
assertThat(authorizationContext.getClientRegistration()).isEqualTo(this.clientRegistration);
assertThat(authorizationContext.getAuthorizedClient()).isSameAs(this.authorizedClient);
assertThat(authorizationContext.getPrincipal()).isEqualTo(this.principal);
assertThat(authorizedClient).isSameAs(this.authorizedClient);
this.saveAuthorizedClientProbe.assertWasNotSubscribed();
}
use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.
the class DefaultReactiveOAuth2AuthorizedClientManagerTests method authorizeWhenNotAuthorizedAndSupportedProviderThenAuthorized.
@SuppressWarnings("unchecked")
@Test
public void authorizeWhenNotAuthorizedAndSupportedProviderThenAuthorized() {
given(this.clientRegistrationRepository.findByRegistrationId(eq(this.clientRegistration.getRegistrationId()))).willReturn(Mono.just(this.clientRegistration));
given(this.authorizedClientProvider.authorize(any(OAuth2AuthorizationContext.class))).willReturn(Mono.just(this.authorizedClient));
OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId(this.clientRegistration.getRegistrationId()).principal(this.principal).build();
OAuth2AuthorizedClient authorizedClient = this.authorizedClientManager.authorize(authorizeRequest).subscriberContext(this.context).block();
verify(this.authorizedClientProvider).authorize(this.authorizationContextCaptor.capture());
verify(this.contextAttributesMapper).apply(eq(authorizeRequest));
OAuth2AuthorizationContext authorizationContext = this.authorizationContextCaptor.getValue();
assertThat(authorizationContext.getClientRegistration()).isEqualTo(this.clientRegistration);
assertThat(authorizationContext.getAuthorizedClient()).isNull();
assertThat(authorizationContext.getPrincipal()).isEqualTo(this.principal);
assertThat(authorizedClient).isSameAs(this.authorizedClient);
verify(this.authorizedClientRepository).saveAuthorizedClient(eq(this.authorizedClient), eq(this.principal), eq(this.serverWebExchange));
this.saveAuthorizedClientProbe.assertWasSubscribed();
verify(this.authorizedClientRepository, never()).removeAuthorizedClient(any(), any(), any());
}
use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.
the class R2dbcReactiveOAuth2AuthorizedClientServiceTests method createAuthorizedClient.
private static OAuth2AuthorizedClient createAuthorizedClient(Authentication principal, ClientRegistration clientRegistration, boolean requiredAttributesOnly) {
Instant issuedAt = Instant.ofEpochSecond(1234567890, 123456000);
OAuth2AccessToken accessToken;
if (!requiredAttributesOnly) {
accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "scopes", issuedAt, issuedAt.plus(Duration.ofDays(1)), new HashSet<>(Arrays.asList("read", "write")));
} else {
accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "no-scopes", issuedAt, issuedAt.plus(Duration.ofDays(1)));
}
OAuth2RefreshToken refreshToken = null;
if (!requiredAttributesOnly) {
refreshToken = new OAuth2RefreshToken("refresh-token", issuedAt);
}
return new OAuth2AuthorizedClient(clientRegistration, principal.getName(), accessToken, refreshToken);
}
use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.
the class RefreshTokenOAuth2AuthorizedClientProviderTests method authorizeWhenAuthorizedAndAccessTokenExpiredThenReauthorize.
@Test
public void authorizeWhenAuthorizedAndAccessTokenExpiredThenReauthorize() {
// @formatter:off
OAuth2AccessTokenResponse accessTokenResponse = TestOAuth2AccessTokenResponses.accessTokenResponse().refreshToken("new-refresh-token").build();
// @formatter:on
given(this.accessTokenResponseClient.getTokenResponse(any())).willReturn(accessTokenResponse);
// @formatter:off
OAuth2AuthorizationContext authorizationContext = OAuth2AuthorizationContext.withAuthorizedClient(this.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());
}
use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.
the class RefreshTokenOAuth2AuthorizedClientProviderTests method setup.
@BeforeEach
public void setup() {
this.authorizedClientProvider = new RefreshTokenOAuth2AuthorizedClientProvider();
this.accessTokenResponseClient = mock(OAuth2AccessTokenResponseClient.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());
}
Aggregations