forked from rethinkdb/horizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-token.js
66 lines (51 loc) · 1.84 KB
/
make-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
61
62
63
64
65
66
'use strict';
const interrupt = require('./utils/interrupt');
const config = require('./utils/config');
const horizon_server = require('@horizon/server');
const path = require('path');
const jwt = require('jsonwebtoken');
const r = horizon_server.r;
const logger = horizon_server.logger;
const argparse = require('argparse');
const parseArguments = (args) => {
const parser = new argparse.ArgumentParser({ prog: 'hz make-token' });
parser.addArgument(
[ '--token-secret' ],
{ type: 'string', metavar: 'SECRET',
help: 'Secret key for signing the token.' });
parser.addArgument(
[ 'user' ],
{ type: 'string', metavar: 'USER_ID',
help: 'The ID of the user to issue a token for.' });
return parser.parseArgs(args);
};
const processConfig = (parsed) => {
let options;
options = config.default_options();
options = config.merge_options(
options, config.read_from_config_file(parsed.project_path));
options = config.merge_options(
options, config.read_from_secrets_file(parsed.project_path));
options = config.merge_options(options, config.read_from_env());
options = config.merge_options(options, config.read_from_flags(parsed));
if (options.project_name === null) {
options.project_name = path.basename(path.resolve(options.project_path));
}
return Object.assign(options, { user: parsed.user });
};
const run = (args) => Promise.resolve().then(() => {
const options = processConfig(parseArguments(args));
if (options.token_secret === null) {
throw new Error('No token secret specified, unable to sign the token.');
}
const token = jwt.sign(
{ id: options.user, provider: null },
new Buffer(options.token_secret, 'base64'),
{ expiresIn: '1d', algorithm: 'HS512' }
);
console.log(`${token}`);
});
module.exports = {
run,
description: 'Generate a token to log in as a user',
};