Skip to content

Commit

Permalink
Parse time claims as Number and convert to Long. (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsongraca authored Jan 17, 2024
1 parent efe9a0e commit 6320d93
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ protected Object getClaimValue(String claimName) {
case nbf:
case updated_at:
try {
claim = claimsSet.getClaimValue(claimType.name(), Long.class);
Number numberClaim = claimsSet.getClaimValue(claimType.name(), Number.class);
if (numberClaim != null) {
claim = numberClaim.longValue();
}
if (claim == null) {
claim = 0L;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.eclipse.microprofile.jwt.Claims;
import org.eclipse.microprofile.jwt.tck.util.TokenUtils;
import org.jose4j.jwt.JwtClaims;
import org.jose4j.jwt.consumer.JwtContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -57,4 +58,24 @@ void getAudienceClaim() {
assertEquals(1, audience.size());
assertArrayEquals(new String[] { TCK_TOKEN1_AUD }, audience.toArray(new String[0]));
}

@Test
void claimsWithDecimalValues() {
Double exp = 1311281970.5;
Double iat = 1311280970.5;

final JwtClaims claims = context.getJwtClaims();
claims.setClaim(Claims.exp.name(), exp);
claims.setClaim(Claims.iat.name(), iat);
DefaultJWTCallerPrincipal principal = new DefaultJWTCallerPrincipal(claims);

Long expClaim = principal.getExpirationTime();
Long iatClaim = principal.getIssuedAtTime();

assertNotNull(expClaim);
assertNotNull(iatClaim);

assertEquals(exp.longValue(), expClaim);
assertEquals(iat.longValue(), iatClaim);
}
}

0 comments on commit 6320d93

Please sign in to comment.