-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtoken.js
More file actions
56 lines (50 loc) · 1.57 KB
/
token.js
File metadata and controls
56 lines (50 loc) · 1.57 KB
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
56
const fs = require('fs');
const path = require('path');
// install jsonwebtoken if this is used
// eslint-disable-next-line import/no-extraneous-dependencies
const jsonwebtoken = require('jsonwebtoken');
const publicKey = require('../config/jwt_debug.pub');
// Include a smaple debugging key for tests
const testPrivateKey = fs.readFileSync(path.join(__dirname, '/../config/jwt_debug.pem'), 'utf8');
const testPublicKey = require('../config/jwt_debug.pub');
const AsToken = {
config: {
jwt: {
algorithm: 'RS256',
prefix: 'v1/',
public: publicKey,
private: testPrivateKey,
},
test: {
private: testPrivateKey,
public: testPublicKey,
},
},
jsonwebtoken,
sign: function sign(json, expiresIn) {
if (!json) {
throw new Error('Cannot sign empty value');
}
return this.config.jwt.prefix + jsonwebtoken.sign(json, this.config.jwt.private, {
algorithm: this.config.jwt.algorithm,
expiresIn: expiresIn === undefined ? 60 : expiresIn, // minutes
});
},
verify: function verify(tokenString) {
const token = tokenString
.replace(/bearer +/i, '')
.replace(this.config.jwt.prefix, '');
return jsonwebtoken.verify(token, this.config.test.public, {
algorithm: this.config.jwt.algorithm,
});
},
decode: function decode(tokenString) {
const token = tokenString
.replace(/bearer +/i, '')
.replace(this.config.jwt.prefix, '');
return jsonwebtoken.decode(token, this.config.test.public, {
algorithm: this.config.jwt.algorithm,
});
},
};
module.exports = AsToken;