-
Notifications
You must be signed in to change notification settings - Fork 10
/
cli.js
executable file
·88 lines (74 loc) · 2.06 KB
/
cli.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
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const meow = require('meow')
const ora = require('ora')
const numeral = require('numeral')
const logUpdate = require('log-update')
const isValid = require('jws').isValid
const through = require('through2')
const split = require('binary-split')
const jwtSecret = require('./')
const cli = meow(`
Usage
$ jwt-secret <token>
--file Read secrets from file path
--stdin Read secrets from stdin
Examples
$ jwt-secret --file ./passwords.txt eyJhbGciOiJIUzI1...
$ curl -sL https://git.io/vP5n1 | jwt-secret --stdin eyJhbGciOiJIUzI1...
`, {
boolean: ['stdin']
})
const opts = cli.flags
const token = cli.input[0]
let input
if (opts.stdin) {
input = process.stdin
} else if (opts.file) {
input = fs.createReadStream(path.resolve(opts.file))
}
if (!token || !input) {
console.log(cli.help)
process.exit(1)
}
if (!isValid(token)) {
const message = chalk.red('Invalid JWT.') + chalk.dim(' It should look something like:')
const exampleToken = chalk.dim('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ')
logUpdate(`\n\n ${message}\n ${exampleToken}`)
process.exit()
}
const data = {
attempts: 0,
secret: null
}
const spinner = ora()
const getAttempts = () => chalk.cyan(numeral(data.attempts).format('0,0')) + ' ' + chalk.dim('secrets checked') + '\n\n'
function exit() {
let output
if (data.secret) {
output = chalk.dim('secret found: ') + chalk.bold(data.secret)
} else {
output = chalk.dim('no secret found')
}
logUpdate(`\n\n ${output}\n\n`)
process.exit()
}
setInterval(() => {
const pre = `\n\n ${chalk.gray.dim(spinner.frame())}`
logUpdate(pre + getAttempts())
}, 50)
input
.pipe(split())
.pipe(through((chunk, enc, callback) => {
data.attempts++
callback(null, chunk)
}))
.pipe(jwtSecret(token))
.on('data', secret => {
data.secret = secret
exit()
})
.on('finish', exit)