Skip to content

Commit 3f310ee

Browse files
committed
Polish gh-201
1 parent aeab085 commit 3f310ee

File tree

5 files changed

+93
-160
lines changed

5 files changed

+93
-160
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/config/annotation/web/configuration/OAuth2AuthorizationServerConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 the original author or authors.
2+
* Copyright 2020-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

oauth2-authorization-server/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/authorization/OAuth2AuthorizationServerConfigurer.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,12 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
8787
private RequestMatcher tokenRevocationEndpointMatcher;
8888
private RequestMatcher jwkSetEndpointMatcher;
8989
private RequestMatcher oidcProviderConfigurationEndpointMatcher;
90-
private final RequestMatcher endpointsMatcher = request -> {
91-
return this.authorizationEndpointMatcher.matches(request) ||
92-
this.tokenEndpointMatcher.matches(request) ||
93-
this.tokenRevocationEndpointMatcher.matches(request) ||
94-
this.jwkSetEndpointMatcher.matches(request) ||
95-
this.oidcProviderConfigurationEndpointMatcher.matches(request);
96-
};
90+
private final RequestMatcher endpointsMatcher = (request) ->
91+
this.authorizationEndpointMatcher.matches(request) ||
92+
this.tokenEndpointMatcher.matches(request) ||
93+
this.tokenRevocationEndpointMatcher.matches(request) ||
94+
this.jwkSetEndpointMatcher.matches(request) ||
95+
this.oidcProviderConfigurationEndpointMatcher.matches(request);
9796

9897
/**
9998
* Sets the repository of registered clients.
@@ -242,16 +241,6 @@ public void configure(B builder) {
242241
builder.addFilterAfter(postProcess(tokenRevocationEndpointFilter), OAuth2TokenEndpointFilter.class);
243242
}
244243

245-
private static void validateProviderSettings(ProviderSettings providerSettings) {
246-
if (providerSettings.issuer() != null) {
247-
try {
248-
new URI(providerSettings.issuer()).toURL();
249-
} catch (Exception ex) {
250-
throw new IllegalArgumentException("issuer must be a valid URL", ex);
251-
}
252-
}
253-
}
254-
255244
private void initEndpointMatchers(ProviderSettings providerSettings) {
256245
this.authorizationEndpointMatcher = new OrRequestMatcher(
257246
new AntPathRequestMatcher(
@@ -270,6 +259,16 @@ private void initEndpointMatchers(ProviderSettings providerSettings) {
270259
OidcProviderConfigurationEndpointFilter.DEFAULT_OIDC_PROVIDER_CONFIGURATION_ENDPOINT_URI, HttpMethod.GET.name());
271260
}
272261

262+
private static void validateProviderSettings(ProviderSettings providerSettings) {
263+
if (providerSettings.issuer() != null) {
264+
try {
265+
new URI(providerSettings.issuer()).toURL();
266+
} catch (Exception ex) {
267+
throw new IllegalArgumentException("issuer must be a valid URL", ex);
268+
}
269+
}
270+
}
271+
273272
private static <B extends HttpSecurityBuilder<B>> RegisteredClientRepository getRegisteredClientRepository(B builder) {
274273
RegisteredClientRepository registeredClientRepository = builder.getSharedObject(RegisteredClientRepository.class);
275274
if (registeredClientRepository == null) {

oauth2-authorization-server/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/authorization/JwkSetTests.java

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@
1515
*/
1616
package org.springframework.security.config.annotation.web.configurers.oauth2.server.authorization;
1717

18-
import static org.hamcrest.CoreMatchers.containsString;
19-
import static org.mockito.Mockito.mock;
20-
import static org.mockito.Mockito.reset;
21-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
22-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
23-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
24-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
25-
18+
import com.nimbusds.jose.jwk.JWKSet;
19+
import com.nimbusds.jose.jwk.source.JWKSource;
20+
import com.nimbusds.jose.proc.SecurityContext;
2621
import org.junit.Before;
2722
import org.junit.BeforeClass;
2823
import org.junit.Rule;
2924
import org.junit.Test;
25+
3026
import org.springframework.beans.factory.annotation.Autowired;
3127
import org.springframework.context.annotation.Bean;
3228
import org.springframework.context.annotation.Import;
@@ -41,12 +37,16 @@
4137
import org.springframework.security.oauth2.server.authorization.web.NimbusJwkSetEndpointFilter;
4238
import org.springframework.test.web.servlet.MockMvc;
4339

44-
import com.nimbusds.jose.jwk.JWKSet;
45-
import com.nimbusds.jose.jwk.source.JWKSource;
46-
import com.nimbusds.jose.proc.SecurityContext;
40+
import static org.hamcrest.CoreMatchers.containsString;
41+
import static org.mockito.Mockito.mock;
42+
import static org.mockito.Mockito.reset;
43+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
44+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
45+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
46+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
4747

4848
/**
49-
* Integration tests for the JWK Set requests.
49+
* Integration tests for the JWK Set endpoint.
5050
*
5151
* @author Florian Berthe
5252
*/
@@ -78,32 +78,26 @@ public void setup() {
7878
}
7979

8080
@Test
81-
public void requestWhenJwkSetValidThenReturnKeys() throws Exception {
81+
public void requestWhenJwkSetThenReturnKeys() throws Exception {
8282
this.spring.register(AuthorizationServerConfiguration.class).autowire();
8383

84-
this.mvc.perform(get(NimbusJwkSetEndpointFilter.DEFAULT_JWK_SET_ENDPOINT_URI))
85-
.andExpect(status().isOk())
86-
.andExpect(header().string(HttpHeaders.CACHE_CONTROL, containsString("no-store")))
87-
.andExpect(header().string(HttpHeaders.PRAGMA, containsString("no-cache")))
88-
.andExpect(jsonPath("$.keys").isNotEmpty())
89-
.andExpect(jsonPath("$.keys").isArray());
90-
84+
assertJwkSetRequestThenReturnKeys(NimbusJwkSetEndpointFilter.DEFAULT_JWK_SET_ENDPOINT_URI);
9185
}
9286

9387
@Test
94-
public void requestWhenCustomProviderSettingsThenOk() throws Exception {
95-
this.spring.register(AuthorizationServerConfigurationWithProviderSettings.class).autowire();
88+
public void requestWhenJwkSetCustomEndpointThenReturnKeys() throws Exception {
89+
this.spring.register(AuthorizationServerConfigurationCustomEndpoints.class).autowire();
9690

97-
this.mvc.perform(get(providerSettings.jwkSetEndpoint()))
98-
.andExpect(status().isOk());
91+
assertJwkSetRequestThenReturnKeys(providerSettings.jwkSetEndpoint());
9992
}
10093

101-
@Test
102-
public void requestWhenCustomProviderSettingsThenNotFound() throws Exception {
103-
this.spring.register(AuthorizationServerConfigurationWithProviderSettings.class).autowire();
104-
105-
this.mvc.perform(get(NimbusJwkSetEndpointFilter.DEFAULT_JWK_SET_ENDPOINT_URI))
106-
.andExpect(status().isNotFound());
94+
private void assertJwkSetRequestThenReturnKeys(String jwkSetEndpointUri) throws Exception {
95+
this.mvc.perform(get(jwkSetEndpointUri))
96+
.andExpect(status().isOk())
97+
.andExpect(header().string(HttpHeaders.CACHE_CONTROL, containsString("no-store")))
98+
.andExpect(header().string(HttpHeaders.PRAGMA, containsString("no-cache")))
99+
.andExpect(jsonPath("$.keys").isNotEmpty())
100+
.andExpect(jsonPath("$.keys").isArray());
107101
}
108102

109103
@EnableWebSecurity
@@ -128,7 +122,7 @@ JWKSource<SecurityContext> jwkSource() {
128122

129123
@EnableWebSecurity
130124
@Import(OAuth2AuthorizationServerConfiguration.class)
131-
static class AuthorizationServerConfigurationWithProviderSettings extends AuthorizationServerConfiguration {
125+
static class AuthorizationServerConfigurationCustomEndpoints extends AuthorizationServerConfiguration {
132126

133127
@Bean
134128
ProviderSettings providerSettings() {

oauth2-authorization-server/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/authorization/OAuth2AuthorizationCodeGrantTests.java

Lines changed: 36 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import org.springframework.security.oauth2.server.authorization.web.OAuth2TokenEndpointFilter;
5959
import org.springframework.test.web.servlet.MockMvc;
6060
import org.springframework.test.web.servlet.MvcResult;
61-
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
6261
import org.springframework.util.LinkedMultiValueMap;
6362
import org.springframework.util.MultiValueMap;
6463
import org.springframework.util.StringUtils;
@@ -114,7 +113,9 @@ public static void init() {
114113
jwtEncoder = new NimbusJwsEncoder(jwkSource);
115114
jwtCustomizer = mock(BiConsumer.class);
116115
jwtEncoder.setJwtCustomizer(jwtCustomizer);
117-
providerSettings = new ProviderSettings().authorizationEndpoint("/test/authorize").tokenEndpoint("/test/token");
116+
providerSettings = new ProviderSettings()
117+
.authorizationEndpoint("/test/authorize")
118+
.tokenEndpoint("/test/token");
118119
}
119120

120121
@Before
@@ -131,7 +132,7 @@ public void requestWhenAuthorizationRequestNotAuthenticatedThenRedirectToLogin()
131132
when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
132133
.thenReturn(registeredClient);
133134

134-
MvcResult mvcResult = this.mvc.perform(MockMvcRequestBuilders.get(OAuth2AuthorizationEndpointFilter.DEFAULT_AUTHORIZATION_ENDPOINT_URI)
135+
MvcResult mvcResult = this.mvc.perform(get(OAuth2AuthorizationEndpointFilter.DEFAULT_AUTHORIZATION_ENDPOINT_URI)
135136
.params(getAuthorizationRequestParameters(registeredClient)))
136137
.andExpect(status().is3xxRedirection())
137138
.andReturn();
@@ -145,11 +146,22 @@ public void requestWhenAuthorizationRequestNotAuthenticatedThenRedirectToLogin()
145146
public void requestWhenAuthorizationRequestAuthenticatedThenRedirectToClient() throws Exception {
146147
this.spring.register(AuthorizationServerConfiguration.class).autowire();
147148

149+
assertAuthorizationRequestRedirectsToClient(OAuth2AuthorizationEndpointFilter.DEFAULT_AUTHORIZATION_ENDPOINT_URI);
150+
}
151+
152+
@Test
153+
public void requestWhenAuthorizationRequestCustomEndpointThenRedirectToClient() throws Exception {
154+
this.spring.register(AuthorizationServerConfigurationCustomEndpoints.class).autowire();
155+
156+
assertAuthorizationRequestRedirectsToClient(providerSettings.authorizationEndpoint());
157+
}
158+
159+
private void assertAuthorizationRequestRedirectsToClient(String authorizationEndpointUri) throws Exception {
148160
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
149161
when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
150162
.thenReturn(registeredClient);
151163

152-
MvcResult mvcResult = this.mvc.perform(get(OAuth2AuthorizationEndpointFilter.DEFAULT_AUTHORIZATION_ENDPOINT_URI)
164+
MvcResult mvcResult = this.mvc.perform(get(authorizationEndpointUri)
153165
.params(getAuthorizationRequestParameters(registeredClient))
154166
.with(user("user")))
155167
.andExpect(status().is3xxRedirection())
@@ -161,34 +173,26 @@ public void requestWhenAuthorizationRequestAuthenticatedThenRedirectToClient() t
161173
}
162174

163175
@Test
164-
public void requestWhenAuthorizationRequestAndCustomProviderSettingsThenOk() throws Exception {
165-
this.spring.register(AuthorizationServerConfigurationWithProviderSettings.class).autowire();
176+
public void requestWhenTokenRequestValidThenReturnAccessTokenResponse() throws Exception {
177+
this.spring.register(AuthorizationServerConfiguration.class).autowire();
166178

167179
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
168180
when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
169181
.thenReturn(registeredClient);
170182

171-
this.mvc.perform(MockMvcRequestBuilders.get(providerSettings.authorizationEndpoint())
172-
.params(getAuthorizationRequestParameters(registeredClient)))
173-
.andExpect(status().is3xxRedirection());
174-
}
175-
176-
@Test
177-
public void requestWhenAuthorizationRequestAndCustomProviderSettingsThenNotFound() throws Exception {
178-
this.spring.register(AuthorizationServerConfigurationWithProviderSettings.class).autowire();
179-
180-
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
181-
when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
182-
.thenReturn(registeredClient);
183+
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
184+
when(authorizationService.findByToken(
185+
eq(authorization.getTokens().getToken(OAuth2AuthorizationCode.class).getTokenValue()),
186+
eq(TokenType.AUTHORIZATION_CODE)))
187+
.thenReturn(authorization);
183188

184-
this.mvc.perform(MockMvcRequestBuilders.get(OAuth2AuthorizationEndpointFilter.DEFAULT_AUTHORIZATION_ENDPOINT_URI)
185-
.params(getAuthorizationRequestParameters(registeredClient)))
186-
.andExpect(status().isNotFound());
189+
assertTokenRequestReturnsAccessTokenResponse(
190+
registeredClient, authorization, OAuth2TokenEndpointFilter.DEFAULT_TOKEN_ENDPOINT_URI);
187191
}
188192

189193
@Test
190-
public void requestWhenTokenRequestValidThenReturnAccessTokenResponse() throws Exception {
191-
this.spring.register(AuthorizationServerConfiguration.class).autowire();
194+
public void requestWhenTokenRequestCustomEndpointThenReturnAccessTokenResponse() throws Exception {
195+
this.spring.register(AuthorizationServerConfigurationCustomEndpoints.class).autowire();
192196

193197
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
194198
when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
@@ -200,7 +204,14 @@ public void requestWhenTokenRequestValidThenReturnAccessTokenResponse() throws E
200204
eq(TokenType.AUTHORIZATION_CODE)))
201205
.thenReturn(authorization);
202206

203-
this.mvc.perform(post(OAuth2TokenEndpointFilter.DEFAULT_TOKEN_ENDPOINT_URI)
207+
assertTokenRequestReturnsAccessTokenResponse(
208+
registeredClient, authorization, providerSettings.tokenEndpoint());
209+
}
210+
211+
private void assertTokenRequestReturnsAccessTokenResponse(RegisteredClient registeredClient,
212+
OAuth2Authorization authorization, String tokenEndpointUri) throws Exception {
213+
214+
this.mvc.perform(post(tokenEndpointUri)
204215
.params(getTokenRequestParameters(registeredClient, authorization))
205216
.header(HttpHeaders.AUTHORIZATION, "Basic " + encodeBasicAuth(
206217
registeredClient.getClientId(), registeredClient.getClientSecret())))
@@ -288,48 +299,6 @@ public void requestWhenCustomJwtEncoderThenUsed() throws Exception {
288299
verify(jwtCustomizer).accept(any(JoseHeader.Builder.class), any(JwtClaimsSet.Builder.class));
289300
}
290301

291-
@Test
292-
public void requestWhenCustomProviderSettingsThenOk() throws Exception {
293-
this.spring.register(AuthorizationServerConfigurationWithProviderSettings.class).autowire();
294-
295-
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
296-
when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
297-
.thenReturn(registeredClient);
298-
299-
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
300-
when(authorizationService.findByToken(
301-
eq(authorization.getTokens().getToken(OAuth2AuthorizationCode.class).getTokenValue()),
302-
eq(TokenType.AUTHORIZATION_CODE)))
303-
.thenReturn(authorization);
304-
305-
this.mvc.perform(post(providerSettings.tokenEndpoint())
306-
.params(getTokenRequestParameters(registeredClient, authorization))
307-
.header(HttpHeaders.AUTHORIZATION, "Basic " + encodeBasicAuth(
308-
registeredClient.getClientId(), registeredClient.getClientSecret())))
309-
.andExpect(status().isOk());
310-
}
311-
312-
@Test
313-
public void requestWhenCustomProviderSettingsThenNotFound() throws Exception {
314-
this.spring.register(AuthorizationServerConfigurationWithProviderSettings.class).autowire();
315-
316-
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
317-
when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
318-
.thenReturn(registeredClient);
319-
320-
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
321-
when(authorizationService.findByToken(
322-
eq(authorization.getTokens().getToken(OAuth2AuthorizationCode.class).getTokenValue()),
323-
eq(TokenType.AUTHORIZATION_CODE)))
324-
.thenReturn(authorization);
325-
326-
this.mvc.perform(post(OAuth2TokenEndpointFilter.DEFAULT_TOKEN_ENDPOINT_URI)
327-
.params(getTokenRequestParameters(registeredClient, authorization))
328-
.header(HttpHeaders.AUTHORIZATION, "Basic " + encodeBasicAuth(
329-
registeredClient.getClientId(), registeredClient.getClientSecret())))
330-
.andExpect(status().isNotFound());
331-
}
332-
333302
private static MultiValueMap<String, String> getAuthorizationRequestParameters(RegisteredClient registeredClient) {
334303
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
335304
parameters.set(OAuth2ParameterNames.RESPONSE_TYPE, OAuth2AuthorizationResponseType.CODE.getValue());
@@ -390,7 +359,7 @@ JwtEncoder jwtEncoder() {
390359

391360
@EnableWebSecurity
392361
@Import(OAuth2AuthorizationServerConfiguration.class)
393-
static class AuthorizationServerConfigurationWithProviderSettings extends AuthorizationServerConfiguration {
362+
static class AuthorizationServerConfigurationCustomEndpoints extends AuthorizationServerConfiguration {
394363

395364
@Bean
396365
ProviderSettings providerSettings() {

0 commit comments

Comments
 (0)