1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
import java .time .Instant ;
21
21
import java .util .Arrays ;
22
22
import java .util .Collections ;
23
- import java .util .HashMap ;
24
23
import java .util .HashSet ;
25
- import java .util .Map ;
26
- import java .util .Objects ;
27
- import java .util .function .Consumer ;
28
24
29
25
import jakarta .servlet .http .HttpServletRequest ;
30
26
import jakarta .servlet .http .HttpServletResponse ;
48
44
import org .springframework .security .oauth2 .client .OAuth2AuthorizeRequest ;
49
45
import org .springframework .security .oauth2 .client .OAuth2AuthorizedClient ;
50
46
import org .springframework .security .oauth2 .client .OAuth2AuthorizedClientManager ;
51
- import org .springframework .security .oauth2 .client .PasswordOAuth2AuthorizedClientProvider ;
52
47
import org .springframework .security .oauth2 .client .RefreshTokenOAuth2AuthorizedClientProvider ;
53
48
import org .springframework .security .oauth2 .client .TokenExchangeOAuth2AuthorizedClientProvider ;
54
49
import org .springframework .security .oauth2 .client .endpoint .AbstractOAuth2AuthorizationGrantRequest ;
55
50
import org .springframework .security .oauth2 .client .endpoint .JwtBearerGrantRequest ;
56
51
import org .springframework .security .oauth2 .client .endpoint .OAuth2AccessTokenResponseClient ;
57
52
import org .springframework .security .oauth2 .client .endpoint .OAuth2AuthorizationCodeGrantRequest ;
58
53
import org .springframework .security .oauth2 .client .endpoint .OAuth2ClientCredentialsGrantRequest ;
59
- import org .springframework .security .oauth2 .client .endpoint .OAuth2PasswordGrantRequest ;
60
54
import org .springframework .security .oauth2 .client .endpoint .OAuth2RefreshTokenGrantRequest ;
61
55
import org .springframework .security .oauth2 .client .endpoint .TokenExchangeGrantRequest ;
62
56
import org .springframework .security .oauth2 .client .registration .ClientRegistration ;
63
57
import org .springframework .security .oauth2 .client .registration .ClientRegistrationRepository ;
64
58
import org .springframework .security .oauth2 .client .registration .InMemoryClientRegistrationRepository ;
65
- import org .springframework .security .oauth2 .client .web .DefaultOAuth2AuthorizedClientManager ;
66
59
import org .springframework .security .oauth2 .client .web .OAuth2AuthorizedClientRepository ;
67
60
import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
68
61
import org .springframework .security .oauth2 .core .ClientAuthenticationMethod ;
71
64
import org .springframework .security .oauth2 .core .OAuth2Error ;
72
65
import org .springframework .security .oauth2 .core .TestOAuth2RefreshTokens ;
73
66
import org .springframework .security .oauth2 .core .endpoint .OAuth2AccessTokenResponse ;
74
- import org .springframework .security .oauth2 .core .endpoint .OAuth2ParameterNames ;
75
67
import org .springframework .security .oauth2 .core .endpoint .TestOAuth2AccessTokenResponses ;
76
68
import org .springframework .security .oauth2 .jwt .JoseHeaderNames ;
77
69
import org .springframework .security .oauth2 .jwt .Jwt ;
78
70
import org .springframework .security .oauth2 .jwt .JwtClaimNames ;
79
71
import org .springframework .security .oauth2 .server .resource .authentication .JwtAuthenticationToken ;
80
- import org .springframework .util .StringUtils ;
81
72
82
73
import static org .assertj .core .api .Assertions .assertThat ;
83
74
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
@@ -237,50 +228,6 @@ private void testClientCredentialsGrant() {
237
228
assertThat (grantRequest .getGrantType ()).isEqualTo (AuthorizationGrantType .CLIENT_CREDENTIALS );
238
229
}
239
230
240
- @ Test
241
- public void authorizeWhenPasswordAccessTokenResponseClientBeanThenUsed () {
242
- this .spring .register (CustomAccessTokenResponseClientsConfig .class ).autowire ();
243
- testPasswordGrant ();
244
- }
245
-
246
- @ Test
247
- public void authorizeWhenPasswordAuthorizedClientProviderBeanThenUsed () {
248
- this .spring .register (CustomAuthorizedClientProvidersConfig .class ).autowire ();
249
- testPasswordGrant ();
250
- }
251
-
252
- private void testPasswordGrant () {
253
- OAuth2AccessTokenResponse accessTokenResponse = TestOAuth2AccessTokenResponses .accessTokenResponse ().build ();
254
- given (MOCK_RESPONSE_CLIENT .getTokenResponse (any (OAuth2PasswordGrantRequest .class )))
255
- .willReturn (accessTokenResponse );
256
-
257
- TestingAuthenticationToken authentication = new TestingAuthenticationToken ("user" , "password" );
258
- ClientRegistration clientRegistration = this .clientRegistrationRepository .findByRegistrationId ("facebook" );
259
- // @formatter:off
260
- OAuth2AuthorizeRequest authorizeRequest = OAuth2AuthorizeRequest
261
- .withClientRegistrationId (clientRegistration .getRegistrationId ())
262
- .principal (authentication )
263
- .attribute (HttpServletRequest .class .getName (), this .request )
264
- .attribute (HttpServletResponse .class .getName (), this .response )
265
- .build ();
266
- // @formatter:on
267
- this .request .setParameter (OAuth2ParameterNames .USERNAME , "user" );
268
- this .request .setParameter (OAuth2ParameterNames .PASSWORD , "password" );
269
- OAuth2AuthorizedClient authorizedClient = this .authorizedClientManager .authorize (authorizeRequest );
270
- assertThat (authorizedClient ).isNotNull ();
271
-
272
- ArgumentCaptor <OAuth2PasswordGrantRequest > grantRequestCaptor = ArgumentCaptor
273
- .forClass (OAuth2PasswordGrantRequest .class );
274
- verify (MOCK_RESPONSE_CLIENT ).getTokenResponse (grantRequestCaptor .capture ());
275
-
276
- OAuth2PasswordGrantRequest grantRequest = grantRequestCaptor .getValue ();
277
- assertThat (grantRequest .getClientRegistration ().getRegistrationId ())
278
- .isEqualTo (clientRegistration .getRegistrationId ());
279
- assertThat (grantRequest .getGrantType ()).isEqualTo (AuthorizationGrantType .PASSWORD );
280
- assertThat (grantRequest .getUsername ()).isEqualTo ("user" );
281
- assertThat (grantRequest .getPassword ()).isEqualTo ("password" );
282
- }
283
-
284
231
@ Test
285
232
public void authorizeWhenJwtBearerAccessTokenResponseClientBeanThenUsed () {
286
233
this .spring .register (CustomAccessTokenResponseClientsConfig .class ).autowire ();
@@ -400,11 +347,6 @@ OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCrede
400
347
return new MockAccessTokenResponseClient <>();
401
348
}
402
349
403
- @ Bean
404
- OAuth2AccessTokenResponseClient <OAuth2PasswordGrantRequest > passwordTokenResponseClient () {
405
- return new MockAccessTokenResponseClient <>();
406
- }
407
-
408
350
@ Bean
409
351
OAuth2AccessTokenResponseClient <JwtBearerGrantRequest > jwtBearerTokenResponseClient () {
410
352
return new MockAccessTokenResponseClient <>();
@@ -440,13 +382,6 @@ ClientCredentialsOAuth2AuthorizedClientProvider clientCredentialsProvider() {
440
382
return authorizedClientProvider ;
441
383
}
442
384
443
- @ Bean
444
- PasswordOAuth2AuthorizedClientProvider passwordProvider () {
445
- PasswordOAuth2AuthorizedClientProvider authorizedClientProvider = new PasswordOAuth2AuthorizedClientProvider ();
446
- authorizedClientProvider .setAccessTokenResponseClient (new MockAccessTokenResponseClient <>());
447
- return authorizedClientProvider ;
448
- }
449
-
450
385
@ Bean
451
386
JwtBearerOAuth2AuthorizedClientProvider jwtBearerAuthorizedClientProvider () {
452
387
JwtBearerOAuth2AuthorizedClientProvider authorizedClientProvider = new JwtBearerOAuth2AuthorizedClientProvider ();
@@ -479,11 +414,6 @@ ClientRegistrationRepository clientRegistrationRepository() {
479
414
.clientSecret ("github-client-secret" )
480
415
.authorizationGrantType (AuthorizationGrantType .CLIENT_CREDENTIALS )
481
416
.build (),
482
- CommonOAuth2Provider .FACEBOOK .getBuilder ("facebook" )
483
- .clientId ("facebook-client-id" )
484
- .clientSecret ("facebook-client-secret" )
485
- .authorizationGrantType (AuthorizationGrantType .PASSWORD )
486
- .build (),
487
417
CommonOAuth2Provider .OKTA .getBuilder ("okta" )
488
418
.clientId ("okta-client-id" )
489
419
.clientSecret ("okta-client-secret" )
@@ -505,26 +435,6 @@ OAuth2AuthorizedClientRepository authorizedClientRepository() {
505
435
return mock (OAuth2AuthorizedClientRepository .class );
506
436
}
507
437
508
- @ Bean
509
- Consumer <DefaultOAuth2AuthorizedClientManager > authorizedClientManagerConsumer () {
510
- return (authorizedClientManager ) -> authorizedClientManager
511
- .setContextAttributesMapper ((authorizeRequest ) -> {
512
- HttpServletRequest request = Objects
513
- .requireNonNull (authorizeRequest .getAttribute (HttpServletRequest .class .getName ()));
514
- String username = request .getParameter (OAuth2ParameterNames .USERNAME );
515
- String password = request .getParameter (OAuth2ParameterNames .PASSWORD );
516
-
517
- Map <String , Object > attributes = Collections .emptyMap ();
518
- if (StringUtils .hasText (username ) && StringUtils .hasText (password )) {
519
- attributes = new HashMap <>();
520
- attributes .put (OAuth2AuthorizationContext .USERNAME_ATTRIBUTE_NAME , username );
521
- attributes .put (OAuth2AuthorizationContext .PASSWORD_ATTRIBUTE_NAME , password );
522
- }
523
-
524
- return attributes ;
525
- });
526
- }
527
-
528
438
}
529
439
530
440
private static class MockAccessTokenResponseClient <T extends AbstractOAuth2AuthorizationGrantRequest >
0 commit comments