1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 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.
19
19
import java .io .ByteArrayOutputStream ;
20
20
import java .io .IOException ;
21
21
import java .io .ObjectOutputStream ;
22
- import java .nio .charset .StandardCharsets ;
23
22
import java .time .Duration ;
24
23
import java .time .Instant ;
25
24
import java .util .Arrays ;
48
47
import org .opensaml .saml .saml2 .core .Attribute ;
49
48
import org .opensaml .saml .saml2 .core .AttributeStatement ;
50
49
import org .opensaml .saml .saml2 .core .AttributeValue ;
51
- import org .opensaml .saml .saml2 .core .AuthnRequest ;
52
50
import org .opensaml .saml .saml2 .core .Conditions ;
53
51
import org .opensaml .saml .saml2 .core .EncryptedAssertion ;
54
52
import org .opensaml .saml .saml2 .core .EncryptedAttribute ;
78
76
import org .springframework .security .saml2 .provider .service .authentication .OpenSaml4AuthenticationProvider .ResponseToken ;
79
77
import org .springframework .security .saml2 .provider .service .authentication .TestCustomOpenSamlObjects .CustomOpenSamlObject ;
80
78
import org .springframework .security .saml2 .provider .service .registration .RelyingPartyRegistration ;
81
- import org .springframework .security .saml2 .provider .service .registration .Saml2MessageBinding ;
82
79
import org .springframework .security .saml2 .provider .service .registration .TestRelyingPartyRegistrations ;
83
80
import org .springframework .util .StringUtils ;
84
81
@@ -228,8 +225,7 @@ public void evaluateInResponseToSucceedsWhenInResponseToInResponseAndAssertionsM
228
225
response .setInResponseTo ("SAML2" );
229
226
response .getAssertions ().add (signed (assertion ("SAML2" )));
230
227
response .getAssertions ().add (signed (assertion ("SAML2" )));
231
- AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mockedStoredAuthenticationRequest ("SAML2" ,
232
- Saml2MessageBinding .POST , false );
228
+ AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mockedStoredAuthenticationRequest ("SAML2" );
233
229
Saml2AuthenticationToken token = token (response , verifying (registration ()), mockAuthenticationRequest );
234
230
this .provider .authenticate (token );
235
231
}
@@ -239,32 +235,18 @@ public void evaluateInResponseToSucceedsWhenInResponseToInAssertionOnlyMatchRequ
239
235
Response response = response ();
240
236
response .getAssertions ().add (signed (assertion ()));
241
237
response .getAssertions ().add (signed (assertion ("SAML2" )));
242
- AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mockedStoredAuthenticationRequest ("SAML2" ,
243
- Saml2MessageBinding .POST , false );
238
+ AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mockedStoredAuthenticationRequest ("SAML2" );
244
239
Saml2AuthenticationToken token = token (response , verifying (registration ()), mockAuthenticationRequest );
245
240
this .provider .authenticate (token );
246
241
}
247
242
248
- @ Test
249
- public void evaluateInResponseToFailsWhenInResponseToInAssertionOnlyAndCorruptedStoredRequest () {
250
- Response response = response ();
251
- response .getAssertions ().add (signed (assertion ()));
252
- response .getAssertions ().add (signed (assertion ("SAML2" )));
253
- AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mockedStoredAuthenticationRequest ("SAML2" ,
254
- Saml2MessageBinding .POST , true );
255
- Saml2AuthenticationToken token = token (response , verifying (registration ()), mockAuthenticationRequest );
256
- assertThatExceptionOfType (Saml2AuthenticationException .class )
257
- .isThrownBy (() -> this .provider .authenticate (token )).withStackTraceContaining ("malformed_request_data" );
258
- }
259
-
260
243
@ Test
261
244
public void evaluateInResponseToFailsWhenInResponseToInAssertionMismatchWithRequestID () {
262
245
Response response = response ();
263
246
response .setInResponseTo ("SAML2" );
264
247
response .getAssertions ().add (signed (assertion ("SAML2" )));
265
248
response .getAssertions ().add (signed (assertion ("BAD" )));
266
- AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mockedStoredAuthenticationRequest ("SAML2" ,
267
- Saml2MessageBinding .POST , false );
249
+ AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mockedStoredAuthenticationRequest ("SAML2" );
268
250
Saml2AuthenticationToken token = token (response , verifying (registration ()), mockAuthenticationRequest );
269
251
assertThatExceptionOfType (Saml2AuthenticationException .class )
270
252
.isThrownBy (() -> this .provider .authenticate (token )).withStackTraceContaining ("invalid_assertion" );
@@ -275,8 +257,7 @@ public void evaluateInResponseToFailsWhenInResponseToInAssertionOnlyAndMismatchW
275
257
Response response = response ();
276
258
response .getAssertions ().add (signed (assertion ()));
277
259
response .getAssertions ().add (signed (assertion ("BAD" )));
278
- AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mockedStoredAuthenticationRequest ("SAML2" ,
279
- Saml2MessageBinding .POST , false );
260
+ AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mockedStoredAuthenticationRequest ("SAML2" );
280
261
Saml2AuthenticationToken token = token (response , verifying (registration ()), mockAuthenticationRequest );
281
262
assertThatExceptionOfType (Saml2AuthenticationException .class )
282
263
.isThrownBy (() -> this .provider .authenticate (token )).withStackTraceContaining ("invalid_assertion" );
@@ -288,26 +269,12 @@ public void evaluateInResponseToFailsWhenInResponseInToResponseMismatchWithReque
288
269
response .setInResponseTo ("BAD" );
289
270
response .getAssertions ().add (signed (assertion ("SAML2" )));
290
271
response .getAssertions ().add (signed (assertion ("SAML2" )));
291
- AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mockedStoredAuthenticationRequest ("SAML2" ,
292
- Saml2MessageBinding .POST , false );
272
+ AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mockedStoredAuthenticationRequest ("SAML2" );
293
273
Saml2AuthenticationToken token = token (response , verifying (registration ()), mockAuthenticationRequest );
294
274
assertThatExceptionOfType (Saml2AuthenticationException .class )
295
275
.isThrownBy (() -> this .provider .authenticate (token )).withStackTraceContaining ("invalid_in_response_to" );
296
276
}
297
277
298
- @ Test
299
- public void evaluateInResponseToFailsWhenInResponseInToResponseAndCorruptedStoredRequest () {
300
- Response response = response ();
301
- response .setInResponseTo ("SAML2" );
302
- response .getAssertions ().add (signed (assertion ()));
303
- response .getAssertions ().add (signed (assertion ()));
304
- AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mockedStoredAuthenticationRequest ("SAML2" ,
305
- Saml2MessageBinding .POST , true );
306
- Saml2AuthenticationToken token = token (response , verifying (registration ()), mockAuthenticationRequest );
307
- assertThatExceptionOfType (Saml2AuthenticationException .class )
308
- .isThrownBy (() -> this .provider .authenticate (token )).withStackTraceContaining ("malformed_request_data" );
309
- }
310
-
311
278
@ Test
312
279
public void evaluateInResponseToFailsWhenInResponseToInResponseButNoSavedRequest () {
313
280
Response response = response ();
@@ -321,8 +288,7 @@ public void evaluateInResponseToFailsWhenInResponseToInResponseButNoSavedRequest
321
288
public void evaluateInResponseToSucceedsWhenNoInResponseToInResponseOrAssertions () {
322
289
Response response = response ();
323
290
response .getAssertions ().add (signed (assertion ()));
324
- AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mockedStoredAuthenticationRequest ("SAML2" ,
325
- Saml2MessageBinding .POST , false );
291
+ AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mockedStoredAuthenticationRequest ("SAML2" );
326
292
Saml2AuthenticationToken token = token (response , verifying (registration ()), mockAuthenticationRequest );
327
293
this .provider .authenticate (token );
328
294
}
@@ -805,17 +771,6 @@ private Response response(String destination, String issuerEntityId) {
805
771
return response ;
806
772
}
807
773
808
- private AuthnRequest request () {
809
- AuthnRequest request = TestOpenSamlObjects .authnRequest ();
810
- return request ;
811
- }
812
-
813
- private String serializedRequest (AuthnRequest request , Saml2MessageBinding binding ) {
814
- String xml = serialize (request );
815
- return (binding == Saml2MessageBinding .POST ) ? Saml2Utils .samlEncode (xml .getBytes (StandardCharsets .UTF_8 ))
816
- : Saml2Utils .samlEncode (Saml2Utils .samlDeflate (xml ));
817
- }
818
-
819
774
private Assertion assertion (String inResponseTo ) {
820
775
Assertion assertion = TestOpenSamlObjects .assertion ();
821
776
assertion .setIssueInstant (Instant .now ());
@@ -871,19 +826,9 @@ private Saml2AuthenticationToken token(Response response, RelyingPartyRegistrati
871
826
return new Saml2AuthenticationToken (registration .build (), serialize (response ), authenticationRequest );
872
827
}
873
828
874
- private AbstractSaml2AuthenticationRequest mockedStoredAuthenticationRequest (String requestId ,
875
- Saml2MessageBinding binding , boolean corruptRequestString ) {
876
- AuthnRequest request = request ();
877
- if (requestId != null ) {
878
- request .setID (requestId );
879
- }
880
- String serializedRequest = serializedRequest (request , binding );
881
- if (corruptRequestString ) {
882
- serializedRequest = serializedRequest .substring (2 , serializedRequest .length () - 2 );
883
- }
829
+ private AbstractSaml2AuthenticationRequest mockedStoredAuthenticationRequest (String requestId ) {
884
830
AbstractSaml2AuthenticationRequest mockAuthenticationRequest = mock (AbstractSaml2AuthenticationRequest .class );
885
- given (mockAuthenticationRequest .getSamlRequest ()).willReturn (serializedRequest );
886
- given (mockAuthenticationRequest .getBinding ()).willReturn (binding );
831
+ given (mockAuthenticationRequest .getId ()).willReturn (requestId );
887
832
return mockAuthenticationRequest ;
888
833
}
889
834
0 commit comments