Skip to content

Commit

Permalink
Added time.getUnixTimeInSeconds; Fix for verifyJWT comparing exp with…
Browse files Browse the repository at this point in the history
… time since unix epoch in seconds as per RFC7519
  • Loading branch information
AIRoboCoder committed Nov 10, 2024
1 parent 6f36fbf commit bec2dbb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crypto/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const verifyJwt = async (publicKey, jwt) => {
throw new Error('Invalid JWT')
}
const payload = _parse(payloadBase64)
if (payload.exp != null && time.getUnixTime() > payload.exp) {
if (payload.exp != null && time.getUnixTimeInSeconds() > payload.exp) {
throw new Error('Expired JWT')
}
return {
Expand Down
7 changes: 7 additions & 0 deletions time.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ export const getDate = () => new Date()
*/
export const getUnixTime = Date.now

/**
* Return current Unix time in seconds (since epoch).
*
* @return {number} current Unix time in seconds
*/
export const getUnixTimeInSeconds = () => Math.floor(Date.now() / 1000)

/**
* Transform time (in ms) to a human readable format. E.g. 1100 => 1.1s. 60s => 1min. .001 => 10μs.
*
Expand Down

0 comments on commit bec2dbb

Please sign in to comment.