-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (48 loc) · 1.51 KB
/
index.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
"use strict";
const fs = require("fs");
const util = require("util");
const jwkToPem = require("jwk-to-pem");
const jwt = require("jsonwebtoken");
const readFileAsync = util.promisify(fs.readFile);
const writeFileAsync = util.promisify(fs.writeFile);
const argv = require("yargs").argv;
let file = argv.file;
let token = argv.token;
let output = argv.output;
if (!file) {
console.log("No file parameter!");
console.log("Usage: node . --file=path-to-jwk-file [--token=token] [--output=path-to-output-file]");
return;
}
main(file, output, token);
function main(file, output, token) {
readFileAsync(file).then(res => {
const jwk = JSON.parse(res.toString());
let pem = jwkToPem(jwk);
if (output) {
console.log("Writing PEM file to", output + "...");
writeFileAsync(output, pem).then(() => {
console.log("Done");
}).catch(err => {
console.log("Got error", err);
});
} else {
console.log(pem);
}
if (token) {
decodeAsync(token, pem).then(payload => {
console.log(payload);
});
}
});
}
function decodeAsync(token, pem) {
return new Promise(resolve => {
jwt.verify(token, pem, (err, decoded) => {
if (err) console.log("Token could not be verified:", err.message);
if (decoded) return resolve(decoded);
let payload = jwt.decode(token);
resolve(payload);
});
});
}