58
58
import org .springframework .security .oauth2 .server .authorization .web .OAuth2TokenEndpointFilter ;
59
59
import org .springframework .test .web .servlet .MockMvc ;
60
60
import org .springframework .test .web .servlet .MvcResult ;
61
- import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
62
61
import org .springframework .util .LinkedMultiValueMap ;
63
62
import org .springframework .util .MultiValueMap ;
64
63
import org .springframework .util .StringUtils ;
@@ -114,7 +113,9 @@ public static void init() {
114
113
jwtEncoder = new NimbusJwsEncoder (jwkSource );
115
114
jwtCustomizer = mock (BiConsumer .class );
116
115
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" );
118
119
}
119
120
120
121
@ Before
@@ -131,7 +132,7 @@ public void requestWhenAuthorizationRequestNotAuthenticatedThenRedirectToLogin()
131
132
when (registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
132
133
.thenReturn (registeredClient );
133
134
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 )
135
136
.params (getAuthorizationRequestParameters (registeredClient )))
136
137
.andExpect (status ().is3xxRedirection ())
137
138
.andReturn ();
@@ -145,11 +146,22 @@ public void requestWhenAuthorizationRequestNotAuthenticatedThenRedirectToLogin()
145
146
public void requestWhenAuthorizationRequestAuthenticatedThenRedirectToClient () throws Exception {
146
147
this .spring .register (AuthorizationServerConfiguration .class ).autowire ();
147
148
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 {
148
160
RegisteredClient registeredClient = TestRegisteredClients .registeredClient ().build ();
149
161
when (registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
150
162
.thenReturn (registeredClient );
151
163
152
- MvcResult mvcResult = this .mvc .perform (get (OAuth2AuthorizationEndpointFilter . DEFAULT_AUTHORIZATION_ENDPOINT_URI )
164
+ MvcResult mvcResult = this .mvc .perform (get (authorizationEndpointUri )
153
165
.params (getAuthorizationRequestParameters (registeredClient ))
154
166
.with (user ("user" )))
155
167
.andExpect (status ().is3xxRedirection ())
@@ -161,34 +173,26 @@ public void requestWhenAuthorizationRequestAuthenticatedThenRedirectToClient() t
161
173
}
162
174
163
175
@ 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 ();
166
178
167
179
RegisteredClient registeredClient = TestRegisteredClients .registeredClient ().build ();
168
180
when (registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
169
181
.thenReturn (registeredClient );
170
182
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 );
183
188
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 );
187
191
}
188
192
189
193
@ 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 ();
192
196
193
197
RegisteredClient registeredClient = TestRegisteredClients .registeredClient ().build ();
194
198
when (registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
@@ -200,7 +204,14 @@ public void requestWhenTokenRequestValidThenReturnAccessTokenResponse() throws E
200
204
eq (TokenType .AUTHORIZATION_CODE )))
201
205
.thenReturn (authorization );
202
206
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 )
204
215
.params (getTokenRequestParameters (registeredClient , authorization ))
205
216
.header (HttpHeaders .AUTHORIZATION , "Basic " + encodeBasicAuth (
206
217
registeredClient .getClientId (), registeredClient .getClientSecret ())))
@@ -288,48 +299,6 @@ public void requestWhenCustomJwtEncoderThenUsed() throws Exception {
288
299
verify (jwtCustomizer ).accept (any (JoseHeader .Builder .class ), any (JwtClaimsSet .Builder .class ));
289
300
}
290
301
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
-
333
302
private static MultiValueMap <String , String > getAuthorizationRequestParameters (RegisteredClient registeredClient ) {
334
303
MultiValueMap <String , String > parameters = new LinkedMultiValueMap <>();
335
304
parameters .set (OAuth2ParameterNames .RESPONSE_TYPE , OAuth2AuthorizationResponseType .CODE .getValue ());
@@ -390,7 +359,7 @@ JwtEncoder jwtEncoder() {
390
359
391
360
@ EnableWebSecurity
392
361
@ Import (OAuth2AuthorizationServerConfiguration .class )
393
- static class AuthorizationServerConfigurationWithProviderSettings extends AuthorizationServerConfiguration {
362
+ static class AuthorizationServerConfigurationCustomEndpoints extends AuthorizationServerConfiguration {
394
363
395
364
@ Bean
396
365
ProviderSettings providerSettings () {
0 commit comments