From 165f3ea292d7852126866b657ed673f5eb97c8c2 Mon Sep 17 00:00:00 2001 From: Justin Richer Date: Mon, 23 Jul 2012 18:44:47 -0400 Subject: [PATCH] fixed some unit tests, broke others --- .../org/mitre/jwt/signer/JwsAlgorithm.java | 20 ++++++----- .../src/test/java/org/mitre/jwt/JwtTest.java | 34 +++++++++++++++++-- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/openid-connect-common/src/main/java/org/mitre/jwt/signer/JwsAlgorithm.java b/openid-connect-common/src/main/java/org/mitre/jwt/signer/JwsAlgorithm.java index 7745442c2..5c847cdb9 100644 --- a/openid-connect-common/src/main/java/org/mitre/jwt/signer/JwsAlgorithm.java +++ b/openid-connect-common/src/main/java/org/mitre/jwt/signer/JwsAlgorithm.java @@ -26,16 +26,16 @@ import org.apache.commons.lang.StringUtils; public enum JwsAlgorithm { // PLAINTEXT - NONE("plaintext"), + NONE("plaintext", "none"), // HMAC - HS256("HMACSHA256"), - HS384("HMACSHA384"), - HS512("HMACSHA512"), + HS256("HMACSHA256", "HS256"), + HS384("HMACSHA384", "HS384"), + HS512("HMACSHA512", "HS512"), // RSA - RS256("SHA256withRSA"), - RS384("SHA384withRSA"), - RS512("SHA512withRSA"); + RS256("SHA256withRSA", "RS256"), + RS384("SHA384withRSA", "RS384"), + RS512("SHA512withRSA", "RS512"); /** * Returns the Algorithm for the name @@ -56,14 +56,16 @@ public enum JwsAlgorithm { } private final String standardName; - + private final String jwaName; + /** * Constructor of JwsAlgorithm * * @param standardName */ - JwsAlgorithm(String standardName) { + JwsAlgorithm(String standardName, String jwaName) { this.standardName = standardName; + this.jwaName = jwaName; } /** diff --git a/openid-connect-server/src/test/java/org/mitre/jwt/JwtTest.java b/openid-connect-server/src/test/java/org/mitre/jwt/JwtTest.java index caefa4104..257f2d2e7 100644 --- a/openid-connect-server/src/test/java/org/mitre/jwt/JwtTest.java +++ b/openid-connect-server/src/test/java/org/mitre/jwt/JwtTest.java @@ -29,6 +29,7 @@ import java.security.spec.RSAPrivateKeySpec; import java.security.spec.RSAPublicKeySpec; import java.util.Date; +import org.bouncycastle.jce.X509Principal; import org.bouncycastle.x509.X509V3CertificateGenerator; import org.junit.Test; import org.junit.runner.RunWith; @@ -39,7 +40,6 @@ import org.mitre.jwt.signer.impl.HmacSigner; import org.mitre.jwt.signer.impl.PlaintextSigner; import org.mitre.jwt.signer.impl.RsaSigner; import org.mitre.jwt.signer.service.impl.KeyStore; -import org.mitre.jwt.signer.service.impl.KeyStoreTest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -132,7 +132,7 @@ public class JwtTest { // BC sez X509V3CertificateGenerator is deprecated and the docs say to // use another, but it seemingly isn't included jar... - X509V3CertificateGenerator v3CertGen = KeyStoreTest.createCertificate("testGenerateRsaSignature", 30, 30); + X509V3CertificateGenerator v3CertGen = createCertificate("testGenerateRsaSignature", 30, 30); v3CertGen.setPublicKey(publicKey); v3CertGen.setSignatureAlgorithm("SHA256WithRSAEncryption"); @@ -191,7 +191,7 @@ public class JwtTest { Jwt jwt = Jwt.parse(source); - assertThat(jwt.getHeader().getAlgorithm(), equalTo(PlaintextSigner.PLAINTEXT)); + assertThat(jwt.getHeader().getAlgorithm(), equalTo(JwsAlgorithm.NONE.toString())); assertThat(jwt.getClaims().getIssuer(), equalTo("joe")); assertThat(jwt.getClaims().getExpiration(), equalTo(new Date(1300819380L * 1000L))); assertThat((Boolean) jwt.getClaims().getClaim("http://example.com/is_root"), equalTo(Boolean.TRUE)); @@ -271,5 +271,33 @@ public class JwtTest { e.printStackTrace(); } } + + + /** + * Creates a certificate. + * + * @param commonName + * @param daysNotValidBefore + * @param daysNotValidAfter + * @return + */ + public static X509V3CertificateGenerator createCertificate( + String commonName, int daysNotValidBefore, int daysNotValidAfter) { + // BC sez X509V3CertificateGenerator is deprecated and the docs say to + // use another, but it seemingly isn't included jar... + X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator(); + + v3CertGen + .setSerialNumber(BigInteger.valueOf(System.currentTimeMillis())); + v3CertGen.setIssuerDN(new X509Principal("CN=" + commonName + + ", OU=None, O=None L=None, C=None")); + v3CertGen.setNotBefore(new Date(System.currentTimeMillis() + - (1000L * 60 * 60 * 24 * daysNotValidBefore))); + v3CertGen.setNotAfter(new Date(System.currentTimeMillis() + + (1000L * 60 * 60 * 24 * daysNotValidAfter))); + v3CertGen.setSubjectDN(new X509Principal("CN=" + commonName + + ", OU=None, O=None L=None, C=None")); + return v3CertGen; + } }