updated jwtHeader typ to use an enum
parent
95dcb10472
commit
3b2268c622
|
@ -15,6 +15,7 @@
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package org.mitre.jwt.model;
|
package org.mitre.jwt.model;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
|
@ -59,7 +60,12 @@ public class JwtHeader extends ClaimSet {
|
||||||
if (element.getValue().isJsonNull()) {
|
if (element.getValue().isJsonNull()) {
|
||||||
pass.add(element.getKey(), element.getValue());
|
pass.add(element.getKey(), element.getValue());
|
||||||
} else if (element.getKey().equals(TYPE)) {
|
} else if (element.getKey().equals(TYPE)) {
|
||||||
|
try {
|
||||||
this.setType(element.getValue().getAsString());
|
this.setType(element.getValue().getAsString());
|
||||||
|
} catch (ParseException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
} else if (element.getKey().equals(ALGORITHM)) {
|
} else if (element.getKey().equals(ALGORITHM)) {
|
||||||
this.setAlgorithm(element.getValue().getAsString());
|
this.setAlgorithm(element.getValue().getAsString());
|
||||||
} else if (element.getKey().equals(ENCRYPTION_METHOD)) {
|
} else if (element.getKey().equals(ENCRYPTION_METHOD)) {
|
||||||
|
@ -85,9 +91,13 @@ public class JwtHeader extends ClaimSet {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param type the type to set
|
* @param type the type to set
|
||||||
|
* @throws ParseException
|
||||||
*/
|
*/
|
||||||
public void setType(String type) {
|
public void setType(String type) throws ParseException {
|
||||||
setClaim(TYPE, type);
|
if(type == null) {
|
||||||
|
throw new NullPointerException("JWT header type value must not be null");
|
||||||
|
}
|
||||||
|
setClaim(TYPE, Type.parse(type));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
package org.mitre.jwt.model;
|
||||||
|
|
||||||
|
public enum Type {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type ({@code typ}) parameter indicating a JWT.
|
||||||
|
*
|
||||||
|
* <p>Corresponds to the follwoing {@code typ} values:
|
||||||
|
*
|
||||||
|
* <ul>
|
||||||
|
* <li>"JWT"
|
||||||
|
* <li>"urn:ietf:params:oauth:token-type:jwt"
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
JWT,
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type ({@code typ}) parameter indicating a nested JWS.
|
||||||
|
*
|
||||||
|
* <p>Corresponds to the following {@code typ} value:
|
||||||
|
*
|
||||||
|
* <ul>
|
||||||
|
* <li>"JWS"
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
JWS,
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type ({@code typ}) parameter indicating a nested JWE.
|
||||||
|
*
|
||||||
|
* <p>Corresponds to the follwoing {@code typ} value:
|
||||||
|
*
|
||||||
|
* <ul>
|
||||||
|
* <li>"JWE"
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
JWE;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses the specified type string (case sensitive).
|
||||||
|
*
|
||||||
|
* <p>Note that both "JWT" and
|
||||||
|
* "urn:ietf:params:oauth:token-type:jwt" resolve to
|
||||||
|
* {@link #JWT}.
|
||||||
|
*
|
||||||
|
* @param s The string to parse.
|
||||||
|
*
|
||||||
|
* @throws java.text.ParseException If the string couldn't be
|
||||||
|
* parsed to a supported JWT
|
||||||
|
* header type.
|
||||||
|
*/
|
||||||
|
public static Type parse(final String s)
|
||||||
|
throws java.text.ParseException {
|
||||||
|
|
||||||
|
if (s == null)
|
||||||
|
throw new NullPointerException("The parsed JWT header \"typ\" value must not be null");
|
||||||
|
|
||||||
|
if (s.equals("JWT") || s.equals("urn:ietf:params:oauth:token-type:jwt"))
|
||||||
|
return JWT;
|
||||||
|
|
||||||
|
if (s.equals("JWS"))
|
||||||
|
return JWS;
|
||||||
|
|
||||||
|
if (s.equals("JWE"))
|
||||||
|
return JWE;
|
||||||
|
|
||||||
|
throw new java.text.ParseException("Unsupported JWT header \"typ\" value: " + s, 0);
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,6 +27,7 @@ import java.security.PublicKey;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
import java.security.spec.RSAPrivateKeySpec;
|
import java.security.spec.RSAPrivateKeySpec;
|
||||||
import java.security.spec.RSAPublicKeySpec;
|
import java.security.spec.RSAPublicKeySpec;
|
||||||
|
import java.text.ParseException;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import org.bouncycastle.jce.X509Principal;
|
import org.bouncycastle.jce.X509Principal;
|
||||||
|
@ -57,7 +58,12 @@ public class JwtTest {
|
||||||
@Test
|
@Test
|
||||||
public void testGenerateHmacSignature() {
|
public void testGenerateHmacSignature() {
|
||||||
Jwt jwt = new Jwt();
|
Jwt jwt = new Jwt();
|
||||||
|
try {
|
||||||
jwt.getHeader().setType("JWT");
|
jwt.getHeader().setType("JWT");
|
||||||
|
} catch (ParseException e1) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
jwt.getHeader().setAlgorithm("HS256");
|
jwt.getHeader().setAlgorithm("HS256");
|
||||||
jwt.getClaims().setExpiration(new Date(1300819380L * 1000L));
|
jwt.getClaims().setExpiration(new Date(1300819380L * 1000L));
|
||||||
jwt.getClaims().setIssuer("joe");
|
jwt.getClaims().setIssuer("joe");
|
||||||
|
|
Loading…
Reference in New Issue