Fixed JWS algorithm parsing

pull/210/head
Justin Richer 2012-08-27 15:58:23 -04:00
parent aeb6644d38
commit 1c34f83297
3 changed files with 21 additions and 12 deletions

View File

@ -15,6 +15,9 @@
******************************************************************************/
package org.mitre.jwt.signer;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
/**
@ -37,6 +40,15 @@ public enum JwsAlgorithm {
RS384("SHA384withRSA", "RS384"),
RS512("SHA512withRSA", "RS512");
private static final Map<String, JwsAlgorithm> jwaLookup = new HashMap<String, JwsAlgorithm>();
private static final Map<String, JwsAlgorithm> jceLookup = new HashMap<String, JwsAlgorithm>();
static {
for (JwsAlgorithm alg : JwsAlgorithm.values()) {
jwaLookup.put(alg.getJwaName(), alg);
jceLookup.put(alg.getStandardName(), alg);
}
}
/**
* Returns the Algorithm for the JWS-registered name
@ -44,15 +56,12 @@ public enum JwsAlgorithm {
* @param name
* @return
*/
public static JwsAlgorithm getByName(String name) {
for (JwsAlgorithm correspondingType : JwsAlgorithm.values()) {
if (correspondingType.toString().equals(name)) {
return correspondingType;
}
}
// corresponding type not found
throw new IllegalArgumentException("JwsAlgorithm name " + name + " does not have a corresponding JwsAlgorithm: expected one of [" + StringUtils.join(JwsAlgorithm.values(), ", ") + "]");
public static JwsAlgorithm getByJwaName(String name) {
return jwaLookup.get(name);
}
public static JwsAlgorithm getByStandardName(String name) {
return jceLookup.get(name);
}
private final String standardName;

View File

@ -101,7 +101,7 @@ public class HmacSigner extends AbstractJwtSigner implements InitializingBean {
* the passphrase
*/
public HmacSigner(String algorithmName, String passphrase) {
super(JwsAlgorithm.getByName(algorithmName));
super(JwsAlgorithm.getByJwaName(algorithmName));
Assert.notNull(passphrase, "A passphrase must be supplied");

View File

@ -101,7 +101,7 @@ public class RsaSigner extends AbstractJwtSigner implements InitializingBean {
* @throws GeneralSecurityException
*/
public RsaSigner(String algorithmName, KeyStore keystore, String alias, String password) throws GeneralSecurityException {
super(JwsAlgorithm.getByName(algorithmName));
super(JwsAlgorithm.getByJwaName(algorithmName));
setKeystore(keystore);
setAlias(alias);
@ -122,7 +122,7 @@ public class RsaSigner extends AbstractJwtSigner implements InitializingBean {
* The private key
*/
public RsaSigner(String algorithmName, PublicKey publicKey, PrivateKey privateKey) {
super(JwsAlgorithm.getByName(algorithmName));
super(JwsAlgorithm.getByJwaName(algorithmName));
this.publicKey = publicKey;
this.privateKey = privateKey;