-
Notifications
You must be signed in to change notification settings - Fork 2
/
jwt.js
55 lines (49 loc) · 1.21 KB
/
jwt.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
54
55
const jsonwebtoken = require('jsonwebtoken');
// import * as jsonwebtoken from 'jsonwebtoken';
class jwtClass {
constructor() {
this.jwt = jsonwebtoken;
this.algo = 'RS256';
this.public_key = process.env.VUE_APP_RDP_SSO_PUB;
this.issuer = process.env.VUE_APP_RDP_SSO_ISS;
this.options = {
algorithm: this.algo,
issuer: this.issuer,
};
}
verifyToken(token) {
try {
const verify = this.jwt.verify(token, this.public_key, this.options);
return verify;
} catch (err) {
return false;
}
}
generatePublicToken(payload, expiresIn) {
const publicOptions = this.options;
publicOptions.algorithm = 'none';
if (typeof expiresIn !== 'undefined' && expiresIn) {
publicOptions.expiresIn = expiresIn;
}
const token = this.jwt.sign(payload, '', publicOptions);
return token;
}
verifyPublicToken(token) {
try {
const publicOptions = this.options;
publicOptions.algorithm = 'none';
publicOptions.ignoreExpiration = false;
if (!this.jwt.verify(token, '', publicOptions)) {
return false;
}
return true;
} catch (err) {
return false;
}
}
}
// module.exports = new jwtClass();
const JWT = jwtClass;
const jwt = new JWT();
// export default jwt;
module.exports = jwt;