Skip to content

Add Static Factories to Saml2X509Credential #8822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,40 @@ public enum Saml2X509CredentialType {
private final X509Certificate certificate;
private final Set<Saml2X509CredentialType> credentialTypes;

/**
* Create a {@link Saml2X509Credential} that can be used for encryption.
* @param certificate the certificate to use for encryption
*/
public static Saml2X509Credential encryption(X509Certificate certificate) {
return new Saml2X509Credential(certificate, Saml2X509CredentialType.ENCRYPTION);
}

/**
* Create a {@link Saml2X509Credential} that can be used for verification.
* @param certificate the certificate to use for verification
*/
public static Saml2X509Credential verification(X509Certificate certificate) {
return new Saml2X509Credential(certificate, Saml2X509CredentialType.VERIFICATION);
}

/**
* Create a {@link Saml2X509Credential} that can be used for decryption.
* @param privateKey the private key to use for decryption
* @param certificate the certificate to use for decryption
*/
public static Saml2X509Credential decryption(PrivateKey privateKey, X509Certificate certificate) {
return new Saml2X509Credential(privateKey, certificate, Saml2X509CredentialType.DECRYPTION);
}

/**
* Create a {@link Saml2X509Credential} that can be used for signing.
* @param privateKey the private key to use for signing
* @param certificate the certificate to use for signing
*/
public static Saml2X509Credential signing(PrivateKey privateKey, X509Certificate certificate) {
return new Saml2X509Credential(privateKey, certificate, Saml2X509CredentialType.SIGNING);
}

/**
* Creates a Saml2X509Credentials representing Identity Provider credentials for
* verification, encryption or both.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

package org.springframework.security.saml2.credentials;

import org.springframework.security.converter.RsaKeyConverters;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.security.converter.RsaKeyConverters;

import java.io.ByteArrayInputStream;
import java.security.PrivateKey;
Expand Down Expand Up @@ -88,13 +87,17 @@ public void constructorWhenRelyingPartyWithCredentialsThenItSucceeds() {
new Saml2X509Credential(key, certificate, SIGNING);
new Saml2X509Credential(key, certificate, SIGNING, DECRYPTION);
new Saml2X509Credential(key, certificate, DECRYPTION);
Saml2X509Credential.signing(key, certificate);
Saml2X509Credential.decryption(key, certificate);
}

@Test
public void constructorWhenAssertingPartyWithCredentialsThenItSucceeds() {
new Saml2X509Credential(certificate, VERIFICATION);
new Saml2X509Credential(certificate, VERIFICATION, ENCRYPTION);
new Saml2X509Credential(certificate, ENCRYPTION);
Saml2X509Credential.verification(certificate);
Saml2X509Credential.encryption(certificate);
}

@Test
Expand Down Expand Up @@ -145,5 +148,51 @@ public void constructorWhenAssertingPartyWithDecryptionUsageThenItFails() {
new Saml2X509Credential(certificate, DECRYPTION);
}

@Test
public void factoryWhenRelyingPartyForSigningWithoutCredentialsThenItFails() {
exception.expect(IllegalArgumentException.class);
Saml2X509Credential.signing(null, null);
}

@Test
public void factoryWhenRelyingPartyForSigningWithoutPrivateKeyThenItFails() {
exception.expect(IllegalArgumentException.class);
Saml2X509Credential.signing(null, certificate);
}

@Test
public void factoryWhenRelyingPartyForSigningWithoutCertificateThenItFails() {
exception.expect(IllegalArgumentException.class);
Saml2X509Credential.signing(key, null);
}

@Test
public void factoryWhenRelyingPartyForDecryptionWithoutCredentialsThenItFails() {
exception.expect(IllegalArgumentException.class);
Saml2X509Credential.decryption(null, null);
}

@Test
public void factoryWhenRelyingPartyForDecryptionWithoutPrivateKeyThenItFails() {
exception.expect(IllegalArgumentException.class);
Saml2X509Credential.decryption(null, certificate);
}

@Test
public void factoryWhenRelyingPartyForDecryptionWithoutCertificateThenItFails() {
exception.expect(IllegalArgumentException.class);
Saml2X509Credential.decryption(key, null);
}

@Test
public void factoryWhenAssertingPartyForVerificationWithoutCertificateThenItFails() {
exception.expect(IllegalArgumentException.class);
Saml2X509Credential.verification(null);
}

@Test
public void factoryWhenAssertingPartyForEncryptionWithoutCertificateThenItFails() {
exception.expect(IllegalArgumentException.class);
Saml2X509Credential.encryption(null);
}
}