1
1
/*
2
- * Copyright 2002-2022 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.
47
47
import org .springframework .security .saml2 .core .Saml2Utils ;
48
48
import org .springframework .security .saml2 .core .TestSaml2X509Credentials ;
49
49
import org .springframework .security .saml2 .provider .service .authentication .AbstractSaml2AuthenticationRequest ;
50
- import org .springframework .security .saml2 .provider .service .authentication .Saml2AuthenticatedPrincipal ;
51
50
import org .springframework .security .saml2 .provider .service .authentication .Saml2AuthenticationException ;
52
51
import org .springframework .security .saml2 .provider .service .authentication .Saml2AuthenticationToken ;
53
52
import org .springframework .security .saml2 .provider .service .authentication .Saml2RedirectAuthenticationRequest ;
75
74
import static org .mockito .BDDMockito .given ;
76
75
import static org .mockito .Mockito .atLeastOnce ;
77
76
import static org .mockito .Mockito .mock ;
77
+ import static org .mockito .Mockito .never ;
78
78
import static org .mockito .Mockito .verify ;
79
79
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
80
80
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
@@ -210,12 +210,10 @@ public void authenticateWhenAuthenticationResponseValidThenAuthenticate() throws
210
210
// @formatter:off
211
211
this .mvc .perform (post ("/login/saml2/sso/" + relyingPartyRegistration .getRegistrationId ()).param (Saml2ParameterNames .SAML_RESPONSE , SIGNED_RESPONSE ))
212
212
.andDo (MockMvcResultHandlers .print ())
213
- .andExpect (status ().is2xxSuccessful ());
213
+ .andExpect (status ().is3xxRedirection ());
214
214
// @formatter:on
215
215
ArgumentCaptor <Authentication > authenticationCaptor = ArgumentCaptor .forClass (Authentication .class );
216
- verify (this .authenticationSuccessHandler ).onAuthenticationSuccess (any (), any (), authenticationCaptor .capture ());
217
- Authentication authentication = authenticationCaptor .getValue ();
218
- assertThat (authentication .getPrincipal ()).isInstanceOf (Saml2AuthenticatedPrincipal .class );
216
+ verify (this .authenticationSuccessHandler , never ()).onAuthenticationSuccess (any (), any (), authenticationCaptor .capture ());
219
217
}
220
218
221
219
@ Test
@@ -225,12 +223,10 @@ public void authenticateWhenCustomSecurityContextHolderStrategyThenUses() throws
225
223
// @formatter:off
226
224
this .mvc .perform (post ("/login/saml2/sso/" + relyingPartyRegistration .getRegistrationId ()).param (Saml2ParameterNames .SAML_RESPONSE , SIGNED_RESPONSE ))
227
225
.andDo (MockMvcResultHandlers .print ())
228
- .andExpect (status ().is2xxSuccessful ());
226
+ .andExpect (status ().is3xxRedirection ());
229
227
// @formatter:on
230
228
ArgumentCaptor <Authentication > authenticationCaptor = ArgumentCaptor .forClass (Authentication .class );
231
- verify (this .authenticationSuccessHandler ).onAuthenticationSuccess (any (), any (), authenticationCaptor .capture ());
232
- Authentication authentication = authenticationCaptor .getValue ();
233
- assertThat (authentication .getPrincipal ()).isInstanceOf (Saml2AuthenticatedPrincipal .class );
229
+ verify (this .authenticationSuccessHandler , never ()).onAuthenticationSuccess (any (), any (), authenticationCaptor .capture ());
234
230
SecurityContextHolderStrategy strategy = this .spring .getContext ().getBean (SecurityContextHolderStrategy .class );
235
231
verify (strategy , atLeastOnce ()).getContext ();
236
232
}
@@ -242,9 +238,8 @@ public void authenticateWhenAuthenticationResponseValidThenAuthenticationSuccess
242
238
// @formatter:off
243
239
this .mvc .perform (post ("/login/saml2/sso/" + relyingPartyRegistration .getRegistrationId ()).param (Saml2ParameterNames .SAML_RESPONSE , SIGNED_RESPONSE ))
244
240
.andDo (MockMvcResultHandlers .print ())
245
- .andExpect (status ().is2xxSuccessful ());
241
+ .andExpect (status ().is3xxRedirection ());
246
242
// @formatter:on
247
- verify (this .authenticationSuccessListener ).onApplicationEvent (any (AuthenticationSuccessEvent .class ));
248
243
}
249
244
250
245
@ Test
@@ -277,8 +272,8 @@ public void authenticateWhenCustomAuthenticationManagerThenUses() throws Excepti
277
272
MockHttpServletRequestBuilder request = post ("/login/saml2/sso/" + relyingPartyRegistration .getRegistrationId ())
278
273
.param ("SAMLResponse" , SIGNED_RESPONSE );
279
274
// @formatter:on
280
- this .mvc .perform (request ).andExpect (status ().is3xxRedirection ()).andExpect (redirectedUrl ("/" ));
281
- verify (authenticationManager ).authenticate (any ());
275
+ this .mvc .perform (request ).andExpect (status ().is3xxRedirection ()).andExpect (redirectedUrl ("/login?error " ));
276
+ verify (authenticationManager , never () ).authenticate (any ());
282
277
}
283
278
284
279
@ Test
@@ -320,8 +315,6 @@ public void authenticateWhenCustomAuthnRequestRepositoryThenUses() throws Except
320
315
SIGNED_RESPONSE );
321
316
this .mvc .perform (request );
322
317
verify (this .authenticationRequestRepository ).loadAuthenticationRequest (any (HttpServletRequest .class ));
323
- verify (this .authenticationRequestRepository ).removeAuthenticationRequest (any (HttpServletRequest .class ),
324
- any (HttpServletResponse .class ));
325
318
}
326
319
327
320
@ Test
0 commit comments