-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcookie-auth.json
332 lines (332 loc) · 11.9 KB
/
cookie-auth.json
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
[
{
"id": "856ad51340ecb1ae",
"type": "comment",
"z": "5cd3730f771ed2a6",
"name": "Cookie-based authorization (w/ expiration)",
"info": "",
"x": 180,
"y": 40,
"wires": []
},
{
"id": "aac45dad5955d437",
"type": "function",
"z": "5cd3730f771ed2a6",
"name": "validate authorization",
"func": " let Cookie = ((msg.req.cookies || {}).authorization || '').trim()\n if (Cookie !== '') {\n let UserRegistry = global.get('UserRegistry') || Object.create(null)\n\n let [UserId,Expiration,Digest] = Cookie.split(':')\n UserId = UserId.toLowerCase()\n if (\n (UserId !== '') && (UserId in UserRegistry) && (UserRegistry[UserId] != null) &&\n /^\\d+$/.test(Expiration) && /^[0-9a-fA-F]+$/.test(Digest)\n ) {\n let TokenKey = global.get('TokenKey')\n const HMAC = crypto.createHmac('sha256',TokenKey)\n HMAC.update(UserId + ':' + Expiration)\n let expectedDigest = HMAC.digest('hex')\n\n if (\n (Digest === expectedDigest) &&\n (parseInt(Expiration,10) >= Date.now())\n ) {\n let UserRoles = UserRegistry[UserId].Roles || []\n if (\n (msg.requiredRole == null) ||\n (UserRoles.indexOf(msg.requiredRole) >= 0)\n ) {\n msg.authenticatedUser = UserId\n msg.authorizedRoles = UserRegistry[UserId].Roles || []\n\n msg.cookies = msg.cookies || {}\n msg.cookies.authorization = Cookie\n return [msg,null] // authorized\n } else {\n msg.cookies = msg.cookies || {}\n msg.cookies.authorization = null\n\n msg.payload = 'Unauthorized'\n msg.statusCode = 401\n return [null,msg] // not authorized\n }\n }\n }\n }\n\n msg.cookies = msg.cookies || {}\n msg.cookies.authorization = null\n\n msg.payload = 'Unauthorized'\n msg.statusCode = 401\n return [null,msg] // not authorized\n",
"outputs": 2,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [
{
"var": "crypto",
"module": "crypto"
}
],
"x": 160,
"y": 300,
"wires": [
[
"0cd2a54d2d7600bf"
],
[
"2b4a0d7e8ed28c87"
]
]
},
{
"id": "6da7d0cab3077886",
"type": "function",
"z": "5cd3730f771ed2a6",
"name": "validate credentials",
"func": "/**** expect credentials to be sent as part of a form or in JSON format ****/\n\n let ContentType = msg.req.headers['content-type'] || ''\n if (\n ! ContentType.startsWith('application/x-www-form-urlencoded') &&\n ! ContentType.startsWith('application/json')\n ) {\n msg.payload = 'Bad Request (wrong content type)'\n msg.statusCode = 400\n return [null,msg]\n }\n\n/**** validate credentials ****/\n\n let UserId = (msg.payload.UserId || '').toLowerCase()\n let Password = msg.payload.Password || ''\n\n let UserRegistry = global.get('UserRegistry') || Object.create(null)\n if ((UserId in UserRegistry) && (UserRegistry[UserId] != null)) {\n let UserSpecs = UserRegistry[UserId]\n if (UserSpecs.Password === Password) { // internal optimization\n return withAuthorizationOf(UserId,UserSpecs.Roles || [])\n }\n\n let PBKDF2Iterations = global.get('PBKDF2Iterations') || 100000\n crypto.pbkdf2(\n Password, Buffer.from(UserSpecs.Salt,'hex'), PBKDF2Iterations, 64, 'sha512',\n function (Error, computedHash) {\n if ((Error == null) && (computedHash.toString('hex') === UserSpecs.Hash)) {\n UserSpecs.Password = Password // speeds up future auth. requests\n return withAuthorizationOf(UserId,UserSpecs.Roles || [])\n } else {\n return withoutAuthorization()\n }\n }\n )\n } else {\n return withoutAuthorization()\n }\n\n function withAuthorizationOf (UserName, UserRoles) {\n if ((msg.requiredRole == null) || (UserRoles.indexOf(msg.requiredRole) >= 0)) {\n msg.authenticatedUser = UserId\n msg.authorizedRoles = UserRoles\n\n delete msg.payload.UserId\n delete msg.payload.Password // very important for security reasons\n\n node.send([msg,null])\n node.done()\n } else {\n return withoutAuthorization()\n }\n }\n\n function withoutAuthorization () {\n msg.cookies = msg.cookies || {}\n msg.cookies.authorization = { value:null }\n\n msg.payload = 'Unauthorized'\n msg.statusCode = 401\n\n node.send([null,msg])\n node.done()\n }\n",
"outputs": 2,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [
{
"var": "crypto",
"module": "crypto"
}
],
"x": 150,
"y": 420,
"wires": [
[
"0617719e376703c2"
],
[
"3ac991c55e5cf9cd"
]
]
},
{
"id": "c6fe87ca0c7bd0ae",
"type": "inject",
"z": "5cd3730f771ed2a6",
"name": "at Startup",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payloadType": "date",
"x": 110,
"y": 160,
"wires": [
[
"890b01c6d94b0ecd"
]
]
},
{
"id": "890b01c6d94b0ecd",
"type": "function",
"z": "5cd3730f771ed2a6",
"name": "generate Token Key",
"func": " let TokenKey = global.get('TokenKey')\n if (TokenKey == null) { // do not change TokenKey upon Node-RED deployment\n global.set('TokenKey',crypto.randomBytes(16).toString('hex'))\n }\n return msg\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [
{
"var": "crypto",
"module": "crypto"
}
],
"x": 320,
"y": 160,
"wires": [
[
"6e75bdb05f92579a"
]
]
},
{
"id": "6e75bdb05f92579a",
"type": "debug",
"z": "5cd3730f771ed2a6",
"name": "Status",
"active": true,
"tosidebar": false,
"console": false,
"tostatus": true,
"complete": "true",
"targetType": "full",
"statusVal": "'token key generated'",
"statusType": "jsonata",
"x": 530,
"y": 160,
"wires": []
},
{
"id": "0617719e376703c2",
"type": "function",
"z": "5cd3730f771ed2a6",
"name": "create token",
"func": " let Expiration = Date.now() + global.get('TokenLifetime')\n let TokenContent = msg.authenticatedUser + ':' + Expiration\n\n let TokenKey = global.get('TokenKey')\n const HMAC = crypto.createHmac('sha256',TokenKey)\n HMAC.update(TokenContent)\n let Digest = HMAC.digest('hex')\n\n msg.cookies = msg.cookies || {}\n msg.cookies.authorization = { value:TokenContent + ':' + Digest }\n return msg\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [
{
"var": "crypto",
"module": "crypto"
}
],
"x": 350,
"y": 360,
"wires": [
[
"39ed0f17d457dba9"
]
]
},
{
"id": "0cd2a54d2d7600bf",
"type": "function",
"z": "5cd3730f771ed2a6",
"name": "refresh token",
"func": " let Expiration = Date.now() + global.get('TokenLifetime')\n let TokenContent = msg.authenticatedUser + ':' + Expiration\n\n let TokenKey = global.get('TokenKey')\n const HMAC = crypto.createHmac('sha256',TokenKey)\n HMAC.update(TokenContent)\n let Digest = HMAC.digest('hex')\n\n msg.cookies = msg.cookies || {}\n msg.cookies.authorization = { value:TokenContent + ':' + Digest }\n return msg\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [
{
"var": "crypto",
"module": "crypto"
}
],
"x": 330,
"y": 240,
"wires": [
[
"c858123e71e0b1ed"
]
]
},
{
"id": "48974b513a556304",
"type": "inject",
"z": "5cd3730f771ed2a6",
"name": "at Startup",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payloadType": "date",
"x": 110,
"y": 100,
"wires": [
[
"7f67f56f5c30af44"
]
]
},
{
"id": "7f67f56f5c30af44",
"type": "change",
"z": "5cd3730f771ed2a6",
"name": "set token lifetime",
"rules": [
{
"t": "set",
"p": "TokenLifetime",
"pt": "global",
"to": "2*60*1000",
"tot": "jsonata"
},
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "TokenLifetime",
"tot": "global"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 310,
"y": 100,
"wires": [
[
"1302190ff82f3747"
]
]
},
{
"id": "1302190ff82f3747",
"type": "debug",
"z": "5cd3730f771ed2a6",
"name": "Status",
"active": true,
"tosidebar": false,
"console": false,
"tostatus": true,
"complete": "true",
"targetType": "full",
"statusVal": "payload",
"statusType": "msg",
"x": 530,
"y": 100,
"wires": []
},
{
"id": "717695f44d31b333",
"type": "reusable-in",
"z": "5cd3730f771ed2a6",
"name": "cookie auth",
"info": "describe your reusable flow here",
"scope": "global",
"x": 90,
"y": 240,
"wires": [
[
"aac45dad5955d437"
]
]
},
{
"id": "2f9bd721c97fbdde",
"type": "reusable-in",
"z": "5cd3730f771ed2a6",
"name": "cookie login",
"info": "describe your reusable flow here",
"scope": "global",
"x": 90,
"y": 360,
"wires": [
[
"6da7d0cab3077886"
]
]
},
{
"id": "c858123e71e0b1ed",
"type": "reusable-out",
"z": "5cd3730f771ed2a6",
"name": "authorized",
"position": 1,
"x": 510,
"y": 240,
"wires": []
},
{
"id": "2b4a0d7e8ed28c87",
"type": "reusable-out",
"z": "5cd3730f771ed2a6",
"name": "unauthorized",
"position": "2",
"x": 510,
"y": 280,
"wires": []
},
{
"id": "39ed0f17d457dba9",
"type": "reusable-out",
"z": "5cd3730f771ed2a6",
"name": "success",
"position": 1,
"x": 520,
"y": 360,
"wires": []
},
{
"id": "3ac991c55e5cf9cd",
"type": "reusable-out",
"z": "5cd3730f771ed2a6",
"name": "failure",
"position": "2",
"x": 530,
"y": 400,
"wires": []
}
]