added jwt string stability to several places, fixed jwe parser

pull/165/merge
Justin Richer 2012-07-31 14:04:10 -04:00
parent 7c33e19950
commit 164090e9d5
3 changed files with 22 additions and 9 deletions

View File

@ -40,6 +40,7 @@ public class Jwe extends Jwt {
this.ciphertext = ciphertext; this.ciphertext = ciphertext;
} }
/*
public Jwe(String headerBase64, String encryptedKeyBase64, String cipherTextBase64, String integrityValueBase64) { public Jwe(String headerBase64, String encryptedKeyBase64, String cipherTextBase64, String integrityValueBase64) {
byte[] decodedEncryptedKey = Base64.decodeBase64(encryptedKeyBase64.getBytes()); byte[] decodedEncryptedKey = Base64.decodeBase64(encryptedKeyBase64.getBytes());
byte[] decodedCipherText = Base64.decodeBase64(cipherTextBase64.getBytes()); byte[] decodedCipherText = Base64.decodeBase64(cipherTextBase64.getBytes());
@ -48,6 +49,7 @@ public class Jwe extends Jwt {
this.ciphertext = decodedCipherText; this.ciphertext = decodedCipherText;
setSignature(integrityValueBase64); setSignature(integrityValueBase64);
} }
*/
public JweHeader getHeader() { public JweHeader getHeader() {
return header; return header;
@ -75,11 +77,10 @@ public class Jwe extends Jwt {
@Override @Override
public String getSignatureBase() { public String getSignatureBase() {
JsonObject h = header.getAsJsonObject();
byte[] c = ciphertext; byte[] c = ciphertext;
byte[] e = encryptedKey; byte[] e = encryptedKey;
String h64 = new String(Base64.encodeBase64URLSafe(h.toString().getBytes())); String h64 = new String(Base64.encodeBase64URLSafe(header.toJsonString().getBytes()));
String e64 = new String(Base64.encodeBase64URLSafe(e)); String e64 = new String(Base64.encodeBase64URLSafe(e));
String c64 = new String(Base64.encodeBase64URLSafe(c)); String c64 = new String(Base64.encodeBase64URLSafe(c));
@ -106,7 +107,10 @@ public class Jwe extends Jwt {
String c64 = parts.get(2); String c64 = parts.get(2);
String i64 = parts.get(3); String i64 = parts.get(3);
Jwe jwe = new Jwe(h64, e64, c64, i64); byte[] decodedEncryptedKey = Base64.decodeBase64(e64.getBytes());
byte[] decodedCipherText = Base64.decodeBase64(c64.getBytes());
Jwe jwe = new Jwe(new JweHeader(h64), decodedEncryptedKey, decodedCipherText, i64);
return jwe; return jwe;

View File

@ -96,7 +96,7 @@ public class ClaimSet {
* Set an extension claim * Set an extension claim
*/ */
public void setClaim(String key, Object value) { public void setClaim(String key, Object value) {
jsonString = null; invalidateString();
claims.put(key, value); claims.put(key, value);
} }
@ -104,7 +104,7 @@ public class ClaimSet {
* Set a primitive claim * Set a primitive claim
*/ */
public void setClaim(String key, JsonPrimitive prim) { public void setClaim(String key, JsonPrimitive prim) {
jsonString = null; invalidateString();
if (prim == null) { if (prim == null) {
// in case we get here with a primitive null // in case we get here with a primitive null
claims.put(key, prim); claims.put(key, prim);
@ -116,12 +116,17 @@ public class ClaimSet {
claims.put(key, prim.getAsString()); claims.put(key, prim.getAsString());
} }
}
private void invalidateString() {
jsonString = null;
} }
/** /**
* Remove an extension claim * Remove an extension claim
*/ */
public Object removeClaim(String key) { public Object removeClaim(String key) {
invalidateString();
return claims.remove(key); return claims.remove(key);
} }
@ -131,6 +136,7 @@ public class ClaimSet {
* @see java.util.Map#clear() * @see java.util.Map#clear()
*/ */
public void clear() { public void clear() {
invalidateString();
claims.clear(); claims.clear();
} }
@ -197,7 +203,7 @@ public class ClaimSet {
} }
/** /**
* Load a new claims set from a Base64 encoded JSON Object string * Load a new claims set from a Base64 encoded JSON Object string and caches the string used
*/ */
public void loadFromBase64JsonObjectString(String b64) { public void loadFromBase64JsonObjectString(String b64) {
byte[] b64decoded = Base64.decodeBase64(b64); byte[] b64decoded = Base64.decodeBase64(b64);
@ -206,9 +212,12 @@ public class ClaimSet {
JsonObject json = parser.parse(new InputStreamReader(new ByteArrayInputStream(b64decoded))).getAsJsonObject(); JsonObject json = parser.parse(new InputStreamReader(new ByteArrayInputStream(b64decoded))).getAsJsonObject();
loadFromJsonObject(json); loadFromJsonObject(json);
// save the string we were passed in (decoded from base64)
jsonString = new String(b64decoded);
} }
public String toString() { public String toJsonString() {
if(jsonString == null) { if(jsonString == null) {
jsonString = this.getAsJsonObject().toString(); jsonString = this.getAsJsonObject().toString();
} }

View File

@ -124,8 +124,8 @@ public class Jwt {
*/ */
public String getSignatureBase() { public String getSignatureBase() {
String h64 = new String(Base64.encodeBase64URLSafe(header.toString().getBytes())); String h64 = new String(Base64.encodeBase64URLSafe(header.toJsonString().getBytes()));
String c64 = new String(Base64.encodeBase64URLSafe(claims.toString().getBytes())); String c64 = new String(Base64.encodeBase64URLSafe(claims.toJsonString().getBytes()));
return h64 + "." + c64; return h64 + "." + c64;
} }