-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"exp" spec claim - String parsing fallback does not allow UTC offsets #534
Comments
4.1.4. “exp” (Expiration Time) Claim
Allowing anything other than NumericDate is not spec compliant. |
I use the following code to test Map<String, Object> headerMap = new LinkedHashMap<>();
headerMap.put("alg","HS512");
headerMap.put("typ","JWT");
Map<String, Object> payload = new LinkedHashMap<>();
payload.put("accout","admin");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(calendar.MINUTE, 15);
long exp = calendar.getTimeInMillis();
System.out.println("JWT creation time is " + exp);
payload.put("exp", exp);
String compactJws = Jwts.builder().setHeader(headerMap)
.setPayload(JSONObject.toJSONString(payload))
.signWith(JWTConstant.KEY)
.compact();
Jws<Claims> claimsJws = Jwts.parser().setSigningKey(JWTConstant.KEY).parseClaimsJws(compactJws);
Claims body = claimsJws.getBody();
Date expDate = body.getExpiration();
System.out.println(expDate.getTime()); Why does 'exp' increase a thousand times? |
@jiangyongbing24 because java.util.Date works with miliseconds, whereas the JWT is epoch-second based, so whatever you pass it is expected to be second, and the library multiplies it by 1000 to follow the java convention. I recommend you to use setExpiration method which takes the java.util.Date |
@jiangyongbing24 your own quote gives you the answer:
not milliseconds. https://stackoverflow.com/questions/39926104/what-format-is-the-exp-expiration-time-claim-in-a-jwt |
Adding this to the 1.0 milestone because it is not a backwards-compatible change (and also marked as |
@lhazlewood same applies for |
"exp" claim should be number, however jjwt supports parsing the string as a number, and as last fallback parsing string as a ISO_8601 formatted date.
In io.jsonwebtoken.lang.DateFormats
there are following patterns
So they are named here as ISO_8601 and if can't parse the exception says it is not ISO_8601 formatted.
Despite that ISO_8601 allows you to have the offset
https://en.wikipedia.org/wiki/ISO_8601
The patterns from DateFormats will accept only 'Z' value.
Therefore values like 2019-11-26T15:54:13.100-0800 are not accepted, resulting in ParseException->IllegalArgumentException.
Another point is that it throws IllegalArgumentException, which does not inherit from JwtException, not intuitively this may result in non-caught exception.
In my understanding the fix would be to change the patterns to following (removing the single quotes around Z letter) :
and throwing MalformedSpecificationClaimException extends JwtException instead of IllegalArgumentException.
The text was updated successfully, but these errors were encountered: