Skip to content

Commit e1826a0

Browse files
committed
Polish Signature Algorithm Support
- Changed name to signatureAlgorithms since method and algorithm are synonymous - Re-ordered methods to follow typical IDPSSODescriptor order - Adjusted JavaDoc to refer to IDPSSODescriptor terminology Issue gh-8952
1 parent 9900658 commit e1826a0

File tree

4 files changed

+49
-39
lines changed

4 files changed

+49
-39
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/authentication/OpenSamlAuthenticationRequestFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ private String serialize(AuthnRequest authnRequest) {
274274

275275
private SignatureSigningParameters resolveSigningParameters(RelyingPartyRegistration relyingPartyRegistration) {
276276
List<Credential> credentials = resolveSigningCredentials(relyingPartyRegistration);
277-
List<String> algorithms = relyingPartyRegistration.getAssertingPartyDetails().getSigningMethodAlgorithms();
277+
List<String> algorithms = relyingPartyRegistration.getAssertingPartyDetails().getSigningAlgorithms();
278278
List<String> digests = Collections.singletonList(SignatureConstants.ALGO_ID_DIGEST_SHA256);
279279
String canonicalization = SignatureConstants.ALGO_ID_C14N_EXCL_OMIT_COMMENTS;
280280
SignatureSigningParametersResolver resolver = new SAMLMetadataSignatureSigningParametersResolver();

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/registration/RelyingPartyRegistration.java

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.security.PrivateKey;
2020
import java.security.cert.X509Certificate;
2121
import java.util.ArrayList;
22-
import java.util.Arrays;
2322
import java.util.Collection;
2423
import java.util.Collections;
2524
import java.util.HashSet;
@@ -368,6 +367,8 @@ public static Builder withRelyingPartyRegistration(RelyingPartyRegistration regi
368367
.assertingPartyDetails((assertingParty) -> assertingParty
369368
.entityId(registration.getAssertingPartyDetails().getEntityId())
370369
.wantAuthnRequestsSigned(registration.getAssertingPartyDetails().getWantAuthnRequestsSigned())
370+
.signingAlgorithms((algorithms) -> algorithms
371+
.addAll(registration.getAssertingPartyDetails().getSigningAlgorithms()))
371372
.verificationX509Credentials((c) -> c
372373
.addAll(registration.getAssertingPartyDetails().getVerificationX509Credentials()))
373374
.encryptionX509Credentials(
@@ -434,6 +435,8 @@ public static final class AssertingPartyDetails {
434435

435436
private final boolean wantAuthnRequestsSigned;
436437

438+
private List<String> signingAlgorithms;
439+
437440
private final Collection<Saml2X509Credential> verificationX509Credentials;
438441

439442
private final Collection<Saml2X509Credential> encryptionX509Credentials;
@@ -442,13 +445,12 @@ public static final class AssertingPartyDetails {
442445

443446
private final Saml2MessageBinding singleSignOnServiceBinding;
444447

445-
private List<String> signingMethodAlgorithms;
446-
447-
private AssertingPartyDetails(String entityId, boolean wantAuthnRequestsSigned,
448+
private AssertingPartyDetails(String entityId, boolean wantAuthnRequestsSigned, List<String> signingAlgorithms,
448449
Collection<Saml2X509Credential> verificationX509Credentials,
449450
Collection<Saml2X509Credential> encryptionX509Credentials, String singleSignOnServiceLocation,
450-
Saml2MessageBinding singleSignOnServiceBinding, List<String> signingMethodAlgorithms) {
451+
Saml2MessageBinding singleSignOnServiceBinding) {
451452
Assert.hasText(entityId, "entityId cannot be null or empty");
453+
Assert.notEmpty(signingAlgorithms, "signingAlgorithms cannot be empty");
452454
Assert.notNull(verificationX509Credentials, "verificationX509Credentials cannot be null");
453455
for (Saml2X509Credential credential : verificationX509Credentials) {
454456
Assert.notNull(credential, "verificationX509Credentials cannot have null values");
@@ -463,14 +465,13 @@ private AssertingPartyDetails(String entityId, boolean wantAuthnRequestsSigned,
463465
}
464466
Assert.notNull(singleSignOnServiceLocation, "singleSignOnServiceLocation cannot be null");
465467
Assert.notNull(singleSignOnServiceBinding, "singleSignOnServiceBinding cannot be null");
466-
Assert.notEmpty(signingMethodAlgorithms, "signingMethodAlgorithms cannot be empty");
467468
this.entityId = entityId;
468469
this.wantAuthnRequestsSigned = wantAuthnRequestsSigned;
470+
this.signingAlgorithms = signingAlgorithms;
469471
this.verificationX509Credentials = verificationX509Credentials;
470472
this.encryptionX509Credentials = encryptionX509Credentials;
471473
this.singleSignOnServiceLocation = singleSignOnServiceLocation;
472474
this.singleSignOnServiceBinding = singleSignOnServiceBinding;
473-
this.signingMethodAlgorithms = signingMethodAlgorithms;
474475
}
475476

476477
/**
@@ -500,6 +501,20 @@ public boolean getWantAuthnRequestsSigned() {
500501
return this.wantAuthnRequestsSigned;
501502
}
502503

504+
/**
505+
* Get the list of org.opensaml.saml.ext.saml2alg.SigningMethod Algorithms for
506+
* this asserting party, in preference order.
507+
*
508+
* <p>
509+
* Equivalent to the values found in &lt;SigningMethod Algorithm="..."/&gt; in the
510+
* asserting party's &lt;IDPSSODescriptor&gt;.
511+
* @return the list of SigningMethod Algorithms
512+
* @since 5.5
513+
*/
514+
public List<String> getSigningAlgorithms() {
515+
return this.signingAlgorithms;
516+
}
517+
503518
/**
504519
* Get all verification {@link Saml2X509Credential}s associated with this
505520
* asserting party
@@ -550,21 +565,14 @@ public Saml2MessageBinding getSingleSignOnServiceBinding() {
550565
return this.singleSignOnServiceBinding;
551566
}
552567

553-
/**
554-
* Return the list of preferred signature algorithm URIs, in preference order.
555-
* @return the list of signature algorithm URIs
556-
* @since 5.5
557-
*/
558-
public List<String> getSigningMethodAlgorithms() {
559-
return this.signingMethodAlgorithms;
560-
}
561-
562568
public static final class Builder {
563569

564570
private String entityId;
565571

566572
private boolean wantAuthnRequestsSigned = true;
567573

574+
private List<String> signingAlgorithms = new ArrayList<>();
575+
568576
private Collection<Saml2X509Credential> verificationX509Credentials = new HashSet<>();
569577

570578
private Collection<Saml2X509Credential> encryptionX509Credentials = new HashSet<>();
@@ -573,8 +581,6 @@ public static final class Builder {
573581

574582
private Saml2MessageBinding singleSignOnServiceBinding = Saml2MessageBinding.REDIRECT;
575583

576-
private List<String> signingMethodAlgorithms = new ArrayList<>();
577-
578584
/**
579585
* Set the asserting party's <a href=
580586
* "https://wiki.shibboleth.net/confluence/display/CONCEPT/EntityNaming">EntityID</a>.
@@ -600,6 +606,19 @@ public Builder wantAuthnRequestsSigned(boolean wantAuthnRequestsSigned) {
600606
return this;
601607
}
602608

609+
/**
610+
* Apply this {@link Consumer} to the list of SigningMethod Algorithms
611+
* @param signingMethodAlgorithmsConsumer a {@link Consumer} of the list of
612+
* SigningMethod Algorithms
613+
* @return this {@link AssertingPartyDetails.Builder} for further
614+
* configuration
615+
* @since 5.5
616+
*/
617+
public Builder signingAlgorithms(Consumer<List<String>> signingMethodAlgorithmsConsumer) {
618+
signingMethodAlgorithmsConsumer.accept(this.signingAlgorithms);
619+
return this;
620+
}
621+
603622
/**
604623
* Apply this {@link Consumer} to the list of {@link Saml2X509Credential}s
605624
* @param credentialsConsumer a {@link Consumer} of the {@link List} of
@@ -658,31 +677,19 @@ public Builder singleSignOnServiceBinding(Saml2MessageBinding singleSignOnServic
658677
return this;
659678
}
660679

661-
/**
662-
* Apply this {@link Consumer} to the list of signature algorithm URIs
663-
* @param signingMethodAlgorithmsConsumer a {@link Consumer} of the list of
664-
* signature algorithm URIs
665-
* @return this {@code Builder}
666-
* @since 5.5
667-
*/
668-
public Builder signingMethodAlgorithms(Consumer<List<String>> signingMethodAlgorithmsConsumer) {
669-
signingMethodAlgorithmsConsumer.accept(this.signingMethodAlgorithms);
670-
return this;
671-
}
672-
673680
/**
674681
* Creates an immutable ProviderDetails object representing the configuration
675682
* for an Identity Provider, IDP
676683
* @return immutable ProviderDetails object
677684
*/
678685
public AssertingPartyDetails build() {
679-
List<String> signingMethodAlgorithmsCopy = this.signingMethodAlgorithms.isEmpty()
680-
? Arrays.asList(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256)
681-
: Collections.unmodifiableList(this.signingMethodAlgorithms);
686+
List<String> signingAlgorithms = this.signingAlgorithms.isEmpty()
687+
? Collections.singletonList(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256)
688+
: Collections.unmodifiableList(this.signingAlgorithms);
682689

683-
return new AssertingPartyDetails(this.entityId, this.wantAuthnRequestsSigned,
690+
return new AssertingPartyDetails(this.entityId, this.wantAuthnRequestsSigned, signingAlgorithms,
684691
this.verificationX509Credentials, this.encryptionX509Credentials,
685-
this.singleSignOnServiceLocation, this.singleSignOnServiceBinding, signingMethodAlgorithmsCopy);
692+
this.singleSignOnServiceLocation, this.singleSignOnServiceBinding);
686693
}
687694

688695
}

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/authentication/OpenSamlAuthenticationRequestFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public void createPostAuthenticationRequestWhenAssertionConsumerServiceBindingTh
244244
public void createRedirectAuthenticationRequestWhenSHA1SignRequestThenSignatureIsPresent() {
245245
RelyingPartyRegistration relyingPartyRegistration = this.relyingPartyRegistrationBuilder
246246
.assertingPartyDetails(
247-
(a) -> a.signingMethodAlgorithms((c) -> c.add(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1)))
247+
(a) -> a.signingAlgorithms((algs) -> algs.add(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1)))
248248
.build();
249249
Saml2AuthenticationRequestContext context = this.contextBuilder.relayState("Relay State Value")
250250
.relyingPartyRegistration(relyingPartyRegistration).build();

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/registration/RelyingPartyRegistrationTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ public class RelyingPartyRegistrationTests {
2828
@Test
2929
public void withRelyingPartyRegistrationWorks() {
3030
RelyingPartyRegistration registration = TestRelyingPartyRegistrations.relyingPartyRegistration()
31-
.providerDetails((p) -> p.binding(Saml2MessageBinding.POST))
32-
.providerDetails((p) -> p.signAuthNRequest(false))
31+
.assertingPartyDetails((a) -> a.singleSignOnServiceBinding(Saml2MessageBinding.POST))
32+
.assertingPartyDetails((a) -> a.wantAuthnRequestsSigned(false))
33+
.assertingPartyDetails((a) -> a.signingAlgorithms((algs) -> algs.add("alg")))
3334
.assertionConsumerServiceBinding(Saml2MessageBinding.REDIRECT).build();
3435
RelyingPartyRegistration copy = RelyingPartyRegistration.withRelyingPartyRegistration(registration).build();
3536
compareRegistrations(registration, copy);
@@ -71,6 +72,8 @@ private void compareRegistrations(RelyingPartyRegistration registration, Relying
7172
.isEqualTo(registration.getAssertingPartyDetails().getEncryptionX509Credentials());
7273
assertThat(copy.getAssertingPartyDetails().getVerificationX509Credentials())
7374
.isEqualTo(registration.getAssertingPartyDetails().getVerificationX509Credentials());
75+
assertThat(copy.getAssertingPartyDetails().getSigningAlgorithms())
76+
.isEqualTo(registration.getAssertingPartyDetails().getSigningAlgorithms());
7477
}
7578

7679
@Test

0 commit comments

Comments
 (0)