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

View File

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

View File

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