Skip to content

Commit bec2dbb

Browse files
author
AIRoboCoder
committed
Added time.getUnixTimeInSeconds; Fix for verifyJWT comparing exp with time since unix epoch in seconds as per RFC7519
1 parent 6f36fbf commit bec2dbb

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

crypto/jwt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const verifyJwt = async (publicKey, jwt) => {
4747
throw new Error('Invalid JWT')
4848
}
4949
const payload = _parse(payloadBase64)
50-
if (payload.exp != null && time.getUnixTime() > payload.exp) {
50+
if (payload.exp != null && time.getUnixTimeInSeconds() > payload.exp) {
5151
throw new Error('Expired JWT')
5252
}
5353
return {

time.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ export const getDate = () => new Date()
2121
*/
2222
export const getUnixTime = Date.now
2323

24+
/**
25+
* Return current Unix time in seconds (since epoch).
26+
*
27+
* @return {number} current Unix time in seconds
28+
*/
29+
export const getUnixTimeInSeconds = () => Math.floor(Date.now() / 1000)
30+
2431
/**
2532
* Transform time (in ms) to a human readable format. E.g. 1100 => 1.1s. 60s => 1min. .001 => 10μs.
2633
*

0 commit comments

Comments
 (0)