|
@@ -15,25 +15,59 @@
|
|
|
*/
|
|
|
package org.springframework.security.config.annotation.web.configurers.oauth2.server.authorization;
|
|
|
|
|
|
+import org.junit.Before;
|
|
|
+import org.junit.BeforeClass;
|
|
|
import org.junit.Rule;
|
|
|
import org.junit.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Import;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
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;
|
|
|
import org.springframework.security.crypto.key.CryptoKeySource;
|
|
|
+import org.springframework.security.crypto.key.StaticKeyGeneratingCryptoKeySource;
|
|
|
+import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
|
|
+import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponseType;
|
|
|
+import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
|
|
+import org.springframework.security.oauth2.core.oidc.OidcScopes;
|
|
|
+import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
|
|
+import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
|
|
+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;
|
|
|
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings;
|
|
|
+import org.springframework.security.oauth2.server.authorization.token.OAuth2AuthorizationCode;
|
|
|
+import org.springframework.security.oauth2.server.authorization.web.OAuth2AuthorizationEndpointFilter;
|
|
|
+import org.springframework.security.oauth2.server.authorization.web.OAuth2TokenEndpointFilter;
|
|
|
import org.springframework.security.oauth2.server.authorization.web.OidcProviderConfigurationEndpointFilter;
|
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
|
import org.springframework.test.web.servlet.MvcResult;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Base64;
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
|
+import static org.hamcrest.CoreMatchers.containsString;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.reset;
|
|
|
+import static org.mockito.Mockito.times;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
|
|
@@ -44,6 +78,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|
|
*/
|
|
|
public class OidcTests {
|
|
|
private static final String issuerUrl = "https://example.com/issuer1";
|
|
|
+ private static RegisteredClientRepository registeredClientRepository;
|
|
|
+ private static OAuth2AuthorizationService authorizationService;
|
|
|
+ private static CryptoKeySource keySource;
|
|
|
|
|
|
@Rule
|
|
|
public final SpringTestRule spring = new SpringTestRule();
|
|
@@ -51,14 +88,26 @@ public class OidcTests {
|
|
|
@Autowired
|
|
|
private MockMvc mvc;
|
|
|
|
|
|
+ @BeforeClass
|
|
|
+ public static void init() {
|
|
|
+ registeredClientRepository = mock(RegisteredClientRepository.class);
|
|
|
+ authorizationService = mock(OAuth2AuthorizationService.class);
|
|
|
+ keySource = new StaticKeyGeneratingCryptoKeySource();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void setup() {
|
|
|
+ reset(registeredClientRepository);
|
|
|
+ reset(authorizationService);
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void requestWhenConfigurationRequestAndIssuerSetThenReturnConfigurationResponse() throws Exception {
|
|
|
this.spring.register(AuthorizationServerConfigurationWithIssuer.class).autowire();
|
|
|
|
|
|
this.mvc.perform(get(OidcProviderConfigurationEndpointFilter.DEFAULT_OIDC_PROVIDER_CONFIGURATION_ENDPOINT_URI))
|
|
|
.andExpect(status().is2xxSuccessful())
|
|
|
- .andExpect(jsonPath("issuer").value(issuerUrl))
|
|
|
- .andReturn();
|
|
|
+ .andExpect(jsonPath("issuer").value(issuerUrl));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -85,18 +134,98 @@ public class OidcTests {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void requestWhenAuthenticationRequestThenTokenResponseIncludesIdToken() throws Exception {
|
|
|
+ this.spring.register(AuthorizationServerConfiguration.class).autowire();
|
|
|
+
|
|
|
+ RegisteredClient registeredClient = TestRegisteredClients.registeredClient().scope(OidcScopes.OPENID).build();
|
|
|
+ when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .thenReturn(registeredClient);
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get(OAuth2AuthorizationEndpointFilter.DEFAULT_AUTHORIZATION_ENDPOINT_URI)
|
|
|
+ .params(getAuthorizationRequestParameters(registeredClient))
|
|
|
+ .with(user("user")))
|
|
|
+ .andExpect(status().is3xxRedirection())
|
|
|
+ .andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getRedirectedUrl()).matches("https://example.com\\?code=.{15,}&state=state");
|
|
|
+
|
|
|
+ verify(registeredClientRepository).findByClientId(eq(registeredClient.getClientId()));
|
|
|
+
|
|
|
+ ArgumentCaptor<OAuth2Authorization> authorizationCaptor = ArgumentCaptor.forClass(OAuth2Authorization.class);
|
|
|
+ verify(authorizationService).save(authorizationCaptor.capture());
|
|
|
+ OAuth2Authorization authorization = authorizationCaptor.getValue();
|
|
|
+
|
|
|
+ when(authorizationService.findByToken(
|
|
|
+ eq(authorization.getTokens().getToken(OAuth2AuthorizationCode.class).getTokenValue()),
|
|
|
+ eq(TokenType.AUTHORIZATION_CODE)))
|
|
|
+ .thenReturn(authorization);
|
|
|
+
|
|
|
+ this.mvc.perform(post(OAuth2TokenEndpointFilter.DEFAULT_TOKEN_ENDPOINT_URI)
|
|
|
+ .params(getTokenRequestParameters(registeredClient, authorization))
|
|
|
+ .header(HttpHeaders.AUTHORIZATION, "Basic " + encodeBasicAuth(
|
|
|
+ registeredClient.getClientId(), registeredClient.getClientSecret())))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .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())
|
|
|
+ .andExpect(jsonPath("$.scope").isNotEmpty())
|
|
|
+ .andExpect(jsonPath("$.id_token").isNotEmpty());
|
|
|
+
|
|
|
+ verify(registeredClientRepository, times(2)).findByClientId(eq(registeredClient.getClientId()));
|
|
|
+ verify(authorizationService).findByToken(
|
|
|
+ eq(authorization.getTokens().getToken(OAuth2AuthorizationCode.class).getTokenValue()),
|
|
|
+ eq(TokenType.AUTHORIZATION_CODE));
|
|
|
+ verify(authorizationService, times(2)).save(any());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static MultiValueMap<String, String> getAuthorizationRequestParameters(RegisteredClient registeredClient) {
|
|
|
+ MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
|
|
|
+ parameters.set(OAuth2ParameterNames.RESPONSE_TYPE, OAuth2AuthorizationResponseType.CODE.getValue());
|
|
|
+ parameters.set(OAuth2ParameterNames.CLIENT_ID, registeredClient.getClientId());
|
|
|
+ parameters.set(OAuth2ParameterNames.REDIRECT_URI, registeredClient.getRedirectUris().iterator().next());
|
|
|
+ parameters.set(OAuth2ParameterNames.SCOPE,
|
|
|
+ StringUtils.collectionToDelimitedString(registeredClient.getScopes(), " "));
|
|
|
+ parameters.set(OAuth2ParameterNames.STATE, "state");
|
|
|
+ return parameters;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static MultiValueMap<String, String> getTokenRequestParameters(RegisteredClient registeredClient,
|
|
|
+ OAuth2Authorization authorization) {
|
|
|
+ MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
|
|
|
+ parameters.set(OAuth2ParameterNames.GRANT_TYPE, AuthorizationGrantType.AUTHORIZATION_CODE.getValue());
|
|
|
+ parameters.set(OAuth2ParameterNames.CODE, authorization.getTokens().getToken(OAuth2AuthorizationCode.class).getTokenValue());
|
|
|
+ parameters.set(OAuth2ParameterNames.REDIRECT_URI, registeredClient.getRedirectUris().iterator().next());
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
@EnableWebSecurity
|
|
|
@Import(OAuth2AuthorizationServerConfiguration.class)
|
|
|
static class AuthorizationServerConfiguration {
|
|
|
|
|
|
@Bean
|
|
|
RegisteredClientRepository registeredClientRepository() {
|
|
|
- return mock(RegisteredClientRepository.class);
|
|
|
+ return registeredClientRepository;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ OAuth2AuthorizationService authorizationService() {
|
|
|
+ return authorizationService;
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
CryptoKeySource keySource() {
|
|
|
- return mock(CryptoKeySource.class);
|
|
|
+ return keySource;
|
|
|
}
|
|
|
}
|
|
|
|