Simplified type checking.

This commit is contained in:
Micah Silverman 2016-06-27 09:05:44 -04:00
parent 29a33c3407
commit 14905fae11
1 changed files with 14 additions and 19 deletions

View File

@ -41,31 +41,31 @@ public class DynamicJWTController extends BaseController {
claims.forEach((key, value) -> { claims.forEach((key, value) -> {
switch (key) { switch (key) {
case "iss": case "iss":
ensureType(key, value, String.class); ensureType(key, value, String.class);
builder.setIssuer((String) value); builder.setIssuer((String) value);
break; break;
case "sub": case "sub":
ensureType(key, value, String.class); ensureType(key, value, String.class);
builder.setSubject((String) value); builder.setSubject((String) value);
break; break;
case "aud": case "aud":
ensureType(key, value, String.class); ensureType(key, value, String.class);
builder.setAudience((String) value); builder.setAudience((String) value);
break; break;
case "exp": case "exp":
value = ensureType(key, value, Long.class); ensureType(key, value, Long.class);
builder.setExpiration(Date.from(Instant.ofEpochSecond((Long) value))); builder.setExpiration(Date.from(Instant.ofEpochSecond(Long.parseLong(value.toString()))));
break; break;
case "nbf": case "nbf":
value = ensureType(key, value, Long.class); ensureType(key, value, Long.class);
builder.setNotBefore(Date.from(Instant.ofEpochSecond((Long) value))); builder.setNotBefore(Date.from(Instant.ofEpochSecond(Long.parseLong(value.toString()))));
break; break;
case "iat": case "iat":
value = ensureType(key, value, Long.class); ensureType(key, value, Long.class);
builder.setIssuedAt(Date.from(Instant.ofEpochSecond((Long) value))); builder.setIssuedAt(Date.from(Instant.ofEpochSecond(Long.parseLong(value.toString()))));
break; break;
case "jti": case "jti":
ensureType(key, value, String.class); ensureType(key, value, String.class);
builder.setId((String) value); builder.setId((String) value);
break; break;
default: default:
@ -78,20 +78,15 @@ public class DynamicJWTController extends BaseController {
return new JwtResponse(builder.compact()); return new JwtResponse(builder.compact());
} }
private Object ensureType(String registeredClaim, Object value, Class expectedType) { private void ensureType(String registeredClaim, Object value, Class expectedType) {
// we want to promote Integers to Longs in this case boolean isCorrectType =
if (expectedType == Long.class && value instanceof Integer) { expectedType.isInstance(value) ||
value = ((Integer) value).longValue(); expectedType == Long.class && value instanceof Integer;
}
boolean isCorrectType = expectedType.isInstance(value);
if (!isCorrectType) { if (!isCorrectType) {
String msg = "Expected type: " + expectedType.getCanonicalName() + " for registered claim: '" + String msg = "Expected type: " + expectedType.getCanonicalName() + " for registered claim: '" +
registeredClaim + "', but got value: " + value + " of type: " + value.getClass().getCanonicalName(); registeredClaim + "', but got value: " + value + " of type: " + value.getClass().getCanonicalName();
throw new JwtException(msg); throw new JwtException(msg);
} }
return value;
} }
} }