package at.gv.egovernment.moa.id.protocols.oauth20.json; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.Signature; import org.apache.commons.lang.StringUtils; /** * Enum of the signature algorithms supported by this package. */ public enum OAuthSignatureAlgorithm { ECDSA256("SHA256withECDSA", "ECDSA256", null), RS256("SHA256withRSA", "RS256", null), ECDSA256_IAKIK("SHA1withECDSA", "ECDSA256", "IAIK_ECC"); private final String signatureName; private final String algorithm; private final String providerName; private OAuthSignatureAlgorithm(final String signatureName, final String hashAlg, final String providerName) { this.signatureName = signatureName; this.algorithm = hashAlg; this.providerName = providerName; } /** * What the signature algorithm is named in the "alg" parameter in a JSON Token's envelope. */ public String getAlgorithm() { return this.algorithm; } /** * * @return the signature name like SHA256withECDSA or SHA256withRSA */ public String getSignatureName() { return this.signatureName; } /** * Calls {@link Signature#getInstance(String)} with the defined signature name * * @return * @throws NoSuchAlgorithmException * @throws NoSuchProviderException */ public Signature getSignatureInstance() throws NoSuchAlgorithmException, NoSuchProviderException { if (!StringUtils.isEmpty(this.providerName)) { //return Signature.getInstance(this.signatureName, this.providerName); return Signature.getInstance(this.signatureName, this.providerName); } else { return Signature.getInstance(this.signatureName); } } /** * Given the name of the algorithm in the envelope, returns the corresponding enum instance. */ public static OAuthSignatureAlgorithm getFromJsonName(String name) { return OAuthSignatureAlgorithm.valueOf(name); } }