5 changed files with 320 additions and 31 deletions
@ -0,0 +1,139 @@
@@ -0,0 +1,139 @@
|
||||
/* |
||||
* Copyright 2020-2021 the original author or authors. |
||||
* |
||||
* 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; |
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString; |
||||
import static org.mockito.Mockito.mock; |
||||
import static org.mockito.Mockito.reset; |
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
||||
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; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.BeforeClass; |
||||
import org.junit.Rule; |
||||
import org.junit.Test; |
||||
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.oauth2.jose.TestJwks; |
||||
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; |
||||
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; |
||||
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings; |
||||
import org.springframework.security.oauth2.server.authorization.web.NimbusJwkSetEndpointFilter; |
||||
import org.springframework.test.web.servlet.MockMvc; |
||||
|
||||
import com.nimbusds.jose.jwk.JWKSet; |
||||
import com.nimbusds.jose.jwk.source.JWKSource; |
||||
import com.nimbusds.jose.proc.SecurityContext; |
||||
|
||||
/** |
||||
* Integration tests for the JWK Set requests. |
||||
* |
||||
* @author Florian Berthe |
||||
*/ |
||||
public class JwkSetTests { |
||||
private static RegisteredClientRepository registeredClientRepository; |
||||
private static OAuth2AuthorizationService authorizationService; |
||||
private static JWKSource<SecurityContext> jwkSource; |
||||
private static ProviderSettings providerSettings; |
||||
|
||||
@Rule |
||||
public final SpringTestRule spring = new SpringTestRule(); |
||||
|
||||
@Autowired |
||||
private MockMvc mvc; |
||||
|
||||
@BeforeClass |
||||
public static void init() { |
||||
registeredClientRepository = mock(RegisteredClientRepository.class); |
||||
authorizationService = mock(OAuth2AuthorizationService.class); |
||||
JWKSet jwkSet = new JWKSet(TestJwks.DEFAULT_RSA_JWK); |
||||
jwkSource = (jwkSelector, securityContext) -> jwkSelector.select(jwkSet); |
||||
providerSettings = new ProviderSettings().jwkSetEndpoint("/test/jwks"); |
||||
} |
||||
|
||||
@Before |
||||
public void setup() { |
||||
reset(registeredClientRepository); |
||||
reset(authorizationService); |
||||
} |
||||
|
||||
@Test |
||||
public void requestWhenJwkSetValidThenReturnKeys() throws Exception { |
||||
this.spring.register(AuthorizationServerConfiguration.class).autowire(); |
||||
|
||||
this.mvc.perform(get(NimbusJwkSetEndpointFilter.DEFAULT_JWK_SET_ENDPOINT_URI)) |
||||
.andExpect(status().isOk()) |
||||
.andExpect(header().string(HttpHeaders.CACHE_CONTROL, containsString("no-store"))) |
||||
.andExpect(header().string(HttpHeaders.PRAGMA, containsString("no-cache"))) |
||||
.andExpect(jsonPath("$.keys").isNotEmpty()) |
||||
.andExpect(jsonPath("$.keys").isArray()); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void requestWhenCustomProviderSettingsThenOk() throws Exception { |
||||
this.spring.register(AuthorizationServerConfigurationWithProviderSettings.class).autowire(); |
||||
|
||||
this.mvc.perform(get(providerSettings.jwkSetEndpoint())) |
||||
.andExpect(status().isOk()); |
||||
} |
||||
|
||||
@Test |
||||
public void requestWhenCustomProviderSettingsThenNotFound() throws Exception { |
||||
this.spring.register(AuthorizationServerConfigurationWithProviderSettings.class).autowire(); |
||||
|
||||
this.mvc.perform(get(NimbusJwkSetEndpointFilter.DEFAULT_JWK_SET_ENDPOINT_URI)) |
||||
.andExpect(status().isNotFound()); |
||||
} |
||||
|
||||
@EnableWebSecurity |
||||
@Import(OAuth2AuthorizationServerConfiguration.class) |
||||
static class AuthorizationServerConfiguration { |
||||
|
||||
@Bean |
||||
RegisteredClientRepository registeredClientRepository() { |
||||
return registeredClientRepository; |
||||
} |
||||
|
||||
@Bean |
||||
OAuth2AuthorizationService authorizationService() { |
||||
return authorizationService; |
||||
} |
||||
|
||||
@Bean |
||||
JWKSource<SecurityContext> jwkSource() { |
||||
return jwkSource; |
||||
} |
||||
} |
||||
|
||||
@EnableWebSecurity |
||||
@Import(OAuth2AuthorizationServerConfiguration.class) |
||||
static class AuthorizationServerConfigurationWithProviderSettings extends AuthorizationServerConfiguration { |
||||
|
||||
@Bean |
||||
ProviderSettings providerSettings() { |
||||
return providerSettings; |
||||
} |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue