forked from simov/grant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwt.js
More file actions
40 lines (33 loc) · 801 Bytes
/
Copy pathjwt.js
File metadata and controls
40 lines (33 loc) · 801 Bytes
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
var fs = require('fs')
var path = require('path')
var jws = require('jws')
var key = {
public: fs.readFileSync(path.resolve(__dirname, './public.pem'), 'utf8'),
private: fs.readFileSync(path.resolve(__dirname, './private.pem'), 'utf8')
}
exports.sign = (user) => {
var epoch = Math.floor(new Date().getTime() / 1000)
var options = {
header: {
alg: 'RS256',
typ: 'JWT'
},
payload: {
// issuer
iss: 'Grant',
// expiration
exp: epoch + (3600 * 24 * 365), // 1 year
// subject
sub: JSON.stringify(user),
// audience
aud: 'Grant',
// issued at
iat: epoch
},
secret: key.private
}
return jws.sign(options)
}
exports.verify = (signature) => {
return jws.verify(signature, 'RS256', key.public)
}