use of org.springframework.security.oauth2.core.OAuth2RefreshToken in project spring-security by spring-projects.
the class ServletOAuth2AuthorizedClientExchangeFilterFunctionTests method filterWhenRefreshRequiredThenRefresh.
@Test
public void filterWhenRefreshRequiredThenRefresh() {
OAuth2AccessTokenResponse response = OAuth2AccessTokenResponse.withToken("token-1").tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(3600).refreshToken("refresh-1").build();
given(this.refreshTokenTokenResponseClient.getTokenResponse(any())).willReturn(response);
Instant issuedAt = Instant.now().minus(Duration.ofDays(1));
Instant accessTokenExpiresAt = issuedAt.plus(Duration.ofHours(1));
this.accessToken = new OAuth2AccessToken(this.accessToken.getTokenType(), this.accessToken.getTokenValue(), issuedAt, accessTokenExpiresAt);
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", issuedAt);
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration, "principalName", this.accessToken, refreshToken);
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com")).attributes(ServletOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient(authorizedClient)).attributes(ServletOAuth2AuthorizedClientExchangeFilterFunction.authentication(this.authentication)).attributes(ServletOAuth2AuthorizedClientExchangeFilterFunction.httpServletRequest(new MockHttpServletRequest())).attributes(ServletOAuth2AuthorizedClientExchangeFilterFunction.httpServletResponse(new MockHttpServletResponse())).build();
this.function.filter(request, this.exchange).block();
verify(this.refreshTokenTokenResponseClient).getTokenResponse(any());
verify(this.authorizedClientRepository).saveAuthorizedClient(this.authorizedClientCaptor.capture(), eq(this.authentication), any(), any());
OAuth2AuthorizedClient newAuthorizedClient = this.authorizedClientCaptor.getValue();
assertThat(newAuthorizedClient.getAccessToken()).isEqualTo(response.getAccessToken());
assertThat(newAuthorizedClient.getRefreshToken()).isEqualTo(response.getRefreshToken());
List<ClientRequest> requests = this.exchange.getRequests();
assertThat(requests).hasSize(1);
ClientRequest request0 = requests.get(0);
assertThat(request0.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Bearer token-1");
assertThat(request0.url().toASCIIString()).isEqualTo("https://example.com");
assertThat(request0.method()).isEqualTo(HttpMethod.GET);
assertThat(getBody(request0)).isEmpty();
}
use of org.springframework.security.oauth2.core.OAuth2RefreshToken in project spring-security by spring-projects.
the class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests method filterWhenRefreshRequiredThenRefresh.
@Test
public void filterWhenRefreshRequiredThenRefresh() {
setupMocks();
OAuth2AccessTokenResponse response = OAuth2AccessTokenResponse.withToken("token-1").tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(3600).refreshToken("refresh-1").build();
given(this.refreshTokenTokenResponseClient.getTokenResponse(any())).willReturn(Mono.just(response));
Instant issuedAt = Instant.now().minus(Duration.ofDays(1));
Instant accessTokenExpiresAt = issuedAt.plus(Duration.ofHours(1));
this.accessToken = new OAuth2AccessToken(this.accessToken.getTokenType(), this.accessToken.getTokenValue(), issuedAt, accessTokenExpiresAt);
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken("refresh-token", issuedAt);
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.registration, "principalName", this.accessToken, refreshToken);
// @formatter:off
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com")).attributes(ServerOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient(authorizedClient)).build();
// @formatter:on
TestingAuthenticationToken authentication = new TestingAuthenticationToken("test", "this");
// @formatter:off
this.function.filter(request, this.exchange).subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)).subscriberContext(serverWebExchange()).block();
// @formatter:on
verify(this.refreshTokenTokenResponseClient).getTokenResponse(any());
verify(this.authorizedClientRepository).saveAuthorizedClient(this.authorizedClientCaptor.capture(), eq(authentication), any());
OAuth2AuthorizedClient newAuthorizedClient = this.authorizedClientCaptor.getValue();
assertThat(newAuthorizedClient.getAccessToken()).isEqualTo(response.getAccessToken());
assertThat(newAuthorizedClient.getRefreshToken()).isEqualTo(response.getRefreshToken());
List<ClientRequest> requests = this.exchange.getRequests();
assertThat(requests).hasSize(1);
ClientRequest request0 = requests.get(0);
assertThat(request0.headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Bearer token-1");
assertThat(request0.url().toASCIIString()).isEqualTo("https://example.com");
assertThat(request0.method()).isEqualTo(HttpMethod.GET);
assertThat(getBody(request0)).isEmpty();
}
use of org.springframework.security.oauth2.core.OAuth2RefreshToken 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);
}
use of org.springframework.security.oauth2.core.OAuth2RefreshToken in project spring-security by spring-projects.
the class DefaultMapOAuth2AccessTokenResponseConverterTests method shouldConvertFull.
@Test
public void shouldConvertFull() {
Map<String, Object> map = new HashMap<>();
map.put("access_token", "access-token-1234");
map.put("token_type", "bearer");
map.put("expires_in", "3600");
map.put("scope", "read write");
map.put("refresh_token", "refresh-token-1234");
map.put("custom_parameter_1", "custom-value-1");
map.put("custom_parameter_2", "custom-value-2");
OAuth2AccessTokenResponse converted = this.messageConverter.convert(map);
OAuth2AccessToken accessToken = converted.getAccessToken();
Assertions.assertNotNull(accessToken);
Assertions.assertEquals("access-token-1234", accessToken.getTokenValue());
Assertions.assertEquals(OAuth2AccessToken.TokenType.BEARER, accessToken.getTokenType());
Set<String> scopes = accessToken.getScopes();
Assertions.assertNotNull(scopes);
Assertions.assertEquals(2, scopes.size());
Assertions.assertTrue(scopes.contains("read"));
Assertions.assertTrue(scopes.contains("write"));
Assertions.assertEquals(3600, Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds());
OAuth2RefreshToken refreshToken = converted.getRefreshToken();
Assertions.assertNotNull(refreshToken);
Assertions.assertEquals("refresh-token-1234", refreshToken.getTokenValue());
Map<String, Object> additionalParameters = converted.getAdditionalParameters();
Assertions.assertNotNull(additionalParameters);
Assertions.assertEquals(2, additionalParameters.size());
Assertions.assertEquals("custom-value-1", additionalParameters.get("custom_parameter_1"));
Assertions.assertEquals("custom-value-2", additionalParameters.get("custom_parameter_2"));
}
use of org.springframework.security.oauth2.core.OAuth2RefreshToken in project spring-security by spring-projects.
the class DefaultMapOAuth2AccessTokenResponseConverterTests method shouldConvertWithObjectAdditionalParameter.
// gh-9685
@Test
public void shouldConvertWithObjectAdditionalParameter() {
Map<String, Object> map = new HashMap<>();
map.put("access_token", "access-token-1234");
map.put("token_type", "bearer");
map.put("expires_in", "3600");
map.put("scope", "read write");
map.put("refresh_token", "refresh-token-1234");
Map<String, Object> nestedObject = new LinkedHashMap<>();
nestedObject.put("a", "first value");
nestedObject.put("b", "second value");
map.put("custom_parameter_1", nestedObject);
map.put("custom_parameter_2", "custom-value-2");
OAuth2AccessTokenResponse converted = this.messageConverter.convert(map);
OAuth2AccessToken accessToken = converted.getAccessToken();
Assertions.assertNotNull(accessToken);
Assertions.assertEquals("access-token-1234", accessToken.getTokenValue());
Assertions.assertEquals(OAuth2AccessToken.TokenType.BEARER, accessToken.getTokenType());
Set<String> scopes = accessToken.getScopes();
Assertions.assertNotNull(scopes);
Assertions.assertEquals(2, scopes.size());
Assertions.assertTrue(scopes.contains("read"));
Assertions.assertTrue(scopes.contains("write"));
Assertions.assertEquals(3600, Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds());
OAuth2RefreshToken refreshToken = converted.getRefreshToken();
Assertions.assertNotNull(refreshToken);
Assertions.assertEquals("refresh-token-1234", refreshToken.getTokenValue());
Map<String, Object> additionalParameters = converted.getAdditionalParameters();
Assertions.assertNotNull(additionalParameters);
Assertions.assertEquals(2, additionalParameters.size());
Assertions.assertEquals(nestedObject, additionalParameters.get("custom_parameter_1"));
Assertions.assertEquals("custom-value-2", additionalParameters.get("custom_parameter_2"));
}
Aggregations