-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt_verify.js
53 lines (45 loc) · 1.13 KB
/
jwt_verify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const jwt = require('jsonwebtoken');
const jwtDecode = require('jwt-decode');
const { publicKey } = require('./config').keys;
const ISSUER = 'simple-sso';
const verifyJwtToken = token =>
new Promise((resolve, reject) => {
jwt.verify(
token,
publicKey,
{ issuer: ISSUER, algorithms: ['RS256'] },
(err, decoded) => {
if (err) return reject(err);
return resolve(decoded);
}
);
});
const decodeJwtToken = token => {
if (token) {
const parsedJwt = jwtDecode(token);
return parsedJwt;
} else {
return null;
}
};
const isTokenExpired = token => {
const parsedJwt = decodeJwtToken(token);
const currentTime = new Date().getTime();
const expireAt = parsedJwt && parsedJwt.exp ? parsedJwt.exp * 1000 : null;
// console.log('parsedJwt', parsedJwt);
// console.log('currentTime', currentTime);
// console.log('expireAt', expireAt);
if (expireAt) {
if (currentTime < expireAt) {
return false;
} else {
return true;
}
} else {
return true;
}
};
module.exports = Object.assign(
{},
{ verifyJwtToken, decodeJwtToken, isTokenExpired }
);