Skip to content

Commit

Permalink
changed set- and isExpired from milliseconds to seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
timonson committed Jul 20, 2020
1 parent 8dbd60f commit f742488
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ interface Jose {
}

// Helper function: setExpiration()
// returns the number of milliseconds since January 1, 1970, 00:00:00 UTC
// returns the number of seconds since January 1, 1970, 00:00:00 UTC
function setExpiration(exp: number | Date): number {
return (exp instanceof Date ? exp : new Date(exp)).getTime();
return Math.round(
(exp instanceof Date ? exp : new Date(exp)).getTime() / 1000
);
}

function convertHexToBase64url(input: string): string {
Expand Down
4 changes: 2 additions & 2 deletions validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function has<K extends string>(
}

function isExpired(exp: number, leeway = 0): boolean {
return new Date(exp + leeway) < new Date();
return exp + leeway < new Date().getTime() / 1000;
}

// A present 'crit' header parameter indicates that the JWS signature validator
Expand Down Expand Up @@ -97,7 +97,7 @@ function validateJwtObject(
if (typeof maybeJwtObject.payload.exp !== "number") {
throw RangeError("claim 'exp' is not a number");
} // Implementers MAY provide for some small leeway to account for clock skew (JWT §4.1.4)
else if (isExpired(maybeJwtObject.payload.exp, 1000)) {
else if (isExpired(maybeJwtObject.payload.exp, 1)) {
throw RangeError("the jwt is expired");
}
}
Expand Down

0 comments on commit f742488

Please sign in to comment.