2020-10-07 16:53:37 +07:00
|
|
|
/*
|
2021-01-15 17:57:15 +07:00
|
|
|
* Copyright 2020-2021 the original author or authors.
|
2020-10-07 16:53:37 +07:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.springframework.security.config.annotation.web.configurers.oauth2.server.authorization;
|
|
|
|
|
2021-01-15 17:57:15 +07:00
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.Base64;
|
2021-01-18 21:31:06 +07:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.stream.Collectors;
|
2021-01-15 17:57:15 +07:00
|
|
|
|
|
|
|
import com.nimbusds.jose.jwk.JWKSet;
|
|
|
|
import com.nimbusds.jose.jwk.source.JWKSource;
|
|
|
|
import com.nimbusds.jose.proc.SecurityContext;
|
2020-10-07 16:53:37 +07:00
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
import org.junit.Rule;
|
|
|
|
import org.junit.Test;
|
2021-01-15 17:57:15 +07:00
|
|
|
|
2020-10-07 16:53:37 +07:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Import;
|
2020-10-31 01:22:47 +07:00
|
|
|
import org.springframework.http.HttpHeaders;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
|
|
import org.springframework.mock.http.client.MockClientHttpResponse;
|
|
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
2020-10-07 16:53:37 +07:00
|
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
|
|
import org.springframework.security.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration;
|
|
|
|
import org.springframework.security.config.test.SpringTestRule;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
2020-10-07 16:53:37 +07:00
|
|
|
import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
|
2020-10-07 16:53:37 +07:00
|
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter;
|
2021-01-15 17:57:15 +07:00
|
|
|
import org.springframework.security.oauth2.jose.TestJwks;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.oauth2.jose.TestKeys;
|
|
|
|
import org.springframework.security.oauth2.jwt.Jwt;
|
|
|
|
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
|
2020-10-07 16:53:37 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationAttributeNames;
|
2020-10-07 16:53:37 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.TestOAuth2Authorizations;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.TokenType;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.token.JwtEncodingContext;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenCustomizer;
|
2020-10-07 16:53:37 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.web.OAuth2TokenEndpointFilter;
|
|
|
|
import org.springframework.test.web.servlet.MockMvc;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.test.web.servlet.MvcResult;
|
2020-10-31 01:22:47 +07:00
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
import org.springframework.util.MultiValueMap;
|
2020-10-07 16:53:37 +07:00
|
|
|
|
2021-01-18 21:31:06 +07:00
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
2020-10-31 01:22:47 +07:00
|
|
|
import static org.hamcrest.CoreMatchers.containsString;
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
2020-10-07 16:53:37 +07:00
|
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.reset;
|
2020-10-31 01:22:47 +07:00
|
|
|
import static org.mockito.Mockito.verify;
|
2020-10-07 16:53:37 +07:00
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
2020-10-31 01:22:47 +07:00
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
2020-10-07 16:53:37 +07:00
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
|
|
|
|
|
/**
|
2020-10-31 01:22:47 +07:00
|
|
|
* Integration tests for the OAuth 2.0 Refresh Token Grant.
|
|
|
|
*
|
2020-10-07 16:53:37 +07:00
|
|
|
* @author Alexey Nesterov
|
|
|
|
* @since 0.0.3
|
|
|
|
*/
|
|
|
|
public class OAuth2RefreshTokenGrantTests {
|
2021-01-18 21:31:06 +07:00
|
|
|
private static final String AUTHORITIES_CLAIM = "authorities";
|
2020-10-07 16:53:37 +07:00
|
|
|
private static RegisteredClientRepository registeredClientRepository;
|
|
|
|
private static OAuth2AuthorizationService authorizationService;
|
2021-01-15 17:57:15 +07:00
|
|
|
private static JWKSource<SecurityContext> jwkSource;
|
2021-01-18 21:31:06 +07:00
|
|
|
private static NimbusJwtDecoder jwtDecoder;
|
|
|
|
private static HttpMessageConverter<OAuth2AccessTokenResponse> accessTokenHttpResponseConverter =
|
|
|
|
new OAuth2AccessTokenResponseHttpMessageConverter();
|
2020-10-07 16:53:37 +07:00
|
|
|
|
|
|
|
@Rule
|
|
|
|
public final SpringTestRule spring = new SpringTestRule();
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private MockMvc mvc;
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
public static void init() {
|
|
|
|
registeredClientRepository = mock(RegisteredClientRepository.class);
|
|
|
|
authorizationService = mock(OAuth2AuthorizationService.class);
|
2021-01-15 17:57:15 +07:00
|
|
|
JWKSet jwkSet = new JWKSet(TestJwks.DEFAULT_RSA_JWK);
|
|
|
|
jwkSource = (jwkSelector, securityContext) -> jwkSelector.select(jwkSet);
|
2021-01-18 21:31:06 +07:00
|
|
|
jwtDecoder = NimbusJwtDecoder.withPublicKey(TestKeys.DEFAULT_PUBLIC_KEY).build();
|
2020-10-07 16:53:37 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setup() {
|
|
|
|
reset(registeredClientRepository);
|
|
|
|
reset(authorizationService);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-31 01:22:47 +07:00
|
|
|
public void requestWhenRefreshTokenRequestValidThenReturnAccessTokenResponse() throws Exception {
|
|
|
|
this.spring.register(AuthorizationServerConfiguration.class).autowire();
|
|
|
|
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
|
|
|
|
when(authorizationService.findByToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
eq(authorization.getRefreshToken().getToken().getTokenValue()),
|
2020-10-31 01:22:47 +07:00
|
|
|
eq(TokenType.REFRESH_TOKEN)))
|
2020-10-07 16:53:37 +07:00
|
|
|
.thenReturn(authorization);
|
|
|
|
|
2021-01-18 21:31:06 +07:00
|
|
|
MvcResult mvcResult = this.mvc.perform(post(OAuth2TokenEndpointFilter.DEFAULT_TOKEN_ENDPOINT_URI)
|
2020-10-31 01:22:47 +07:00
|
|
|
.params(getRefreshTokenRequestParameters(authorization))
|
|
|
|
.header(HttpHeaders.AUTHORIZATION, "Basic " + encodeBasicAuth(
|
|
|
|
registeredClient.getClientId(), registeredClient.getClientSecret())))
|
2020-10-07 16:53:37 +07:00
|
|
|
.andExpect(status().isOk())
|
2020-10-31 01:22:47 +07:00
|
|
|
.andExpect(header().string(HttpHeaders.CACHE_CONTROL, containsString("no-store")))
|
|
|
|
.andExpect(header().string(HttpHeaders.PRAGMA, containsString("no-cache")))
|
|
|
|
.andExpect(jsonPath("$.access_token").isNotEmpty())
|
|
|
|
.andExpect(jsonPath("$.token_type").isNotEmpty())
|
|
|
|
.andExpect(jsonPath("$.expires_in").isNotEmpty())
|
|
|
|
.andExpect(jsonPath("$.refresh_token").isNotEmpty())
|
2021-01-18 21:31:06 +07:00
|
|
|
.andExpect(jsonPath("$.scope").isNotEmpty())
|
|
|
|
.andReturn();
|
2020-10-31 01:22:47 +07:00
|
|
|
|
|
|
|
verify(registeredClientRepository).findByClientId(eq(registeredClient.getClientId()));
|
|
|
|
verify(authorizationService).findByToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
eq(authorization.getRefreshToken().getToken().getTokenValue()),
|
2020-10-31 01:22:47 +07:00
|
|
|
eq(TokenType.REFRESH_TOKEN));
|
|
|
|
verify(authorizationService).save(any());
|
|
|
|
|
2021-01-18 21:31:06 +07:00
|
|
|
MockHttpServletResponse servletResponse = mvcResult.getResponse();
|
|
|
|
MockClientHttpResponse httpResponse = new MockClientHttpResponse(
|
|
|
|
servletResponse.getContentAsByteArray(), HttpStatus.valueOf(servletResponse.getStatus()));
|
|
|
|
OAuth2AccessTokenResponse accessTokenResponse = accessTokenHttpResponseConverter.read(
|
|
|
|
OAuth2AccessTokenResponse.class, httpResponse);
|
|
|
|
|
|
|
|
// Assert user authorities was propagated as claim in JWT
|
|
|
|
Jwt jwt = jwtDecoder.decode(accessTokenResponse.getAccessToken().getTokenValue());
|
|
|
|
List<String> authoritiesClaim = jwt.getClaim(AUTHORITIES_CLAIM);
|
|
|
|
Authentication principal = authorization.getAttribute(OAuth2AuthorizationAttributeNames.PRINCIPAL);
|
|
|
|
Set<String> userAuthorities = principal.getAuthorities().stream()
|
|
|
|
.map(GrantedAuthority::getAuthority)
|
|
|
|
.collect(Collectors.toSet());
|
|
|
|
assertThat(authoritiesClaim).containsExactlyInAnyOrderElementsOf(userAuthorities);
|
2020-10-31 01:22:47 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
private static MultiValueMap<String, String> getRefreshTokenRequestParameters(OAuth2Authorization authorization) {
|
|
|
|
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
|
|
|
|
parameters.set(OAuth2ParameterNames.GRANT_TYPE, AuthorizationGrantType.REFRESH_TOKEN.getValue());
|
2021-02-06 01:20:17 +07:00
|
|
|
parameters.set(OAuth2ParameterNames.REFRESH_TOKEN, authorization.getRefreshToken().getToken().getTokenValue());
|
2020-10-31 01:22:47 +07:00
|
|
|
return parameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String encodeBasicAuth(String clientId, String secret) throws Exception {
|
|
|
|
clientId = URLEncoder.encode(clientId, StandardCharsets.UTF_8.name());
|
|
|
|
secret = URLEncoder.encode(secret, StandardCharsets.UTF_8.name());
|
|
|
|
String credentialsString = clientId + ":" + secret;
|
|
|
|
byte[] encodedBytes = Base64.getEncoder().encode(credentialsString.getBytes(StandardCharsets.UTF_8));
|
|
|
|
return new String(encodedBytes, StandardCharsets.UTF_8);
|
2020-10-07 16:53:37 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@EnableWebSecurity
|
|
|
|
@Import(OAuth2AuthorizationServerConfiguration.class)
|
|
|
|
static class AuthorizationServerConfiguration {
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
RegisteredClientRepository registeredClientRepository() {
|
|
|
|
return registeredClientRepository;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
OAuth2AuthorizationService authorizationService() {
|
|
|
|
return authorizationService;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
2021-01-15 17:57:15 +07:00
|
|
|
JWKSource<SecurityContext> jwkSource() {
|
|
|
|
return jwkSource;
|
2020-10-26 06:58:29 +07:00
|
|
|
}
|
2021-01-18 21:31:06 +07:00
|
|
|
|
|
|
|
@Bean
|
|
|
|
OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer() {
|
|
|
|
return context -> {
|
|
|
|
if (AuthorizationGrantType.REFRESH_TOKEN.equals(context.getAuthorizationGrantType())) {
|
|
|
|
Authentication principal = context.getPrincipal();
|
|
|
|
Set<String> authorities = principal.getAuthorities().stream()
|
|
|
|
.map(GrantedAuthority::getAuthority)
|
|
|
|
.collect(Collectors.toSet());
|
|
|
|
context.getClaims().claim(AUTHORITIES_CLAIM, authorities);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-10-07 16:53:37 +07:00
|
|
|
}
|
|
|
|
}
|