Skip to content

Commit 1460d53

Browse files
svschouw-bbjzheaux
authored andcommitted
Preserve order of RelyingPartRegistration credentials
Issue gh-10799
1 parent 040a28a commit 1460d53

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
lines changed

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/core/TestSaml2X509Credentials.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,6 +51,10 @@ public static Saml2X509Credential relyingPartyVerifyingCredential() {
5151
return new Saml2X509Credential(idpCertificate(), Saml2X509CredentialType.VERIFICATION);
5252
}
5353

54+
public static Saml2X509Credential relyingPartyEncryptingCredential() {
55+
return new Saml2X509Credential(idpCertificate(), Saml2X509CredentialType.ENCRYPTION);
56+
}
57+
5458
public static Saml2X509Credential relyingPartySigningCredential() {
5559
return new Saml2X509Credential(spPrivateKey(), spCertificate(), Saml2X509CredentialType.SIGNING);
5660
}
@@ -59,6 +63,15 @@ public static Saml2X509Credential relyingPartyDecryptingCredential() {
5963
return new Saml2X509Credential(spPrivateKey(), spCertificate(), Saml2X509CredentialType.DECRYPTION);
6064
}
6165

66+
public static Saml2X509Credential altPublicCredential() {
67+
return new Saml2X509Credential(altCertificate(), Saml2X509CredentialType.VERIFICATION, Saml2X509CredentialType.ENCRYPTION);
68+
}
69+
70+
public static Saml2X509Credential altPrivateCredential() {
71+
return new Saml2X509Credential(altPrivateKey(), altCertificate(), Saml2X509CredentialType.SIGNING,
72+
Saml2X509CredentialType.DECRYPTION);
73+
}
74+
6275
private static X509Certificate certificate(String cert) {
6376
ByteArrayInputStream certBytes = new ByteArrayInputStream(cert.getBytes());
6477
try {
@@ -170,4 +183,40 @@ private static PrivateKey spPrivateKey() {
170183
+ "-----END PRIVATE KEY-----");
171184
}
172185

186+
private static X509Certificate altCertificate() {
187+
return certificate(
188+
"-----BEGIN CERTIFICATE-----\n" + "MIICkDCCAfkCFEstVfmWSFQp/j88GaMUwqVK72adMA0GCSqGSIb3DQEBCwUAMIGG\n"
189+
+ "MQswCQYDVQQGEwJVUzETMBEGA1UECAwKV2FzaGluZ3RvbjESMBAGA1UEBwwJVmFu\n"
190+
+ "Y291dmVyMR0wGwYDVQQKDBRTcHJpbmcgU2VjdXJpdHkgU0FNTDEMMAoGA1UECwwD\n"
191+
+ "YWx0MSEwHwYDVQQDDBhhbHQuc3ByaW5nLnNlY3VyaXR5LnNhbWwwHhcNMjIwMjEw\n"
192+
+ "MTY1ODA4WhcNMzIwMjEwMTY1ODA4WjCBhjELMAkGA1UEBhMCVVMxEzARBgNVBAgM\n"
193+
+ "Cldhc2hpbmd0b24xEjAQBgNVBAcMCVZhbmNvdXZlcjEdMBsGA1UECgwUU3ByaW5n\n"
194+
+ "IFNlY3VyaXR5IFNBTUwxDDAKBgNVBAsMA2FsdDEhMB8GA1UEAwwYYWx0LnNwcmlu\n"
195+
+ "Zy5zZWN1cml0eS5zYW1sMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9ZGWj\n"
196+
+ "TPDsymQCJL044py4xLsBI/S9RvzNeR9oD/tHyoxCE+YZzjf0PyBtwqKzkKWqCPf4\n"
197+
+ "XGUYHfEpkM5kJYwCW8TsOx5fnwLIQweiPqjYrBr/O0IjHMqYG9HlR/ros7iBt4ab\n"
198+
+ "EGUu/B9yYg1YRYPxKQ6TNP3AD+9tBT8TsFFyjwIDAQABMA0GCSqGSIb3DQEBCwUA\n"
199+
+ "A4GBAKJf2VHLjkCHRxlbWn63jGiquq3ENYgd1JS0DZ3ggFmuc6zQiqxzRGtArIDZ\n"
200+
+ "0jH5nrG0jcvO0fqDqBQh0iT8thfUnkViAQvACZ9a+0x0NzUicJ+Ra51c8Z2enqbg\n"
201+
+ "pXy+ga67HcAXrDekm1MCGCgiEb/Cgl41lsideqhC8Efl7PRN\n" + "-----END CERTIFICATE-----");
202+
}
203+
204+
private static PrivateKey altPrivateKey() {
205+
return privateKey(
206+
"-----BEGIN PRIVATE KEY-----\n" + "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAL1kZaNM8OzKZAIk\n"
207+
+ "vTjinLjEuwEj9L1G/M15H2gP+0fKjEIT5hnON/Q/IG3CorOQpaoI9/hcZRgd8SmQ\n"
208+
+ "zmQljAJbxOw7Hl+fAshDB6I+qNisGv87QiMcypgb0eVH+uizuIG3hpsQZS78H3Ji\n"
209+
+ "DVhFg/EpDpM0/cAP720FPxOwUXKPAgMBAAECgYEApYKslAZ0cer5dSoYNzNLFOnQ\n"
210+
+ "J1H92r/Dw+k6+h0lUvr+keyD5T9jhM76DxHOUDBzpmIKGoDcVDQugk2rILfzXsQA\n"
211+
+ "JtwvDRJk32Z02Vt0jb7t/WUOOQhjKCjQuv9/tOx90GCl0VxYG69UOjaMRWrlg/i9\n"
212+
+ "6/zcTRIahIn5XxF0psECQQD7ivJCpDbOLJGsc8gNJR4cvjZ1q0mHIOrbKqJC0y1n\n"
213+
+ "5DrzGEflPeyCUwnOKNp9HJQP8gmZzXfj0JM9KsjpiUChAkEAwL+FmhDoTiqStIrH\n"
214+
+ "h9Kdnsev//imMmRHxjwDhntYvqavUsISRmY3imd8inoYq5dzWQMzBtoTyMRmqeLT\n"
215+
+ "DHV1LwJAW4xaV37Eo4z9B7Kr4Hzd1MA1ueW5QQDt+Q4vN/r7z4/1FHyFzh0Xcucd\n"
216+
+ "7nZX7qj0CkmgzOVG+Rb0P5LOxJA7gQJBAK1KQ2qNct375qPM9bEGSVGchH6k5X7+\n"
217+
+ "q4ztHdpFgTb/EzdbZiTG935GpjC1rwJuinTnrHOnkwv4j7iDRm24GF8CQQDqPvrQ\n"
218+
+ "GcItR6UUy0q/B8UxLzlE6t+HiznfiJKfyGgCHU56Y4/ZhzSQz2MZHz9SK4DsUL9s\n"
219+
+ "bOYrWq8VY2fyjV1t\n" + "-----END PRIVATE KEY-----");
220+
}
221+
173222
}

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

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
import org.junit.Test;
2020

21-
import org.springframework.security.saml2.credentials.TestSaml2X509Credentials;
21+
import org.springframework.security.saml2.core.Saml2X509Credential;
22+
import org.springframework.security.saml2.core.TestSaml2X509Credentials;
2223
import org.springframework.security.saml2.provider.service.servlet.filter.Saml2WebSsoAuthenticationFilter;
2324

2425
import static org.assertj.core.api.Assertions.assertThat;
@@ -81,9 +82,53 @@ public void buildWhenUsingDefaultsThenAssertionConsumerServiceBindingDefaultsToP
8182
RelyingPartyRegistration relyingPartyRegistration = RelyingPartyRegistration.withRegistrationId("id")
8283
.entityId("entity-id").assertionConsumerServiceLocation("location")
8384
.assertingPartyDetails((assertingParty) -> assertingParty.entityId("entity-id")
84-
.singleSignOnServiceLocation("location"))
85-
.credentials((c) -> c.add(TestSaml2X509Credentials.relyingPartyVerifyingCredential())).build();
85+
.singleSignOnServiceLocation("location")
86+
.verificationX509Credentials((c) -> c.add(TestSaml2X509Credentials.relyingPartyVerifyingCredential()))
87+
).build();
8688
assertThat(relyingPartyRegistration.getAssertionConsumerServiceBinding()).isEqualTo(Saml2MessageBinding.POST);
8789
}
8890

91+
@Test
92+
public void buildPreservesCredentialsOrder() {
93+
Saml2X509Credential altRpCredential = TestSaml2X509Credentials.altPrivateCredential();
94+
Saml2X509Credential altApCredential = TestSaml2X509Credentials.altPublicCredential();
95+
Saml2X509Credential verifyingCredential = TestSaml2X509Credentials.relyingPartyVerifyingCredential();
96+
Saml2X509Credential encryptingCredential = TestSaml2X509Credentials.relyingPartyEncryptingCredential();
97+
Saml2X509Credential signingCredential = TestSaml2X509Credentials.relyingPartySigningCredential();
98+
Saml2X509Credential decryptionCredential = TestSaml2X509Credentials.relyingPartyDecryptingCredential();
99+
100+
// Test with the alt credentials first
101+
RelyingPartyRegistration relyingPartyRegistration = TestRelyingPartyRegistrations.noCredentials()
102+
.assertingPartyDetails((assertingParty) -> assertingParty
103+
.verificationX509Credentials((c) -> { c.add(altApCredential); c.add(verifyingCredential); })
104+
.encryptionX509Credentials((c) -> { c.add(altApCredential); c.add(encryptingCredential); }))
105+
.signingX509Credentials(c -> { c.add(altRpCredential); c.add(signingCredential); })
106+
.decryptionX509Credentials(c -> { c.add(altRpCredential); c.add(decryptionCredential); })
107+
.build();
108+
assertThat(relyingPartyRegistration.getSigningX509Credentials())
109+
.containsExactly(altRpCredential, signingCredential);
110+
assertThat(relyingPartyRegistration.getDecryptionX509Credentials())
111+
.containsExactly(altRpCredential, decryptionCredential);
112+
assertThat(relyingPartyRegistration.getAssertingPartyDetails().getVerificationX509Credentials())
113+
.containsExactly(altApCredential, verifyingCredential);
114+
assertThat(relyingPartyRegistration.getAssertingPartyDetails().getEncryptionX509Credentials())
115+
.containsExactly(altApCredential, encryptingCredential);
116+
117+
// Test with the alt credentials last
118+
relyingPartyRegistration = TestRelyingPartyRegistrations.noCredentials()
119+
.assertingPartyDetails((assertingParty) -> assertingParty
120+
.verificationX509Credentials((c) -> { c.add(verifyingCredential); c.add(altApCredential); })
121+
.encryptionX509Credentials((c) -> { c.add(encryptingCredential); c.add(altApCredential); }))
122+
.signingX509Credentials(c -> { c.add(signingCredential); c.add(altRpCredential); })
123+
.decryptionX509Credentials(c -> { c.add(decryptionCredential); c.add(altRpCredential); })
124+
.build();
125+
assertThat(relyingPartyRegistration.getSigningX509Credentials())
126+
.containsExactly(signingCredential, altRpCredential);
127+
assertThat(relyingPartyRegistration.getDecryptionX509Credentials())
128+
.containsExactly(decryptionCredential, altRpCredential);
129+
assertThat(relyingPartyRegistration.getAssertingPartyDetails().getVerificationX509Credentials())
130+
.containsExactly(verifyingCredential, altApCredential);
131+
assertThat(relyingPartyRegistration.getAssertingPartyDetails().getEncryptionX509Credentials())
132+
.containsExactly(encryptingCredential, altApCredential);
133+
}
89134
}

0 commit comments

Comments
 (0)