-
Notifications
You must be signed in to change notification settings - Fork 18
/
jwt.js
176 lines (161 loc) · 6.37 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
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
module.exports = function (RED) {
var jwt = require('jsonwebtoken');
var fs = require('fs');
function JwtSign(n) {
RED.nodes.createNode(this, n);
this.name = n.name;
this.payload = n.payload;
this.alg = n.alg;
this.exp = n.exp;
this.jwkurl = n.jwkurl;
this.jwkkid = n.jwkkid;
this.secret = n.secret;
this.key = n.key;
this.signvar = n.signvar;
this.storetoken = n.storetoken;
var node = this;
if (node.jwkurl) {
GetJWK(node.jwkurl, node);
}else{
node.jwk = false;
// changed to load key on deploy level and not on runtime level why fs.readFileSync is sync.
if (node.alg === 'RS256' ||
node.alg === 'RS384' ||
node.alg === 'RS512' ||
node.alg === 'ES256' ||
node.alg === 'ES384' ||
node.alg === 'ES512') {
node.secret = process.env.NODE_RED_NODE_JWT_PRIVATE_KEY || fs.readFileSync(node.key);
} else {
node.secret = process.env.NODE_RED_NODE_JWT_SECRET || node.secret;
}
}
node.on('input', function (msg, send, done) {
send = send || function() { node.send.apply(node,arguments) }
done = done || function(err) { if(err)node.error(err, msg); }
try {
if (node.jwk) {
//use JWK to sign
var key = node.jwk.findKeyById(node.jwkkid);
if (key === undefined) {
console.log("No Key Found in JWK: " + node.jwkkid)
}
node.secret = key.key.toPrivateKeyPEM();
}
jwt.sign(msg[node.signvar],
node.secret,
{algorithm: node.alg, expiresIn: node.exp, keyid: node.jwkkid}, function (err, token) {
if (err) {
done(err);
} else {
msg[node.storetoken] = token;
send(msg);
done();
}
});
} catch (err) {
node.error(err.message);
}
});
}
RED.nodes.registerType("jwt sign", JwtSign);
function contains(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) {
return true;
}
}
return false;
}
function JwtVerify(n) {
RED.nodes.createNode(this, n);
this.name = n.name;
this.payload = n.payload;
this.alg = n.alg;
this.jwkurl = n.jwkurl;
this.secret = n.secret;
this.key = n.key;
this.signvar = n.signvar;
this.storetoken = n.storetoken;
var node = this;
if (node.jwkurl) {
GetJWK(node.jwkurl, node);
}else{
node.jwk = false;
if (contains(node.alg, 'RS256') || contains(node.alg, 'RS384') || contains(node.alg, 'RS512') || contains(node.alg, 'ES512') || contains(node.alg, 'ES384') || contains(node.alg, 'ES256')) {
node.secret = process.env.NODE_RED_NODE_JWT_PUBLIC_KEY || fs.readFileSync(node.key);
} else {
node.secret = process.env.NODE_RED_NODE_JWT_SECRET || node.secret;
}
}
node.on('input', function (msg, send, done) {
send = send || function() { node.send.apply(node,arguments) }
done = done || function(err) { if(err)node.error(err, msg); }
if (node.signvar === 'bearer') {
if (msg.req !== undefined && msg.req.get('authorization') !== undefined) {
var authz = msg.req.get('authorization').split(' ');
if(authz.length == 2 && (authz[0] === 'Bearer' || (msg.prefix !== undefined && authz[0] === msg.prefix))){
msg.bearer = authz[1];
}
} else if (msg.req.query.access_token !== undefined) {
msg.bearer = msg.req.query.access_token;
} else if (msg.req.body !== undefined && msg.req.body.access_token !== undefined) {
msg.bearer = msg.req.body.access_token;
}
}
if (node.jwk) {
//use JWK to verify
var header = GetTokenHeader(msg[node.signvar]);
//find kid if present
var kid = header.kid;
var key;
if (kid !== undefined) {
key = node.jwk.findKeyById(kid);
} else {
//...otherwise use first key in set
key = node.jwk.keys[0];
}
node.alg = header.alg;
node.secret = key.key.toPublicKeyPEM();
}
jwt.verify(msg[node.signvar], node.secret, {algorithms: node.alg}, function (err, decoded) {
if (err) {
msg['payload'] = err;
msg['statusCode'] = 401;
done(err);
} else {
msg[node.storetoken] = decoded;
send([msg, null]);
done();
}
});
});
}
RED.nodes.registerType("jwt verify", JwtVerify);
function GetJWK(url, node) {
//fetch jwk and cache it in node
var jwk;
var njwk = require('node-jwk');
var request = require("request");
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
node.jwk = njwk.JWKSet.fromObject(body);
console.log(node.jwk._keys.length + " keys loaded from JWK: " + url );
} else {
console.log("Unable to fetch JWK: " + url);
}
})
}
function GetTokenKid(token) {
//get kid from token header
var header = GetTokenHeader(token);
return header.kid;
}
function GetTokenHeader(token) {
var json = jwt.decode(token, {complete: true});
return json.header;
}
};