Skip to content

Commit

Permalink
Parse time claims as Number and convert to Long.
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsongraca committed Jan 16, 2024
1 parent d98c36e commit 73aac69
Show file tree
Hide file tree
Showing 2 changed files with 32 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,31 @@ void getAudienceClaim() {
assertEquals(1, audience.size());
assertArrayEquals(new String[] { TCK_TOKEN1_AUD }, audience.toArray(new String[0]));
}

@Test
void claimsWithDecimalValues() {
//time claims are not read from the json so we need to build our own
// long currentTimeInSecs = TokenUtils.currentTimeInSecs();
Double exp = 1311281970.5; //(double) (currentTimeInSecs + 300L);
Double iat = 1311280970.5; //(double) currentTimeInSecs;
Double authTime = 1311280969.5; //(double) currentTimeInSecs;

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

Long expClaim = principal.getClaim(Claims.exp.name());
Long iatClaim = principal.getClaim(Claims.iat.name());
Long authTimeClaim = principal.getClaim(Claims.auth_time.name());

assertNotNull(expClaim);
assertNotNull(iatClaim);
assertNotNull(authTimeClaim);

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

0 comments on commit 73aac69

Please sign in to comment.