-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate-token.js
executable file
·60 lines (49 loc) · 1.46 KB
/
generate-token.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
56
57
58
59
60
#!/usr/bin/env node
'use strict';
const fs = require('fs-extra'),
program = require('commander'),
{generate, verify} = require('../src/jwtToken'),
myColors = require('../helpers/myColors')
;
const file = './.jwt/tokenRegistry.json';
program
.version('0.1.0')
.option('-s, --subject <sub>', 'Set subject field on jwt token. Must be an email address')
.option('-e, --expiresin <days>', 'Set expiresIn length in days. Must be an integer[1-90]')
.action((options) => {generateToken(options.subject, options.expiresin);})
.parse(process.argv)
;
function generateToken (sub, exp) {
const token = generate({sub, exp}),
entry = _buildRegistration(token)
;
fs.readJson(file)
.then((registry) => {
registry.push(entry);
return registry;
})
.catch((err) => {
if (err.code === 'ENOENT') {
return [entry];
}
console.error(err);
process.exit(1);
})
.then((json) => {
fs.outputJson(file, json, {spaces: 2})
.then(() => {
console.info('Jwt token generated'.bold.success);
console.dir(entry);
});
});
function _buildRegistration (token) {
const decoded = verify(token);
return {
token,
creation : (new Date(decoded.iat * 1000)).toLocaleString(),
expiration: (new Date(decoded.exp * 1000)).toLocaleString(),
subject : decoded.sub,
jwtid : decoded.jti
};
}
}