Merging Jwt.java back into the branch

pull/59/head
Michael Jett 2012-02-23 11:31:47 -05:00
parent f71ea2133c
commit b274098348
1 changed files with 147 additions and 147 deletions

View File

@ -1,147 +1,147 @@
package org.mitre.jwt.model; package org.mitre.jwt.model;
import java.util.List; import java.util.List;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
public class Jwt { public class Jwt {
private JwtHeader header; private JwtHeader header;
private JwtClaims claims; private JwtClaims claims;
/** /**
* Base64Url encoded signature string * Base64Url encoded signature string
*/ */
private String signature; private String signature;
public Jwt() { public Jwt() {
this.header = new JwtHeader(); this.header = new JwtHeader();
this.claims = new JwtClaims(); this.claims = new JwtClaims();
this.signature = null; // unsigned by default this.signature = null; // unsigned by default
} }
/** /**
* Create a Jwt from existing components * Create a Jwt from existing components
* @param header * @param header
* @param claims * @param claims
* @param signature * @param signature
*/ */
public Jwt(JwtHeader header, JwtClaims claims, String signature) { public Jwt(JwtHeader header, JwtClaims claims, String signature) {
super(); super();
this.header = header; this.header = header;
this.claims = claims; this.claims = claims;
this.signature = signature; this.signature = signature;
} }
/** /**
* @return the header * @return the header
*/ */
public JwtHeader getHeader() { public JwtHeader getHeader() {
return header; return header;
} }
/** /**
* @param header the header to set * @param header the header to set
*/ */
public void setHeader(JwtHeader header) { public void setHeader(JwtHeader header) {
this.header = header; this.header = header;
} }
/** /**
* @return the claims * @return the claims
*/ */
public JwtClaims getClaims() { public JwtClaims getClaims() {
return claims; return claims;
} }
/** /**
* @param claims the claims to set * @param claims the claims to set
*/ */
public void setClaims(JwtClaims claims) { public void setClaims(JwtClaims claims) {
this.claims = claims; this.claims = claims;
} }
/** /**
* @return the signature * @return the signature
*/ */
public String getSignature() { public String getSignature() {
return signature; return signature;
} }
/** /**
* @param signature the signature to set * @param signature the signature to set
*/ */
public void setSignature(String signature) { public void setSignature(String signature) {
this.signature = signature; this.signature = signature;
} }
/** /**
* Return the canonical encoded string of this JWT, the header in Base64, a period ".", the claims in Base64, a period ".", and the signature in Base64. * Return the canonical encoded string of this JWT, the header in Base64, a period ".", the claims in Base64, a period ".", and the signature in Base64.
*/ */
public String toString() { public String toString() {
return getSignatureBase() + "." + Strings.nullToEmpty(this.signature); return getSignatureBase() + "." + Strings.nullToEmpty(this.signature);
} }
/** /**
* The signature base of a JWT is the header in Base64, a period ".", and the claims in Base64. * The signature base of a JWT is the header in Base64, a period ".", and the claims in Base64.
*/ */
public String getSignatureBase() { public String getSignatureBase() {
JsonObject h = header.getAsJsonObject(); JsonObject h = header.getAsJsonObject();
JsonObject c = claims.getAsJsonObject(); JsonObject c = claims.getAsJsonObject();
String h64 = new String(Base64.encodeBase64URLSafe(h.toString().getBytes())); String h64 = new String(Base64.encodeBase64URLSafe(h.toString().getBytes()));
String c64 = new String(Base64.encodeBase64URLSafe(c.toString().getBytes())); String c64 = new String(Base64.encodeBase64URLSafe(c.toString().getBytes()));
return h64 + "." + c64; return h64 + "." + c64;
} }
/** /**
* Parse a wire-encoded JWT * Parse a wire-encoded JWT
*/ */
public static Jwt parse(String s) { public static Jwt parse(String s) {
// split on the dots // split on the dots
List<String> parts = Lists.newArrayList(Splitter.on(".").split(s)); List<String> parts = Lists.newArrayList(Splitter.on(".").split(s));
if (parts.size() != 3) { if (parts.size() != 3) {
throw new IllegalArgumentException("Invalid JWT format."); throw new IllegalArgumentException("Invalid JWT format.");
} }
String h64 = parts.get(0); String h64 = parts.get(0);
String c64 = parts.get(1); String c64 = parts.get(1);
String s64 = parts.get(2); String s64 = parts.get(2);
// shuttle for return value // shuttle for return value
Jwt jwt = new Jwt(new JwtHeader(h64), new JwtClaims(c64), s64); Jwt jwt = new Jwt(new JwtHeader(h64), new JwtClaims(c64), s64);
// TODO: save the wire-encoded string in the Jwt object itself? // TODO: save the wire-encoded string in the Jwt object itself?
return jwt; return jwt;
} }
} }