diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..30de70fe7 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.acceptSuggestionOnEnter": "on" +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index bef4cfff4..fbd77d3a1 100644 --- a/src/index.js +++ b/src/index.js @@ -55,6 +55,7 @@ import isBIC from './lib/isBIC'; import isMD5 from './lib/isMD5'; import isHash from './lib/isHash'; import isJWT from './lib/isJWT'; +import isJWTExpiration from './lib/isJWTExpiration'; import isJSON from './lib/isJSON'; import isEmpty from './lib/isEmpty'; @@ -177,6 +178,7 @@ const validator = { isMD5, isHash, isJWT, + isJWTExpiration, isJSON, isEmpty, isLength, diff --git a/src/lib/isJWTExpiration.js b/src/lib/isJWTExpiration.js new file mode 100644 index 000000000..71b4c886e --- /dev/null +++ b/src/lib/isJWTExpiration.js @@ -0,0 +1,16 @@ +export default function isJWTExpiration(token) { + if (!token || typeof token !== 'string') { + return false; + } + try { + const payload = JSON.parse(atob(token.split('.')[1])); + + if (payload.exp && typeof payload.exp === 'number') { + const currentTimestamp = Math.floor(Date.now() / 1000); + return payload.exp >= currentTimestamp; + } + return false; + } catch (error) { + return false; + } +}