19
19
import java .io .IOException ;
20
20
import java .net .URLDecoder ;
21
21
import java .time .Duration ;
22
- import java .time .Instant ;
23
22
import java .util .Base64 ;
24
23
import java .util .Collection ;
25
24
import java .util .Collections ;
34
33
import org .junit .jupiter .api .extension .ExtendWith ;
35
34
import org .mockito .ArgumentCaptor ;
36
35
import org .opensaml .saml .saml2 .core .Assertion ;
37
- import org .opensaml .saml .saml2 .core .AuthnRequest ;
38
36
39
37
import org .springframework .beans .factory .BeanCreationException ;
40
38
import org .springframework .beans .factory .annotation .Autowired ;
68
66
import org .springframework .security .saml2 .core .TestSaml2X509Credentials ;
69
67
import org .springframework .security .saml2 .provider .service .authentication .AbstractSaml2AuthenticationRequest ;
70
68
import org .springframework .security .saml2 .provider .service .authentication .OpenSaml4AuthenticationProvider ;
71
- import org .springframework .security .saml2 .provider .service .authentication .OpenSaml4AuthenticationRequestFactory ;
72
69
import org .springframework .security .saml2 .provider .service .authentication .OpenSamlAuthenticationProvider ;
73
70
import org .springframework .security .saml2 .provider .service .authentication .Saml2AuthenticatedPrincipal ;
74
71
import org .springframework .security .saml2 .provider .service .authentication .Saml2Authentication ;
75
72
import org .springframework .security .saml2 .provider .service .authentication .Saml2AuthenticationException ;
76
- import org .springframework .security .saml2 .provider .service .authentication .Saml2AuthenticationRequestContext ;
77
- import org .springframework .security .saml2 .provider .service .authentication .Saml2AuthenticationRequestFactory ;
78
73
import org .springframework .security .saml2 .provider .service .authentication .Saml2AuthenticationToken ;
79
- import org .springframework .security .saml2 .provider .service .authentication .TestOpenSamlObjects ;
80
- import org .springframework .security .saml2 .provider .service .authentication .TestSaml2AuthenticationRequestContexts ;
81
74
import org .springframework .security .saml2 .provider .service .registration .RelyingPartyRegistration ;
82
75
import org .springframework .security .saml2 .provider .service .registration .RelyingPartyRegistrationRepository ;
83
76
import org .springframework .security .saml2 .provider .service .registration .TestRelyingPartyRegistrations ;
84
77
import org .springframework .security .saml2 .provider .service .servlet .filter .Saml2WebSsoAuthenticationFilter ;
85
78
import org .springframework .security .saml2 .provider .service .web .DefaultRelyingPartyRegistrationResolver ;
86
79
import org .springframework .security .saml2 .provider .service .web .RelyingPartyRegistrationResolver ;
87
- import org .springframework .security .saml2 .provider .service .web .Saml2AuthenticationRequestContextResolver ;
88
80
import org .springframework .security .saml2 .provider .service .web .Saml2AuthenticationRequestRepository ;
89
81
import org .springframework .security .saml2 .provider .service .web .Saml2AuthenticationTokenConverter ;
90
82
import org .springframework .security .saml2 .provider .service .web .authentication .OpenSaml4AuthenticationRequestResolver ;
113
105
import static org .mockito .BDDMockito .given ;
114
106
import static org .mockito .Mockito .mock ;
115
107
import static org .mockito .Mockito .verify ;
116
- import static org .mockito .Mockito .verifyNoInteractions ;
117
108
import static org .springframework .security .config .Customizer .withDefaults ;
118
109
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
119
110
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
@@ -211,30 +202,6 @@ public void saml2LoginWhenConfiguringAuthenticationDefaultsUsingCustomizerThenTh
211
202
validateSaml2WebSsoAuthenticationFilterConfiguration ();
212
203
}
213
204
214
- @ Test
215
- public void saml2LoginWhenCustomAuthenticationRequestContextResolverThenUses () throws Exception {
216
- this .spring .register (CustomAuthenticationRequestContextResolver .class ).autowire ();
217
- Saml2AuthenticationRequestContext context = TestSaml2AuthenticationRequestContexts
218
- .authenticationRequestContext ().build ();
219
- Saml2AuthenticationRequestContextResolver resolver = this .spring .getContext ()
220
- .getBean (Saml2AuthenticationRequestContextResolver .class );
221
- given (resolver .resolve (any (HttpServletRequest .class ))).willReturn (context );
222
- this .mvc .perform (get ("/saml2/authenticate/registration-id" )).andExpect (status ().isFound ());
223
- verify (resolver ).resolve (any (HttpServletRequest .class ));
224
- }
225
-
226
- @ Test
227
- public void authenticationRequestWhenAuthnRequestContextConverterThenUses () throws Exception {
228
- this .spring .register (CustomAuthenticationRequestContextConverterResolver .class ).autowire ();
229
-
230
- MvcResult result = this .mvc .perform (get ("/saml2/authenticate/registration-id" )).andReturn ();
231
- UriComponents components = UriComponentsBuilder .fromHttpUrl (result .getResponse ().getRedirectedUrl ()).build ();
232
- String samlRequest = components .getQueryParams ().getFirst ("SAMLRequest" );
233
- String decoded = URLDecoder .decode (samlRequest , "UTF-8" );
234
- String inflated = Saml2Utils .samlInflate (Saml2Utils .samlDecode (decoded ));
235
- assertThat (inflated ).contains ("ForceAuthn=\" true\" " );
236
- }
237
-
238
205
@ Test
239
206
public void authenticationRequestWhenAuthenticationRequestResolverBeanThenUses () throws Exception {
240
207
this .spring .register (CustomAuthenticationRequestResolverBean .class ).autowire ();
@@ -257,19 +224,6 @@ public void authenticationRequestWhenAuthenticationRequestResolverDslThenUses()
257
224
assertThat (inflated ).contains ("ForceAuthn=\" true\" " );
258
225
}
259
226
260
- @ Test
261
- public void authenticationRequestWhenAuthenticationRequestResolverAndFactoryThenResolverTakesPrecedence ()
262
- throws Exception {
263
- this .spring .register (CustomAuthenticationRequestResolverPrecedence .class ).autowire ();
264
- MvcResult result = this .mvc .perform (get ("/saml2/authenticate/registration-id" )).andReturn ();
265
- UriComponents components = UriComponentsBuilder .fromHttpUrl (result .getResponse ().getRedirectedUrl ()).build ();
266
- String samlRequest = components .getQueryParams ().getFirst ("SAMLRequest" );
267
- String decoded = URLDecoder .decode (samlRequest , "UTF-8" );
268
- String inflated = Saml2Utils .samlInflate (Saml2Utils .samlDecode (decoded ));
269
- assertThat (inflated ).contains ("ForceAuthn=\" true\" " );
270
- verifyNoInteractions (this .spring .getContext ().getBean (Saml2AuthenticationRequestFactory .class ));
271
- }
272
-
273
227
@ Test
274
228
public void authenticateWhenCustomAuthenticationConverterThenUses () throws Exception {
275
229
this .spring .register (CustomAuthenticationConverter .class ).autowire ();
@@ -513,61 +467,6 @@ protected void configure(HttpSecurity http) throws Exception {
513
467
514
468
}
515
469
516
- @ EnableWebSecurity
517
- @ Import (Saml2LoginConfigBeans .class )
518
- static class CustomAuthenticationRequestContextResolver extends WebSecurityConfigurerAdapter {
519
-
520
- private final Saml2AuthenticationRequestContextResolver resolver = mock (
521
- Saml2AuthenticationRequestContextResolver .class );
522
-
523
- @ Override
524
- protected void configure (HttpSecurity http ) throws Exception {
525
- // @formatter:off
526
- http
527
- .authorizeRequests ((authz ) -> authz
528
- .anyRequest ().authenticated ()
529
- )
530
- .saml2Login (withDefaults ());
531
- // @formatter:on
532
- }
533
-
534
- @ Bean
535
- Saml2AuthenticationRequestContextResolver resolver () {
536
- return this .resolver ;
537
- }
538
-
539
- }
540
-
541
- @ EnableWebSecurity
542
- @ Import (Saml2LoginConfigBeans .class )
543
- static class CustomAuthenticationRequestContextConverterResolver extends WebSecurityConfigurerAdapter {
544
-
545
- @ Override
546
- protected void configure (HttpSecurity http ) throws Exception {
547
- // @formatter:off
548
- http
549
- .authorizeRequests ((authz ) -> authz
550
- .anyRequest ().authenticated ()
551
- )
552
- .saml2Login ((saml2 ) -> {
553
- });
554
- // @formatter:on
555
- }
556
-
557
- @ Bean
558
- Saml2AuthenticationRequestFactory authenticationRequestFactory () {
559
- OpenSaml4AuthenticationRequestFactory authenticationRequestFactory = new OpenSaml4AuthenticationRequestFactory ();
560
- authenticationRequestFactory .setAuthenticationRequestContextConverter ((context ) -> {
561
- AuthnRequest authnRequest = TestOpenSamlObjects .authnRequest ();
562
- authnRequest .setIssueInstant (Instant .now ());
563
- authnRequest .setForceAuthn (true );
564
- return authnRequest ;
565
- });
566
- return authenticationRequestFactory ;
567
- }
568
-
569
- }
570
-
571
470
@ EnableWebSecurity
572
471
@ Import (Saml2LoginConfigBeans .class )
573
472
static class CustomAuthenticationRequestResolverBean {
@@ -630,41 +529,6 @@ Saml2AuthenticationRequestResolver authenticationRequestResolver(
630
529
631
530
}
632
531
633
- @ EnableWebSecurity
634
- @ Import (Saml2LoginConfigBeans .class )
635
- static class CustomAuthenticationRequestResolverPrecedence {
636
-
637
- @ Bean
638
- SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
639
- // @formatter:off
640
- http
641
- .authorizeRequests ((authz ) -> authz
642
- .anyRequest ().authenticated ()
643
- )
644
- .saml2Login (Customizer .withDefaults ());
645
- // @formatter:on
646
-
647
- return http .build ();
648
- }
649
-
650
- @ Bean
651
- Saml2AuthenticationRequestFactory authenticationRequestFactory () {
652
- return mock (Saml2AuthenticationRequestFactory .class );
653
- }
654
-
655
- @ Bean
656
- Saml2AuthenticationRequestResolver authenticationRequestResolver (
657
- RelyingPartyRegistrationRepository registrations ) {
658
- RelyingPartyRegistrationResolver registrationResolver = new DefaultRelyingPartyRegistrationResolver (
659
- registrations );
660
- OpenSaml4AuthenticationRequestResolver delegate = new OpenSaml4AuthenticationRequestResolver (
661
- registrationResolver );
662
- delegate .setAuthnRequestCustomizer ((parameters ) -> parameters .getAuthnRequest ().setForceAuthn (true ));
663
- return delegate ;
664
- }
665
-
666
- }
667
-
668
532
@ EnableWebSecurity
669
533
@ Import (Saml2LoginConfigBeans .class )
670
534
static class CustomAuthenticationConverter extends WebSecurityConfigurerAdapter {
0 commit comments