39
39
import org .opensaml .core .xml .io .MarshallingException ;
40
40
import org .opensaml .core .xml .schema .XSDateTime ;
41
41
import org .opensaml .core .xml .schema .impl .XSDateTimeBuilder ;
42
+ import org .opensaml .saml .common .SignableSAMLObject ;
42
43
import org .opensaml .saml .common .assertion .ValidationContext ;
43
44
import org .opensaml .saml .saml2 .assertion .SAML2AssertionValidationParameters ;
44
45
import org .opensaml .saml .saml2 .core .Assertion ;
@@ -144,9 +145,7 @@ public void authenticateWhenXmlErrorThenThrowAuthenticationException() {
144
145
public void authenticateWhenInvalidDestinationThenThrowAuthenticationException () {
145
146
Response response = response (DESTINATION + "invalid" , ASSERTING_PARTY_ENTITY_ID );
146
147
response .getAssertions ().add (assertion ());
147
- TestOpenSamlObjects .signed (response , TestSaml2X509Credentials .assertingPartySigningCredential (),
148
- RELYING_PARTY_ENTITY_ID );
149
- Saml2AuthenticationToken token = token (response , verifying (registration ()));
148
+ Saml2AuthenticationToken token = token (signed (response ), verifying (registration ()));
150
149
assertThatExceptionOfType (Saml2AuthenticationException .class )
151
150
.isThrownBy (() -> this .provider .authenticate (token ))
152
151
.satisfies (errorOf (Saml2ErrorCodes .INVALID_DESTINATION ));
@@ -176,9 +175,7 @@ public void authenticateWhenOpenSAMLValidationErrorThenThrowAuthenticationExcept
176
175
Assertion assertion = assertion ();
177
176
assertion .getSubject ().getSubjectConfirmations ().get (0 ).getSubjectConfirmationData ()
178
177
.setNotOnOrAfter (Instant .now ().minus (Duration .ofDays (3 )));
179
- TestOpenSamlObjects .signed (assertion , TestSaml2X509Credentials .assertingPartySigningCredential (),
180
- RELYING_PARTY_ENTITY_ID );
181
- response .getAssertions ().add (assertion );
178
+ response .getAssertions ().add (signed (assertion ));
182
179
Saml2AuthenticationToken token = token (response , verifying (registration ()));
183
180
assertThatExceptionOfType (Saml2AuthenticationException .class )
184
181
.isThrownBy (() -> this .provider .authenticate (token ))
@@ -190,9 +187,7 @@ public void authenticateWhenMissingSubjectThenThrowAuthenticationException() {
190
187
Response response = response ();
191
188
Assertion assertion = assertion ();
192
189
assertion .setSubject (null );
193
- TestOpenSamlObjects .signed (assertion , TestSaml2X509Credentials .assertingPartySigningCredential (),
194
- RELYING_PARTY_ENTITY_ID );
195
- response .getAssertions ().add (assertion );
190
+ response .getAssertions ().add (signed (assertion ));
196
191
Saml2AuthenticationToken token = token (response , verifying (registration ()));
197
192
assertThatExceptionOfType (Saml2AuthenticationException .class )
198
193
.isThrownBy (() -> this .provider .authenticate (token ))
@@ -204,9 +199,7 @@ public void authenticateWhenUsernameMissingThenThrowAuthenticationException() {
204
199
Response response = response ();
205
200
Assertion assertion = assertion ();
206
201
assertion .getSubject ().getNameID ().setValue (null );
207
- TestOpenSamlObjects .signed (assertion , TestSaml2X509Credentials .assertingPartySigningCredential (),
208
- RELYING_PARTY_ENTITY_ID );
209
- response .getAssertions ().add (assertion );
202
+ response .getAssertions ().add (signed (assertion ));
210
203
Saml2AuthenticationToken token = token (response , verifying (registration ()));
211
204
assertThatExceptionOfType (Saml2AuthenticationException .class )
212
205
.isThrownBy (() -> this .provider .authenticate (token ))
@@ -219,9 +212,7 @@ public void authenticateWhenAssertionContainsValidationAddressThenItSucceeds() {
219
212
Assertion assertion = assertion ();
220
213
assertion .getSubject ().getSubjectConfirmations ()
221
214
.forEach ((sc ) -> sc .getSubjectConfirmationData ().setAddress ("10.10.10.10" ));
222
- TestOpenSamlObjects .signed (assertion , TestSaml2X509Credentials .assertingPartySigningCredential (),
223
- RELYING_PARTY_ENTITY_ID );
224
- response .getAssertions ().add (assertion );
215
+ response .getAssertions ().add (signed (assertion ));
225
216
Saml2AuthenticationToken token = token (response , verifying (registration ()));
226
217
this .provider .authenticate (token );
227
218
}
@@ -232,9 +223,7 @@ public void authenticateWhenAssertionContainsAttributesThenItSucceeds() {
232
223
Assertion assertion = assertion ();
233
224
List <AttributeStatement > attributes = attributeStatements ();
234
225
assertion .getAttributeStatements ().addAll (attributes );
235
- TestOpenSamlObjects .signed (assertion , TestSaml2X509Credentials .assertingPartySigningCredential (),
236
- RELYING_PARTY_ENTITY_ID );
237
- response .getAssertions ().add (assertion );
226
+ response .getAssertions ().add (signed (assertion ));
238
227
Saml2AuthenticationToken token = token (response , verifying (registration ()));
239
228
Authentication authentication = this .provider .authenticate (token );
240
229
Saml2AuthenticatedPrincipal principal = (Saml2AuthenticatedPrincipal ) authentication .getPrincipal ();
@@ -258,9 +247,7 @@ public void authenticateWhenAssertionContainsCustomAttributesThenItSucceeds() {
258
247
AttributeStatement attribute = TestOpenSamlObjects .customAttributeStatement ("Address" ,
259
248
TestCustomOpenSamlObjects .instance ());
260
249
assertion .getAttributeStatements ().add (attribute );
261
- TestOpenSamlObjects .signed (assertion , TestSaml2X509Credentials .assertingPartySigningCredential (),
262
- RELYING_PARTY_ENTITY_ID );
263
- response .getAssertions ().add (assertion );
250
+ response .getAssertions ().add (signed (assertion ));
264
251
Saml2AuthenticationToken token = token (response , verifying (registration ()));
265
252
Authentication authentication = this .provider .authenticate (token );
266
253
Saml2AuthenticatedPrincipal principal = (Saml2AuthenticatedPrincipal ) authentication .getPrincipal ();
@@ -291,9 +278,7 @@ public void authenticateWhenEncryptedAssertionWithSignatureThenItSucceeds() {
291
278
EncryptedAssertion encryptedAssertion = TestOpenSamlObjects .encrypted (assertion ,
292
279
TestSaml2X509Credentials .assertingPartyEncryptingCredential ());
293
280
response .getEncryptedAssertions ().add (encryptedAssertion );
294
- TestOpenSamlObjects .signed (response , TestSaml2X509Credentials .assertingPartySigningCredential (),
295
- RELYING_PARTY_ENTITY_ID );
296
- Saml2AuthenticationToken token = token (response , decrypting (verifying (registration ())));
281
+ Saml2AuthenticationToken token = token (signed (response ), decrypting (verifying (registration ())));
297
282
this .provider .authenticate (token );
298
283
}
299
284
@@ -303,9 +288,7 @@ public void authenticateWhenEncryptedAssertionWithResponseSignatureThenItSucceed
303
288
EncryptedAssertion encryptedAssertion = TestOpenSamlObjects .encrypted (assertion (),
304
289
TestSaml2X509Credentials .assertingPartyEncryptingCredential ());
305
290
response .getEncryptedAssertions ().add (encryptedAssertion );
306
- TestOpenSamlObjects .signed (response , TestSaml2X509Credentials .assertingPartySigningCredential (),
307
- RELYING_PARTY_ENTITY_ID );
308
- Saml2AuthenticationToken token = token (response , decrypting (verifying (registration ())));
291
+ Saml2AuthenticationToken token = token (signed (response ), decrypting (verifying (registration ())));
309
292
this .provider .authenticate (token );
310
293
}
311
294
@@ -318,9 +301,7 @@ public void authenticateWhenEncryptedNameIdWithSignatureThenItSucceeds() {
318
301
TestSaml2X509Credentials .assertingPartyEncryptingCredential ());
319
302
assertion .getSubject ().setNameID (null );
320
303
assertion .getSubject ().setEncryptedID (encryptedID );
321
- response .getAssertions ().add (assertion );
322
- TestOpenSamlObjects .signed (assertion , TestSaml2X509Credentials .assertingPartySigningCredential (),
323
- RELYING_PARTY_ENTITY_ID );
304
+ response .getAssertions ().add (signed (assertion ));
324
305
Saml2AuthenticationToken token = token (response , decrypting (verifying (registration ())));
325
306
this .provider .authenticate (token );
326
307
}
@@ -335,9 +316,7 @@ public void authenticateWhenEncryptedAttributeThenDecrypts() {
335
316
statement .getEncryptedAttributes ().add (attribute );
336
317
assertion .getAttributeStatements ().add (statement );
337
318
response .getAssertions ().add (assertion );
338
- TestOpenSamlObjects .signed (response , TestSaml2X509Credentials .assertingPartySigningCredential (),
339
- RELYING_PARTY_ENTITY_ID );
340
- Saml2AuthenticationToken token = token (response , decrypting (verifying (registration ())));
319
+ Saml2AuthenticationToken token = token (signed (response ), decrypting (verifying (registration ())));
341
320
Saml2Authentication authentication = (Saml2Authentication ) this .provider .authenticate (token );
342
321
Saml2AuthenticatedPrincipal principal = (Saml2AuthenticatedPrincipal ) authentication .getPrincipal ();
343
322
assertThat (principal .getAttribute ("name" )).containsExactly ("value" );
@@ -349,9 +328,7 @@ public void authenticateWhenDecryptionKeysAreMissingThenThrowAuthenticationExcep
349
328
EncryptedAssertion encryptedAssertion = TestOpenSamlObjects .encrypted (assertion (),
350
329
TestSaml2X509Credentials .assertingPartyEncryptingCredential ());
351
330
response .getEncryptedAssertions ().add (encryptedAssertion );
352
- TestOpenSamlObjects .signed (response , TestSaml2X509Credentials .assertingPartySigningCredential (),
353
- RELYING_PARTY_ENTITY_ID );
354
- Saml2AuthenticationToken token = token (response , verifying (registration ()));
331
+ Saml2AuthenticationToken token = token (signed (response ), verifying (registration ()));
355
332
assertThatExceptionOfType (Saml2AuthenticationException .class )
356
333
.isThrownBy (() -> this .provider .authenticate (token ))
357
334
.satisfies (errorOf (Saml2ErrorCodes .DECRYPTION_ERROR , "Failed to decrypt EncryptedData" ));
@@ -363,9 +340,7 @@ public void authenticateWhenDecryptionKeysAreWrongThenThrowAuthenticationExcepti
363
340
EncryptedAssertion encryptedAssertion = TestOpenSamlObjects .encrypted (assertion (),
364
341
TestSaml2X509Credentials .assertingPartyEncryptingCredential ());
365
342
response .getEncryptedAssertions ().add (encryptedAssertion );
366
- TestOpenSamlObjects .signed (response , TestSaml2X509Credentials .assertingPartySigningCredential (),
367
- RELYING_PARTY_ENTITY_ID );
368
- Saml2AuthenticationToken token = token (response , registration ()
343
+ Saml2AuthenticationToken token = token (signed (response ), registration ()
369
344
.decryptionX509Credentials ((c ) -> c .add (TestSaml2X509Credentials .assertingPartyPrivateCredential ())));
370
345
assertThatExceptionOfType (Saml2AuthenticationException .class )
371
346
.isThrownBy (() -> this .provider .authenticate (token ))
@@ -378,9 +353,7 @@ public void authenticateWhenAuthenticationHasDetailsThenSucceeds() {
378
353
Assertion assertion = assertion ();
379
354
assertion .getSubject ().getSubjectConfirmations ()
380
355
.forEach ((sc ) -> sc .getSubjectConfirmationData ().setAddress ("10.10.10.10" ));
381
- TestOpenSamlObjects .signed (assertion , TestSaml2X509Credentials .assertingPartySigningCredential (),
382
- RELYING_PARTY_ENTITY_ID );
383
- response .getAssertions ().add (assertion );
356
+ response .getAssertions ().add (signed (assertion ));
384
357
Saml2AuthenticationToken token = token (response , verifying (registration ()));
385
358
token .setDetails ("some-details" );
386
359
Authentication authentication = this .provider .authenticate (token );
@@ -395,9 +368,7 @@ public void writeObjectWhenTypeIsSaml2AuthenticationThenNoException() throws IOE
395
368
EncryptedAssertion encryptedAssertion = TestOpenSamlObjects .encrypted (assertion ,
396
369
TestSaml2X509Credentials .assertingPartyEncryptingCredential ());
397
370
response .getEncryptedAssertions ().add (encryptedAssertion );
398
- TestOpenSamlObjects .signed (response , TestSaml2X509Credentials .assertingPartySigningCredential (),
399
- RELYING_PARTY_ENTITY_ID );
400
- Saml2AuthenticationToken token = token (response , decrypting (verifying (registration ())));
371
+ Saml2AuthenticationToken token = token (signed (response ), decrypting (verifying (registration ())));
401
372
Saml2Authentication authentication = (Saml2Authentication ) this .provider .authenticate (token );
402
373
// the following code will throw an exception if authentication isn't serializable
403
374
ByteArrayOutputStream byteStream = new ByteArrayOutputStream (1024 );
@@ -432,9 +403,7 @@ public void authenticateWhenDelegatingToDefaultAssertionValidatorThenUses() {
432
403
OneTimeUse oneTimeUse = build (OneTimeUse .DEFAULT_ELEMENT_NAME );
433
404
assertion .getConditions ().getConditions ().add (oneTimeUse );
434
405
response .getAssertions ().add (assertion );
435
- TestOpenSamlObjects .signed (response , TestSaml2X509Credentials .assertingPartySigningCredential (),
436
- ASSERTING_PARTY_ENTITY_ID );
437
- Saml2AuthenticationToken token = token (response , verifying (registration ()));
406
+ Saml2AuthenticationToken token = token (signed (response ), verifying (registration ()));
438
407
// @formatter:off
439
408
assertThatExceptionOfType (Saml2AuthenticationException .class )
440
409
.isThrownBy (() -> provider .authenticate (token )).isInstanceOf (Saml2AuthenticationException .class )
@@ -456,9 +425,7 @@ public void authenticateWhenCustomAssertionValidatorThenUses() {
456
425
Response response = response ();
457
426
Assertion assertion = assertion ();
458
427
response .getAssertions ().add (assertion );
459
- TestOpenSamlObjects .signed (response , TestSaml2X509Credentials .assertingPartySigningCredential (),
460
- ASSERTING_PARTY_ENTITY_ID );
461
- Saml2AuthenticationToken token = token (response , verifying (registration ()));
428
+ Saml2AuthenticationToken token = token (signed (response ), verifying (registration ()));
462
429
given (validator .convert (any (OpenSaml4AuthenticationProvider .AssertionToken .class )))
463
430
.willReturn (Saml2ResponseValidatorResult .success ());
464
431
provider .authenticate (token );
@@ -475,9 +442,7 @@ public void authenticateWhenDefaultConditionValidatorNotUsedThenSignatureStillCh
475
442
RELYING_PARTY_ENTITY_ID ); // broken
476
443
// signature
477
444
response .getAssertions ().add (assertion );
478
- TestOpenSamlObjects .signed (response , TestSaml2X509Credentials .assertingPartySigningCredential (),
479
- ASSERTING_PARTY_ENTITY_ID );
480
- Saml2AuthenticationToken token = token (response , verifying (registration ()));
445
+ Saml2AuthenticationToken token = token (signed (response ), verifying (registration ()));
481
446
// @formatter:off
482
447
assertThatExceptionOfType (Saml2AuthenticationException .class )
483
448
.isThrownBy (() -> provider .authenticate (token ))
@@ -496,9 +461,7 @@ public void authenticateWhenValidationContextCustomizedThenUsers() {
496
461
OpenSaml4AuthenticationProvider .createDefaultAssertionValidator ((assertionToken ) -> context ));
497
462
Response response = response ();
498
463
Assertion assertion = assertion ();
499
- response .getAssertions ().add (assertion );
500
- TestOpenSamlObjects .signed (response , TestSaml2X509Credentials .assertingPartySigningCredential (),
501
- ASSERTING_PARTY_ENTITY_ID );
464
+ response .getAssertions ().add (signed (assertion ));
502
465
Saml2AuthenticationToken token = token (response , verifying (registration ()));
503
466
// @formatter:off
504
467
assertThatExceptionOfType (Saml2AuthenticationException .class )
@@ -570,13 +533,12 @@ public void setAssertionElementsDecrypterWhenNullThenIllegalArgument() {
570
533
public void authenticateWhenCustomResponseElementsDecrypterThenDecryptsResponse () {
571
534
Response response = response ();
572
535
Assertion assertion = assertion ();
573
- TestOpenSamlObjects .signed (assertion , TestSaml2X509Credentials .assertingPartySigningCredential (),
574
- RELYING_PARTY_ENTITY_ID );
575
536
response .getEncryptedAssertions ().add (new EncryptedAssertionBuilder ().buildObject ());
576
537
TestOpenSamlObjects .signed (response , TestSaml2X509Credentials .assertingPartySigningCredential (),
577
538
RELYING_PARTY_ENTITY_ID );
578
539
Saml2AuthenticationToken token = token (response , verifying (registration ()));
579
- this .provider .setResponseElementsDecrypter ((tuple ) -> tuple .getResponse ().getAssertions ().add (assertion ));
540
+ this .provider
541
+ .setResponseElementsDecrypter ((tuple ) -> tuple .getResponse ().getAssertions ().add (signed (assertion )));
580
542
Authentication authentication = this .provider .authenticate (token );
581
543
assertThat (authentication .getName ()).isEqualTo ("test@saml.user" );
582
544
}
@@ -588,9 +550,7 @@ public void authenticateWhenCustomAssertionElementsDecrypterThenDecryptsAssertio
588
550
EncryptedID id = new EncryptedIDBuilder ().buildObject ();
589
551
id .setEncryptedData (new EncryptedDataBuilder ().buildObject ());
590
552
assertion .getSubject ().setEncryptedID (id );
591
- TestOpenSamlObjects .signed (assertion , TestSaml2X509Credentials .assertingPartySigningCredential (),
592
- RELYING_PARTY_ENTITY_ID );
593
- response .getAssertions ().add (assertion );
553
+ response .getAssertions ().add (signed (assertion ));
594
554
Saml2AuthenticationToken token = token (response , verifying (registration ()));
595
555
this .provider .setAssertionElementsDecrypter ((tuple ) -> {
596
556
NameID name = new NameIDBuilder ().buildObject ();
@@ -639,9 +599,7 @@ public void authenticateWhenCustomResponseValidatorThenUses() {
639
599
Response response = response ();
640
600
Assertion assertion = assertion ();
641
601
response .getAssertions ().add (assertion );
642
- TestOpenSamlObjects .signed (response , TestSaml2X509Credentials .assertingPartySigningCredential (),
643
- ASSERTING_PARTY_ENTITY_ID );
644
- Saml2AuthenticationToken token = token (response , verifying (registration ()));
602
+ Saml2AuthenticationToken token = token (signed (response ), verifying (registration ()));
645
603
given (validator .convert (any (OpenSaml4AuthenticationProvider .ResponseToken .class )))
646
604
.willReturn (Saml2ResponseValidatorResult .success ());
647
605
provider .authenticate (token );
@@ -655,9 +613,7 @@ public void authenticateWhenAssertionIssuerNotValidThenFailsWithInvalidIssuer()
655
613
Assertion assertion = assertion ();
656
614
assertion .setIssuer (TestOpenSamlObjects .issuer ("https://invalid.idp.test/saml2/idp" ));
657
615
response .getAssertions ().add (assertion );
658
- TestOpenSamlObjects .signed (response , TestSaml2X509Credentials .assertingPartySigningCredential (),
659
- ASSERTING_PARTY_ENTITY_ID );
660
- Saml2AuthenticationToken token = token (response , verifying (registration ()));
616
+ Saml2AuthenticationToken token = token (signed (response ), verifying (registration ()));
661
617
assertThatExceptionOfType (Saml2AuthenticationException .class ).isThrownBy (() -> provider .authenticate (token ))
662
618
.withMessageContaining ("did not match any valid issuers" );
663
619
}
@@ -716,6 +672,12 @@ private Assertion assertion() {
716
672
return assertion ;
717
673
}
718
674
675
+ private <T extends SignableSAMLObject > T signed (T toSign ) {
676
+ TestOpenSamlObjects .signed (toSign , TestSaml2X509Credentials .assertingPartySigningCredential (),
677
+ RELYING_PARTY_ENTITY_ID );
678
+ return toSign ;
679
+ }
680
+
719
681
private List <AttributeStatement > attributeStatements () {
720
682
List <AttributeStatement > attributeStatements = TestOpenSamlObjects .attributeStatements ();
721
683
AttributeBuilder attributeBuilder = new AttributeBuilder ();
0 commit comments