Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/addedISJWTExpiriation #2315

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.acceptSuggestionOnEnter": "on"
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -177,6 +178,7 @@ const validator = {
isMD5,
isHash,
isJWT,
isJWTExpiration,
isJSON,
isEmpty,
isLength,
Expand Down
16 changes: 16 additions & 0 deletions src/lib/isJWTExpiration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default function isJWTExpiration(token) {

Check warning on line 1 in src/lib/isJWTExpiration.js

View check run for this annotation

Codecov / codecov/patch

src/lib/isJWTExpiration.js#L1

Added line #L1 was not covered by tests
if (!token || typeof token !== 'string') {
return false;

Check warning on line 3 in src/lib/isJWTExpiration.js

View check run for this annotation

Codecov / codecov/patch

src/lib/isJWTExpiration.js#L3

Added line #L3 was not covered by tests
}
try {
const payload = JSON.parse(atob(token.split('.')[1]));

Check warning on line 6 in src/lib/isJWTExpiration.js

View check run for this annotation

Codecov / codecov/patch

src/lib/isJWTExpiration.js#L5-L6

Added lines #L5 - L6 were not covered by tests

if (payload.exp && typeof payload.exp === 'number') {
const currentTimestamp = Math.floor(Date.now() / 1000);
return payload.exp >= currentTimestamp;

Check warning on line 10 in src/lib/isJWTExpiration.js

View check run for this annotation

Codecov / codecov/patch

src/lib/isJWTExpiration.js#L9-L10

Added lines #L9 - L10 were not covered by tests
}
return false;

Check warning on line 12 in src/lib/isJWTExpiration.js

View check run for this annotation

Codecov / codecov/patch

src/lib/isJWTExpiration.js#L12

Added line #L12 was not covered by tests
} catch (error) {
return false;

Check warning on line 14 in src/lib/isJWTExpiration.js

View check run for this annotation

Codecov / codecov/patch

src/lib/isJWTExpiration.js#L14

Added line #L14 was not covered by tests
}
}
Loading