-
Notifications
You must be signed in to change notification settings - Fork 3
/
jwt.js
137 lines (117 loc) · 3.02 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"use strict";
const jwt = require("jsonwebtoken");
const clipboard = require("clipboardy");
const chalk = require("chalk");
const { inspect } = require("util");
const { log, error } = console;
const { exit } = process;
const ALGORITHMS = ["HS256", "HS384", "HS512", "RS256", "RS384", "RS512", "ES256", "ES384", "ES512", "none"];
const algorithmsRegex = new RegExp(`^(${ALGORITHMS.join("|")})$`, "i");
// Helpers
function verifyOption(name, value, message = `"${name}" is required`) {
if (!value) {
error(message);
exit(1);
}
}
function parseObjectOption(name, value, message = `"${name}" must be valid JSON`) {
try {
value = JSON.parse(value);
} catch (err) {
error(message);
exit(1);
}
return value;
}
function filterOptions(options, predicate = ([, v]) => v !== undefined, reducer = (obj, [k, v]) => ((obj[k] = v), obj)) {
return Object.entries(options)
.filter(predicate)
.reduce(reducer, {});
}
// Exports
function sign(payload, secret, options = {}) {
verifyOption("payload", payload);
verifyOption("secret", secret);
let { algorithm, audience, expiresIn, header, issuer, keyid, notBefore, noTimestamp, jwtid, subject, noCopy } = options;
payload = parseObjectOption("payload", payload);
header = header && parseObjectOption("header", header);
const jwtOptions = filterOptions({
algorithm,
audience,
expiresIn,
header,
issuer,
keyid,
notBefore,
noTimestamp,
jwtid,
subject
});
try {
const token = jwt.sign(payload, secret, jwtOptions);
if (!noCopy) {
clipboard.writeSync(token);
log();
log(chalk.black.bgYellow.bold("copied to clipboard:"));
log();
}
log(chalk(token));
} catch (err) {
log();
log(chalk.red(` ${err.message} `));
log();
exit(1);
}
}
function verify(token, secret, options = {}) {
verifyOption("token", token);
verifyOption("secret", secret);
const { algorithms, audience, clockTimestamp, clockTolerance, ignoreExpiration, ignoreNotBefore, issuer, maxAge, subject } = options;
const jwtOptions = filterOptions({
algorithms,
audience,
clockTimestamp,
clockTolerance,
ignoreExpiration,
ignoreNotBefore,
issuer,
maxAge,
subject
});
try {
jwt.verify(token, secret, jwtOptions);
log();
log(chalk.green("valid!"));
} catch (err) {
log();
log(chalk.red(` ${err.message} `));
log();
exit(1);
}
}
function decode(token, options = {}) {
verifyOption("token", token);
const { complete, noCopy } = options;
const jwtOptions = filterOptions({ complete });
try {
let payload = jwt.decode(token, jwtOptions);
if (!noCopy) {
clipboard.writeSync(JSON.stringify(payload));
log();
log(chalk.black.bgYellow.bold("copied to clipboard:"));
log();
}
log(inspect(payload, { depth: null, colors: true, compact: false }));
} catch (err) {
log();
log(chalk.red(` ${err.message} `));
log();
exit(1);
}
}
module.exports = {
algorithmsRegex,
sign,
verify,
decode
};