1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 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.
38
38
import org .springframework .mock .web .MockHttpServletRequest ;
39
39
import org .springframework .mock .web .MockHttpServletResponse ;
40
40
import org .springframework .security .authentication .TestingAuthenticationToken ;
41
- import org .springframework .security .config .Customizer ;
42
- import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
43
41
import org .springframework .security .config .oauth2 .client .CommonOAuth2Provider ;
44
42
import org .springframework .security .config .test .SpringTestContext ;
45
43
import org .springframework .security .oauth2 .client .AuthorizationCodeOAuth2AuthorizedClientProvider ;
61
59
import org .springframework .security .oauth2 .client .endpoint .OAuth2PasswordGrantRequest ;
62
60
import org .springframework .security .oauth2 .client .endpoint .OAuth2RefreshTokenGrantRequest ;
63
61
import org .springframework .security .oauth2 .client .endpoint .TokenExchangeGrantRequest ;
64
- import org .springframework .security .oauth2 .client .oidc .userinfo .OidcUserRequest ;
65
- import org .springframework .security .oauth2 .client .oidc .userinfo .OidcUserService ;
66
62
import org .springframework .security .oauth2 .client .registration .ClientRegistration ;
67
63
import org .springframework .security .oauth2 .client .registration .ClientRegistrationRepository ;
68
64
import org .springframework .security .oauth2 .client .registration .InMemoryClientRegistrationRepository ;
69
- import org .springframework .security .oauth2 .client .userinfo .DefaultOAuth2UserService ;
70
- import org .springframework .security .oauth2 .client .userinfo .OAuth2UserRequest ;
71
- import org .springframework .security .oauth2 .client .userinfo .OAuth2UserService ;
72
65
import org .springframework .security .oauth2 .client .web .DefaultOAuth2AuthorizedClientManager ;
73
66
import org .springframework .security .oauth2 .client .web .OAuth2AuthorizedClientRepository ;
74
67
import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
80
73
import org .springframework .security .oauth2 .core .endpoint .OAuth2AccessTokenResponse ;
81
74
import org .springframework .security .oauth2 .core .endpoint .OAuth2ParameterNames ;
82
75
import org .springframework .security .oauth2 .core .endpoint .TestOAuth2AccessTokenResponses ;
83
- import org .springframework .security .oauth2 .core .oidc .user .OidcUser ;
84
- import org .springframework .security .oauth2 .core .user .OAuth2User ;
85
76
import org .springframework .security .oauth2 .jwt .JoseHeaderNames ;
86
77
import org .springframework .security .oauth2 .jwt .Jwt ;
87
78
import org .springframework .security .oauth2 .jwt .JwtClaimNames ;
88
79
import org .springframework .security .oauth2 .server .resource .authentication .JwtAuthenticationToken ;
89
- import org .springframework .security .web .SecurityFilterChain ;
90
80
import org .springframework .util .StringUtils ;
91
81
92
82
import static org .assertj .core .api .Assertions .assertThat ;
@@ -397,42 +387,32 @@ static class CustomAccessTokenResponseClientsConfig extends OAuth2ClientBaseConf
397
387
398
388
@ Bean
399
389
OAuth2AccessTokenResponseClient <OAuth2AuthorizationCodeGrantRequest > authorizationCodeTokenResponseClient () {
400
- return new MockAuthorizationCodeClient ();
390
+ return new MockAccessTokenResponseClient <> ();
401
391
}
402
392
403
393
@ Bean
404
394
OAuth2AccessTokenResponseClient <OAuth2RefreshTokenGrantRequest > refreshTokenTokenResponseClient () {
405
- return new MockRefreshTokenClient ();
395
+ return new MockAccessTokenResponseClient <> ();
406
396
}
407
397
408
398
@ Bean
409
399
OAuth2AccessTokenResponseClient <OAuth2ClientCredentialsGrantRequest > clientCredentialsTokenResponseClient () {
410
- return new MockClientCredentialsClient ();
400
+ return new MockAccessTokenResponseClient <> ();
411
401
}
412
402
413
403
@ Bean
414
404
OAuth2AccessTokenResponseClient <OAuth2PasswordGrantRequest > passwordTokenResponseClient () {
415
- return new MockPasswordClient ();
405
+ return new MockAccessTokenResponseClient <> ();
416
406
}
417
407
418
408
@ Bean
419
409
OAuth2AccessTokenResponseClient <JwtBearerGrantRequest > jwtBearerTokenResponseClient () {
420
- return new MockJwtBearerClient ();
410
+ return new MockAccessTokenResponseClient <> ();
421
411
}
422
412
423
413
@ Bean
424
414
OAuth2AccessTokenResponseClient <TokenExchangeGrantRequest > tokenExchangeTokenResponseClient () {
425
- return new MockTokenExchangeClient ();
426
- }
427
-
428
- @ Bean
429
- OAuth2UserService <OAuth2UserRequest , OAuth2User > oauth2UserService () {
430
- return mock (DefaultOAuth2UserService .class );
431
- }
432
-
433
- @ Bean
434
- OAuth2UserService <OidcUserRequest , OidcUser > oidcUserService () {
435
- return mock (OidcUserService .class );
415
+ return new MockAccessTokenResponseClient <>();
436
416
}
437
417
438
418
}
@@ -449,57 +429,46 @@ AuthorizationCodeOAuth2AuthorizedClientProvider authorizationCodeProvider() {
449
429
@ Bean
450
430
RefreshTokenOAuth2AuthorizedClientProvider refreshTokenProvider () {
451
431
RefreshTokenOAuth2AuthorizedClientProvider authorizedClientProvider = new RefreshTokenOAuth2AuthorizedClientProvider ();
452
- authorizedClientProvider .setAccessTokenResponseClient (new MockRefreshTokenClient ());
432
+ authorizedClientProvider .setAccessTokenResponseClient (new MockAccessTokenResponseClient <> ());
453
433
return authorizedClientProvider ;
454
434
}
455
435
456
436
@ Bean
457
437
ClientCredentialsOAuth2AuthorizedClientProvider clientCredentialsProvider () {
458
438
ClientCredentialsOAuth2AuthorizedClientProvider authorizedClientProvider = new ClientCredentialsOAuth2AuthorizedClientProvider ();
459
- authorizedClientProvider .setAccessTokenResponseClient (new MockClientCredentialsClient ());
439
+ authorizedClientProvider .setAccessTokenResponseClient (new MockAccessTokenResponseClient <> ());
460
440
return authorizedClientProvider ;
461
441
}
462
442
463
443
@ Bean
464
444
PasswordOAuth2AuthorizedClientProvider passwordProvider () {
465
445
PasswordOAuth2AuthorizedClientProvider authorizedClientProvider = new PasswordOAuth2AuthorizedClientProvider ();
466
- authorizedClientProvider .setAccessTokenResponseClient (new MockPasswordClient ());
446
+ authorizedClientProvider .setAccessTokenResponseClient (new MockAccessTokenResponseClient <> ());
467
447
return authorizedClientProvider ;
468
448
}
469
449
470
450
@ Bean
471
451
JwtBearerOAuth2AuthorizedClientProvider jwtBearerAuthorizedClientProvider () {
472
452
JwtBearerOAuth2AuthorizedClientProvider authorizedClientProvider = new JwtBearerOAuth2AuthorizedClientProvider ();
473
- authorizedClientProvider .setAccessTokenResponseClient (new MockJwtBearerClient ());
453
+ authorizedClientProvider .setAccessTokenResponseClient (new MockAccessTokenResponseClient <> ());
474
454
return authorizedClientProvider ;
475
455
}
476
456
477
457
@ Bean
478
458
TokenExchangeOAuth2AuthorizedClientProvider tokenExchangeAuthorizedClientProvider () {
479
459
TokenExchangeOAuth2AuthorizedClientProvider authorizedClientProvider = new TokenExchangeOAuth2AuthorizedClientProvider ();
480
- authorizedClientProvider .setAccessTokenResponseClient (new MockTokenExchangeClient ());
460
+ authorizedClientProvider .setAccessTokenResponseClient (new MockAccessTokenResponseClient <> ());
481
461
return authorizedClientProvider ;
482
462
}
483
463
484
464
}
485
465
486
466
abstract static class OAuth2ClientBaseConfig {
487
467
488
- @ Bean
489
- SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
490
- // @formatter:off
491
- http
492
- .authorizeHttpRequests ((authorize ) -> authorize .anyRequest ().authenticated ())
493
- .oauth2Login (Customizer .withDefaults ())
494
- .oauth2Client (Customizer .withDefaults ());
495
- return http .build ();
496
- // @formatter:on
497
- }
498
-
499
468
@ Bean
500
469
ClientRegistrationRepository clientRegistrationRepository () {
501
470
// @formatter:off
502
- return new InMemoryClientRegistrationRepository (Arrays . asList (
471
+ return new InMemoryClientRegistrationRepository (
503
472
CommonOAuth2Provider .GOOGLE .getBuilder ("google" )
504
473
.clientId ("google-client-id" )
505
474
.clientSecret ("google-client-secret" )
@@ -527,7 +496,7 @@ ClientRegistrationRepository clientRegistrationRepository() {
527
496
.clientAuthenticationMethod (ClientAuthenticationMethod .CLIENT_SECRET_BASIC )
528
497
.authorizationGrantType (AuthorizationGrantType .TOKEN_EXCHANGE )
529
498
.scope ("user.read" , "user.write" )
530
- .build ())) ;
499
+ .build ());
531
500
// @formatter:on
532
501
}
533
502
@@ -558,60 +527,11 @@ Consumer<DefaultOAuth2AuthorizedClientManager> authorizedClientManagerConsumer()
558
527
559
528
}
560
529
561
- private static class MockAuthorizationCodeClient
562
- implements OAuth2AccessTokenResponseClient <OAuth2AuthorizationCodeGrantRequest > {
563
-
564
- @ Override
565
- public OAuth2AccessTokenResponse getTokenResponse (
566
- OAuth2AuthorizationCodeGrantRequest authorizationGrantRequest ) {
567
- return MOCK_RESPONSE_CLIENT .getTokenResponse (authorizationGrantRequest );
568
- }
569
-
570
- }
571
-
572
- private static class MockRefreshTokenClient
573
- implements OAuth2AccessTokenResponseClient <OAuth2RefreshTokenGrantRequest > {
574
-
575
- @ Override
576
- public OAuth2AccessTokenResponse getTokenResponse (OAuth2RefreshTokenGrantRequest authorizationGrantRequest ) {
577
- return MOCK_RESPONSE_CLIENT .getTokenResponse (authorizationGrantRequest );
578
- }
579
-
580
- }
581
-
582
- private static class MockClientCredentialsClient
583
- implements OAuth2AccessTokenResponseClient <OAuth2ClientCredentialsGrantRequest > {
584
-
585
- @ Override
586
- public OAuth2AccessTokenResponse getTokenResponse (
587
- OAuth2ClientCredentialsGrantRequest authorizationGrantRequest ) {
588
- return MOCK_RESPONSE_CLIENT .getTokenResponse (authorizationGrantRequest );
589
- }
590
-
591
- }
592
-
593
- private static class MockPasswordClient implements OAuth2AccessTokenResponseClient <OAuth2PasswordGrantRequest > {
594
-
595
- @ Override
596
- public OAuth2AccessTokenResponse getTokenResponse (OAuth2PasswordGrantRequest authorizationGrantRequest ) {
597
- return MOCK_RESPONSE_CLIENT .getTokenResponse (authorizationGrantRequest );
598
- }
599
-
600
- }
601
-
602
- private static class MockJwtBearerClient implements OAuth2AccessTokenResponseClient <JwtBearerGrantRequest > {
603
-
604
- @ Override
605
- public OAuth2AccessTokenResponse getTokenResponse (JwtBearerGrantRequest authorizationGrantRequest ) {
606
- return MOCK_RESPONSE_CLIENT .getTokenResponse (authorizationGrantRequest );
607
- }
608
-
609
- }
610
-
611
- private static class MockTokenExchangeClient implements OAuth2AccessTokenResponseClient <TokenExchangeGrantRequest > {
530
+ private static class MockAccessTokenResponseClient <T extends AbstractOAuth2AuthorizationGrantRequest >
531
+ implements OAuth2AccessTokenResponseClient <T > {
612
532
613
533
@ Override
614
- public OAuth2AccessTokenResponse getTokenResponse (TokenExchangeGrantRequest authorizationGrantRequest ) {
534
+ public OAuth2AccessTokenResponse getTokenResponse (T authorizationGrantRequest ) {
615
535
return MOCK_RESPONSE_CLIENT .getTokenResponse (authorizationGrantRequest );
616
536
}
617
537
0 commit comments