refactored JWA algorithm markers to use enum instead of string as stored class
parent
165f3ea292
commit
319568d971
|
@ -25,24 +25,23 @@ import com.google.common.collect.Lists;
|
||||||
|
|
||||||
public abstract class AbstractJwtSigner implements JwtSigner {
|
public abstract class AbstractJwtSigner implements JwtSigner {
|
||||||
|
|
||||||
// TODO: make this a JwsAlgorithm enum value?
|
private JwsAlgorithm algorithm;
|
||||||
private String algorithm;
|
|
||||||
|
|
||||||
public AbstractJwtSigner(String algorithm) {
|
public AbstractJwtSigner(JwsAlgorithm algorithm) {
|
||||||
this.algorithm = algorithm;
|
this.algorithm = algorithm;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the algorithm
|
* @return the algorithm
|
||||||
*/
|
*/
|
||||||
public String getAlgorithm() {
|
public JwsAlgorithm getAlgorithm() {
|
||||||
return algorithm;
|
return algorithm;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param algorithm the algorithm to set
|
* @param algorithm the algorithm to set
|
||||||
*/
|
*/
|
||||||
public void setAlgorithm(String algorithm) {
|
public void setAlgorithm(JwsAlgorithm algorithm) {
|
||||||
this.algorithm = algorithm;
|
this.algorithm = algorithm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -76,4 +76,13 @@ public enum JwsAlgorithm {
|
||||||
public String getStandardName() {
|
public String getStandardName() {
|
||||||
return standardName;
|
return standardName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the JWA Standard name
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getJwaName() {
|
||||||
|
return jwaName;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ import org.mitre.jwt.model.Jwt;
|
||||||
|
|
||||||
public interface JwtSigner {
|
public interface JwtSigner {
|
||||||
|
|
||||||
public String getAlgorithm();
|
public JwsAlgorithm getAlgorithm();
|
||||||
|
|
||||||
public Jwt sign(Jwt jwt) throws NoSuchAlgorithmException;
|
public Jwt sign(Jwt jwt) throws NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
|
|
@ -47,8 +47,7 @@ public class HmacSigner extends AbstractJwtSigner implements InitializingBean {
|
||||||
|
|
||||||
public static final String DEFAULT_PASSPHRASE = "changeit";
|
public static final String DEFAULT_PASSPHRASE = "changeit";
|
||||||
|
|
||||||
public static final String DEFAULT_ALGORITHM = JwsAlgorithm.HS256
|
public static final JwsAlgorithm DEFAULT_ALGORITHM = JwsAlgorithm.HS256;
|
||||||
.toString();
|
|
||||||
|
|
||||||
private static Log logger = LogFactory.getLog(HmacSigner.class);
|
private static Log logger = LogFactory.getLog(HmacSigner.class);
|
||||||
|
|
||||||
|
@ -71,7 +70,7 @@ public class HmacSigner extends AbstractJwtSigner implements InitializingBean {
|
||||||
*/
|
*/
|
||||||
public HmacSigner(byte[] passphraseAsRawBytes)
|
public HmacSigner(byte[] passphraseAsRawBytes)
|
||||||
throws NoSuchAlgorithmException {
|
throws NoSuchAlgorithmException {
|
||||||
this(DEFAULT_ALGORITHM, new String(passphraseAsRawBytes,
|
this(DEFAULT_ALGORITHM.getJwaName(), new String(passphraseAsRawBytes,
|
||||||
Charset.forName("UTF-8")));
|
Charset.forName("UTF-8")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +81,7 @@ public class HmacSigner extends AbstractJwtSigner implements InitializingBean {
|
||||||
* The passphrase as raw bytes
|
* The passphrase as raw bytes
|
||||||
*/
|
*/
|
||||||
public HmacSigner(String passphrase) throws NoSuchAlgorithmException {
|
public HmacSigner(String passphrase) throws NoSuchAlgorithmException {
|
||||||
this(DEFAULT_ALGORITHM, passphrase);
|
this(DEFAULT_ALGORITHM.getJwaName(), passphrase);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -108,7 +107,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(algorithmName);
|
super(JwsAlgorithm.getByName(algorithmName));
|
||||||
|
|
||||||
Assert.notNull(passphrase, "A passphrase must be supplied");
|
Assert.notNull(passphrase, "A passphrase must be supplied");
|
||||||
|
|
||||||
|
@ -179,7 +178,7 @@ public class HmacSigner extends AbstractJwtSigner implements InitializingBean {
|
||||||
private void initializeMac() {
|
private void initializeMac() {
|
||||||
if (mac == null) {
|
if (mac == null) {
|
||||||
try {
|
try {
|
||||||
mac = Mac.getInstance(JwsAlgorithm.getByName(super.getAlgorithm()).getStandardName());
|
mac = Mac.getInstance(getAlgorithm().getStandardName());
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
@ -21,7 +21,7 @@ import org.mitre.jwt.signer.JwsAlgorithm;
|
||||||
public class PlaintextSigner extends AbstractJwtSigner {
|
public class PlaintextSigner extends AbstractJwtSigner {
|
||||||
|
|
||||||
public PlaintextSigner() {
|
public PlaintextSigner() {
|
||||||
super(JwsAlgorithm.NONE.toString());
|
super(JwsAlgorithm.NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -50,7 +50,7 @@ public class RsaSigner extends AbstractJwtSigner implements InitializingBean {
|
||||||
public static final String KEYPAIR_ALGORITHM = "RSA";
|
public static final String KEYPAIR_ALGORITHM = "RSA";
|
||||||
public static final String DEFAULT_PASSWORD = "changeit";
|
public static final String DEFAULT_PASSWORD = "changeit";
|
||||||
|
|
||||||
public static final String DEFAULT_ALGORITHM = JwsAlgorithm.RS256.toString();
|
public static final JwsAlgorithm DEFAULT_ALGORITHM = JwsAlgorithm.RS256;
|
||||||
|
|
||||||
private KeyStore keystore;
|
private KeyStore keystore;
|
||||||
private String alias;
|
private String alias;
|
||||||
|
@ -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(algorithmName);
|
super(JwsAlgorithm.getByName(algorithmName));
|
||||||
|
|
||||||
setKeystore(keystore);
|
setKeystore(keystore);
|
||||||
setAlias(alias);
|
setAlias(alias);
|
||||||
|
@ -115,14 +115,14 @@ public class RsaSigner extends AbstractJwtSigner implements InitializingBean {
|
||||||
* created with larger bit sizes obviously create larger signatures.
|
* created with larger bit sizes obviously create larger signatures.
|
||||||
*
|
*
|
||||||
* @param algorithmName
|
* @param algorithmName
|
||||||
* The algorithm name
|
* The JWA algorithm name
|
||||||
* @param publicKey
|
* @param publicKey
|
||||||
* The public key
|
* The public key
|
||||||
* @param privateKey
|
* @param privateKey
|
||||||
* The private key
|
* The private key
|
||||||
*/
|
*/
|
||||||
public RsaSigner(String algorithmName, PublicKey publicKey, PrivateKey privateKey) {
|
public RsaSigner(String algorithmName, PublicKey publicKey, PrivateKey privateKey) {
|
||||||
super(algorithmName);
|
super(JwsAlgorithm.getByName(algorithmName));
|
||||||
|
|
||||||
this.publicKey = publicKey;
|
this.publicKey = publicKey;
|
||||||
this.privateKey = privateKey;
|
this.privateKey = privateKey;
|
||||||
|
@ -235,7 +235,7 @@ public class RsaSigner extends AbstractJwtSigner implements InitializingBean {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (signer == null) {
|
if (signer == null) {
|
||||||
signer = Signature.getInstance(JwsAlgorithm.getByName(super.getAlgorithm()).getStandardName());
|
signer = Signature.getInstance(getAlgorithm().getStandardName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -136,7 +136,7 @@ public class JwtSigningAndValidationServiceDefault extends AbstractJwtSigningAnd
|
||||||
JwtSigner signer = getSigners().get(signerId);
|
JwtSigner signer = getSigners().get(signerId);
|
||||||
|
|
||||||
// set the signing algorithm in the JWT
|
// set the signing algorithm in the JWT
|
||||||
jwt.getHeader().setAlgorithm(signer.getAlgorithm());
|
jwt.getHeader().setAlgorithm(signer.getAlgorithm().getJwaName());
|
||||||
|
|
||||||
signer.sign(jwt);
|
signer.sign(jwt);
|
||||||
|
|
||||||
|
|
|
@ -73,7 +73,7 @@ public class Rsa256Test{
|
||||||
keyPair = keyGen.generateKeyPair();
|
keyPair = keyGen.generateKeyPair();
|
||||||
publicKey = keyPair.getPublic();
|
publicKey = keyPair.getPublic();
|
||||||
privateKey = keyPair.getPrivate();
|
privateKey = keyPair.getPrivate();
|
||||||
RsaSigner rsa = new RsaSigner(JwsAlgorithm.RS256.toString(), publicKey, privateKey);
|
RsaSigner rsa = new RsaSigner(JwsAlgorithm.RS256.getJwaName(), publicKey, privateKey);
|
||||||
jwt = rsa.sign(jwt);
|
jwt = rsa.sign(jwt);
|
||||||
assertEquals(rsa.verify(jwt.toString()), true);
|
assertEquals(rsa.verify(jwt.toString()), true);
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ public class Rsa384Test {
|
||||||
keyPair = keyGen.generateKeyPair();
|
keyPair = keyGen.generateKeyPair();
|
||||||
publicKey = keyPair.getPublic();
|
publicKey = keyPair.getPublic();
|
||||||
privateKey = keyPair.getPrivate();
|
privateKey = keyPair.getPrivate();
|
||||||
RsaSigner rsa = new RsaSigner(JwsAlgorithm.RS384.toString(), publicKey, privateKey);
|
RsaSigner rsa = new RsaSigner(JwsAlgorithm.RS384.getJwaName(), publicKey, privateKey);
|
||||||
jwt = rsa.sign(jwt);
|
jwt = rsa.sign(jwt);
|
||||||
assertEquals(rsa.verify(jwt.toString()), true);
|
assertEquals(rsa.verify(jwt.toString()), true);
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ public class Rsa512Test {
|
||||||
keyPair = keyGen.generateKeyPair();
|
keyPair = keyGen.generateKeyPair();
|
||||||
publicKey = keyPair.getPublic();
|
publicKey = keyPair.getPublic();
|
||||||
privateKey = keyPair.getPrivate();
|
privateKey = keyPair.getPrivate();
|
||||||
RsaSigner rsa = new RsaSigner(JwsAlgorithm.RS512.toString(), publicKey, privateKey);
|
RsaSigner rsa = new RsaSigner(JwsAlgorithm.RS512.getJwaName(), publicKey, privateKey);
|
||||||
jwt = rsa.sign(jwt);
|
jwt = rsa.sign(jwt);
|
||||||
assertEquals(rsa.verify(jwt.toString()), true);
|
assertEquals(rsa.verify(jwt.toString()), true);
|
||||||
|
|
||||||
|
|
|
@ -153,7 +153,7 @@ public class JwtTest {
|
||||||
jwt.getClaims().setIssuer("joe");
|
jwt.getClaims().setIssuer("joe");
|
||||||
jwt.getClaims().setClaim("http://example.com/is_root", Boolean.TRUE);
|
jwt.getClaims().setClaim("http://example.com/is_root", Boolean.TRUE);
|
||||||
|
|
||||||
JwtSigner signer = new RsaSigner(JwsAlgorithm.RS256.toString(), keystore, "testGenerateRsaSignature", RsaSigner.DEFAULT_PASSWORD);
|
JwtSigner signer = new RsaSigner(JwsAlgorithm.RS256.getJwaName(), keystore, "testGenerateRsaSignature", RsaSigner.DEFAULT_PASSWORD);
|
||||||
((RsaSigner) signer).afterPropertiesSet();
|
((RsaSigner) signer).afterPropertiesSet();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -191,7 +191,7 @@ public class JwtTest {
|
||||||
|
|
||||||
Jwt jwt = Jwt.parse(source);
|
Jwt jwt = Jwt.parse(source);
|
||||||
|
|
||||||
assertThat(jwt.getHeader().getAlgorithm(), equalTo(JwsAlgorithm.NONE.toString()));
|
assertThat(jwt.getHeader().getAlgorithm(), equalTo(JwsAlgorithm.NONE.getJwaName()));
|
||||||
assertThat(jwt.getClaims().getIssuer(), equalTo("joe"));
|
assertThat(jwt.getClaims().getIssuer(), equalTo("joe"));
|
||||||
assertThat(jwt.getClaims().getExpiration(), equalTo(new Date(1300819380L * 1000L)));
|
assertThat(jwt.getClaims().getExpiration(), equalTo(new Date(1300819380L * 1000L)));
|
||||||
assertThat((Boolean) jwt.getClaims().getClaim("http://example.com/is_root"), equalTo(Boolean.TRUE));
|
assertThat((Boolean) jwt.getClaims().getClaim("http://example.com/is_root"), equalTo(Boolean.TRUE));
|
||||||
|
|
Loading…
Reference in New Issue