-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
184 lines (161 loc) · 3.75 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const fs = require('fs')
const path = require('path')
const crypto = require('crypto')
const ecdsa = require('ecdsa-sig-formatter')
const SEP = '.'
const TYP = 'JWT'
const FMT = 'base64'
const debug = process.env.DEBUG ? console.error : () => {}
function readKey(key, cb){
if (key && key.charAt && path.isAbsolute(key)) {
return fs.readFile(key, cb)
}
cb(null, key)
}
function pos(jwt, index){
switch(index){
case 0: return -1
case 1: return jwt.indexOf(SEP)
case 2: return jwt.lastIndexOf(SEP)
}
}
function cut(jwt, start, end){
return jwt.substring(pos(jwt, start) + 1, pos(jwt, end))
}
function algoMap(alg) {
switch (alg.substr(0, 2)) {
case 'HS': return 'SHA' + alg.substr(2)
case 'ES':
case 'PS':
case 'RS': return 'RSA-SHA' + alg.substr(2)
}
}
function base64(str){
return Buffer.from(str).toString(FMT)
}
function urlEscape(b64) {
return b64
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '')
}
function _urlUnescape(b64) {
return b64.replace(/-/g, '+').replace(/_/g, '/') + new Array(5 - b64.length % 4).join('=')
}
function sign(segments, alg, key){
const algo = algoMap(alg)
let c
switch(alg.charAt(0)){
case 'H':
c = crypto.createHmac(algo, key)
c.update(segments[0])
c.update(SEP)
c.update(segments[1])
return urlEscape(c.digest(FMT))
case 'P':
c = crypto.createSign(algo)
c.write(segments[0])
c.write(SEP)
c.write(segments[1])
c.end()
return urlEscape(c.sign({
key,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST
}, FMT))
case 'E':
c = crypto.createSign(algo)
c.write(segments[0])
c.write(SEP)
c.write(segments[1])
c.end()
return ecdsa.derToJose(urlEscape(c.sign(key, FMT)), alg)
default:
c = crypto.createSign(algo)
c.write(segments[0])
c.write(SEP)
c.write(segments[1])
c.end()
return urlEscape(c.sign(key, FMT))
}
}
function verify(headpay, sig, alg, key){
const algo = algoMap(alg)
if (!algo) return debug('algo not supported', alg), false
let c
switch(alg.charAt(0)){
case 'H':
c = crypto.createHmac(algo, key)
c.update(headpay)
return urlEscape(c.digest(FMT)) === sig
case 'P':
c = crypto.createVerify(algo)
c.write(headpay)
c.end()
return c.verify({
key,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST
}, Buffer.from(sig, FMT))
case 'E':
sig = ecdsa.joseToDer(sig, alg)
// fall through
default:
c = crypto.createVerify(algo)
c.write(headpay)
c.end()
return c.verify(key, Buffer.from(sig, FMT))
}
}
function encode(obj){
return urlEscape(base64(JSON.stringify(obj)))
}
function decode(b64){
try {
return JSON.parse(Buffer.from(b64, FMT))
} catch (exp) {
return debug(exp)
}
}
function JWT(algo, secret, key){
this.algo = algo
this.addKeys(secret, key)
}
JWT.prototype = {
addKeys(secret, key, cb){
cb = cb || function(){}
let i = 0
readKey(secret, (err, privateKey) => {
this.privateKey = privateKey
if (2 === ++i) cb()
})
readKey(key, (err, publicKey) => {
this.publicKey = publicKey
if (2 === ++i) cb()
})
},
create(payload, header = {}){
const h = Object.assign({
typ: TYP,
alg: this.algo
}, header)
const header64 = encode(h)
const body64 = encode(payload)
const segments = [header64, body64]
segments.push(sign(segments, h.alg, this.privateKey))
return segments.join(SEP)
},
header(jwt){
return decode(cut(jwt, 0, 1))
},
payload(jwt){
return decode(cut(jwt, 1, 2))
},
verify(jwt){
const header = this.header(jwt)
if (!header) return debug('no header'), false
if (header.typ && TYP !== header.typ) return debug('wrong type', header.typ), false
return verify(cut(jwt, 0, 2), cut(jwt, 2), header.alg, this.publicKey || this.privateKey)
}
}
module.exports = JWT