Skip to content

Commit 3978cc5

Browse files
ThomasVitalejzheaux
authored andcommitted
Add Static Factories to Saml2X509Credential
- Add static factories to Saml2X509Credential for verification, encryption, signing, and decryption. - Add unit tests for new static factories in Saml2X509Credential. Fixes gh-8789
1 parent cc44a93 commit 3978cc5

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/credentials/Saml2X509Credential.java

+34
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,40 @@ public enum Saml2X509CredentialType {
5454
private final X509Certificate certificate;
5555
private final Set<Saml2X509CredentialType> credentialTypes;
5656

57+
/**
58+
* Create a {@link Saml2X509Credential} that can be used for encryption.
59+
* @param certificate the certificate to use for encryption
60+
*/
61+
public static Saml2X509Credential encryption(X509Certificate certificate) {
62+
return new Saml2X509Credential(certificate, Saml2X509CredentialType.ENCRYPTION);
63+
}
64+
65+
/**
66+
* Create a {@link Saml2X509Credential} that can be used for verification.
67+
* @param certificate the certificate to use for verification
68+
*/
69+
public static Saml2X509Credential verification(X509Certificate certificate) {
70+
return new Saml2X509Credential(certificate, Saml2X509CredentialType.VERIFICATION);
71+
}
72+
73+
/**
74+
* Create a {@link Saml2X509Credential} that can be used for decryption.
75+
* @param privateKey the private key to use for decryption
76+
* @param certificate the certificate to use for decryption
77+
*/
78+
public static Saml2X509Credential decryption(PrivateKey privateKey, X509Certificate certificate) {
79+
return new Saml2X509Credential(privateKey, certificate, Saml2X509CredentialType.DECRYPTION);
80+
}
81+
82+
/**
83+
* Create a {@link Saml2X509Credential} that can be used for signing.
84+
* @param privateKey the private key to use for signing
85+
* @param certificate the certificate to use for signing
86+
*/
87+
public static Saml2X509Credential signing(PrivateKey privateKey, X509Certificate certificate) {
88+
return new Saml2X509Credential(privateKey, certificate, Saml2X509CredentialType.SIGNING);
89+
}
90+
5791
/**
5892
* Creates a Saml2X509Credentials representing Identity Provider credentials for
5993
* verification, encryption or both.

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/credentials/Saml2X509CredentialTests.java

+51-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616

1717
package org.springframework.security.saml2.credentials;
1818

19-
import org.springframework.security.converter.RsaKeyConverters;
20-
2119
import org.junit.Before;
2220
import org.junit.Rule;
2321
import org.junit.Test;
2422
import org.junit.rules.ExpectedException;
23+
import org.springframework.security.converter.RsaKeyConverters;
2524

2625
import java.io.ByteArrayInputStream;
2726
import java.security.PrivateKey;
@@ -88,13 +87,17 @@ public void constructorWhenRelyingPartyWithCredentialsThenItSucceeds() {
8887
new Saml2X509Credential(key, certificate, SIGNING);
8988
new Saml2X509Credential(key, certificate, SIGNING, DECRYPTION);
9089
new Saml2X509Credential(key, certificate, DECRYPTION);
90+
Saml2X509Credential.signing(key, certificate);
91+
Saml2X509Credential.decryption(key, certificate);
9192
}
9293

9394
@Test
9495
public void constructorWhenAssertingPartyWithCredentialsThenItSucceeds() {
9596
new Saml2X509Credential(certificate, VERIFICATION);
9697
new Saml2X509Credential(certificate, VERIFICATION, ENCRYPTION);
9798
new Saml2X509Credential(certificate, ENCRYPTION);
99+
Saml2X509Credential.verification(certificate);
100+
Saml2X509Credential.encryption(certificate);
98101
}
99102

100103
@Test
@@ -145,5 +148,51 @@ public void constructorWhenAssertingPartyWithDecryptionUsageThenItFails() {
145148
new Saml2X509Credential(certificate, DECRYPTION);
146149
}
147150

151+
@Test
152+
public void factoryWhenRelyingPartyForSigningWithoutCredentialsThenItFails() {
153+
exception.expect(IllegalArgumentException.class);
154+
Saml2X509Credential.signing(null, null);
155+
}
148156

157+
@Test
158+
public void factoryWhenRelyingPartyForSigningWithoutPrivateKeyThenItFails() {
159+
exception.expect(IllegalArgumentException.class);
160+
Saml2X509Credential.signing(null, certificate);
161+
}
162+
163+
@Test
164+
public void factoryWhenRelyingPartyForSigningWithoutCertificateThenItFails() {
165+
exception.expect(IllegalArgumentException.class);
166+
Saml2X509Credential.signing(key, null);
167+
}
168+
169+
@Test
170+
public void factoryWhenRelyingPartyForDecryptionWithoutCredentialsThenItFails() {
171+
exception.expect(IllegalArgumentException.class);
172+
Saml2X509Credential.decryption(null, null);
173+
}
174+
175+
@Test
176+
public void factoryWhenRelyingPartyForDecryptionWithoutPrivateKeyThenItFails() {
177+
exception.expect(IllegalArgumentException.class);
178+
Saml2X509Credential.decryption(null, certificate);
179+
}
180+
181+
@Test
182+
public void factoryWhenRelyingPartyForDecryptionWithoutCertificateThenItFails() {
183+
exception.expect(IllegalArgumentException.class);
184+
Saml2X509Credential.decryption(key, null);
185+
}
186+
187+
@Test
188+
public void factoryWhenAssertingPartyForVerificationWithoutCertificateThenItFails() {
189+
exception.expect(IllegalArgumentException.class);
190+
Saml2X509Credential.verification(null);
191+
}
192+
193+
@Test
194+
public void factoryWhenAssertingPartyForEncryptionWithoutCertificateThenItFails() {
195+
exception.expect(IllegalArgumentException.class);
196+
Saml2X509Credential.encryption(null);
197+
}
149198
}

0 commit comments

Comments
 (0)