Re-arranged packages in the JWT library; added an implementation of the

ES256 signature method (untested) and a stub for the RE256 signature
method.
pull/59/head
Amanda Anganes 2011-12-28 21:26:11 -05:00
parent 204df8f0bd
commit 15f8675e1a
11 changed files with 1053 additions and 938 deletions

View File

@ -1,4 +1,4 @@
package org.mitre.jwt;
package org.mitre.jwt.model;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;

View File

@ -1,4 +1,4 @@
package org.mitre.jwt;
package org.mitre.jwt.model;
import java.text.DateFormat;
import java.text.ParseException;

View File

@ -1,4 +1,4 @@
package org.mitre.jwt;
package org.mitre.jwt.model;
import java.util.HashMap;
import java.util.Map;

View File

@ -1,7 +1,9 @@
package org.mitre.jwt;
package org.mitre.jwt.signer;
import java.util.List;
import org.mitre.jwt.model.Jwt;
import com.google.common.base.Objects;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;

View File

@ -1,4 +1,6 @@
package org.mitre.jwt;
package org.mitre.jwt.signer;
import org.mitre.jwt.model.Jwt;
public interface JwtSigner {

View File

@ -0,0 +1,18 @@
package org.mitre.jwt.signer.impl;
import org.mitre.jwt.signer.AbstractJwtSigner;
public class Es256Signer extends AbstractJwtSigner {
public Es256Signer(String algorithm) {
super(algorithm);
// TODO Auto-generated constructor stub
}
@Override
protected String generateSignature(String signatureBase) {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -1,4 +1,4 @@
package org.mitre.jwt;
package org.mitre.jwt.signer.impl;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
@ -9,6 +9,7 @@ import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.mitre.jwt.signer.AbstractJwtSigner;
public class Hmac256Signer extends AbstractJwtSigner {

View File

@ -1,4 +1,6 @@
package org.mitre.jwt;
package org.mitre.jwt.signer.impl;
import org.mitre.jwt.signer.AbstractJwtSigner;
public class PlaintextSigner extends AbstractJwtSigner {

View File

@ -0,0 +1,85 @@
package org.mitre.jwt.signer.impl;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.SignatureException;
import org.apache.commons.codec.binary.Base64;
import org.mitre.jwt.signer.AbstractJwtSigner;
/**
* JWT Signer using RSA SHA-256 algorithm
* @author AANGANES
*
*/
public class Rs256Signer extends AbstractJwtSigner {
//TODO: should this class generate a new private key or get one passed into the constructor?
private PrivateKey privateKey;
private Signature signer;
public Rs256Signer() {
this(null);
}
public Rs256Signer(PrivateKey privateKey) {
super("RS256");
setPrivateKey(privateKey);
try {
signer = Signature.getInstance("SHA256withRSA");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected String generateSignature(String signatureBase) {
try {
signer.initSign(privateKey);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
signer.update(signatureBase.getBytes("UTF-8"));
} catch (SignatureException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] sigBytes;
String sig = "";
try {
sigBytes = signer.sign();
sig = new String(Base64.encodeBase64URLSafe(sigBytes));
// strip off any padding
sig = sig.replace("=", "");
} catch (SignatureException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sig;
}
public PrivateKey getPrivateKey() {
return privateKey;
}
public void setPrivateKey(PrivateKey privateKey) {
this.privateKey = privateKey;
}
}

View File

@ -2,7 +2,7 @@ package org.mitre.openid.connect.model;
import javax.persistence.Entity;
import org.mitre.jwt.Jwt;
import org.mitre.jwt.model.Jwt;
/*
* TODO: This class needs to be encoded as a JWT

View File

@ -8,6 +8,11 @@ import java.util.Date;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mitre.jwt.model.Jwt;
import org.mitre.jwt.signer.AbstractJwtSigner;
import org.mitre.jwt.signer.JwtSigner;
import org.mitre.jwt.signer.impl.Hmac256Signer;
import org.mitre.jwt.signer.impl.PlaintextSigner;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class JwtTest {