lemonldap-ng-handler 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +89 -64
- package/README.md +92 -76
- package/lib/handlerDevOps.d.ts +5 -5
- package/lib/handlerDevOpsST.d.ts +5 -5
- package/lib/handlerMain.d.ts +4 -4
- package/lib/handlerServiceToken.d.ts +3 -3
- package/lib/index.d.ts +5 -5
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +122 -120
- package/lib/index.mjs.map +1 -1
- package/lib/init.d.ts +5 -5
- package/lib/tsv.d.ts +2 -2
- package/package.json +2 -1
package/lib/index.mjs
CHANGED
|
@@ -7,22 +7,22 @@ import RE2 from 're2';
|
|
|
7
7
|
import http from 'http';
|
|
8
8
|
import normalizeUrl from 'normalize-url';
|
|
9
9
|
|
|
10
|
-
const condMatch = new RE2(
|
|
10
|
+
const condMatch = new RE2("^logout(?:_sso|_app|_app_sso|)(?:s+(.*))?$", "i");
|
|
11
11
|
let sid = 0;
|
|
12
12
|
const requiredFields = [
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
13
|
+
"cda",
|
|
14
|
+
"cookieExpiration",
|
|
15
|
+
"cipher",
|
|
16
|
+
"cookieName",
|
|
17
|
+
"customFunctions",
|
|
18
|
+
"httpOnly",
|
|
19
|
+
"securedCookie",
|
|
20
|
+
"timeout",
|
|
21
|
+
"timeoutActivity",
|
|
22
|
+
"useRedirectOnError",
|
|
23
|
+
"useRedirectOnForbidden",
|
|
24
|
+
"whatToTrace",
|
|
25
|
+
"loopBackUrl",
|
|
26
26
|
];
|
|
27
27
|
class HandlerInit {
|
|
28
28
|
constructor(args) {
|
|
@@ -40,8 +40,8 @@ class HandlerInit {
|
|
|
40
40
|
locationRegexp: {},
|
|
41
41
|
maintenance: {},
|
|
42
42
|
port: {},
|
|
43
|
-
portal: () =>
|
|
44
|
-
vhostAlias: {}
|
|
43
|
+
portal: () => "",
|
|
44
|
+
vhostAlias: {},
|
|
45
45
|
};
|
|
46
46
|
this.localConf = args;
|
|
47
47
|
if (!args.configStorage)
|
|
@@ -51,19 +51,19 @@ class HandlerInit {
|
|
|
51
51
|
args.configStorage.localStorageOptions ||
|
|
52
52
|
(args.configStorage.localStorageOptions = args.localStorageOptions);
|
|
53
53
|
this.confAcc = new Conf(args.configStorage);
|
|
54
|
-
const handlerConf = this.confAcc.getLocalConf(
|
|
55
|
-
const nodeHandlerConf = (this.confAcc.getLocalConf(
|
|
56
|
-
[handlerConf, nodeHandlerConf].forEach(iniSection => {
|
|
54
|
+
const handlerConf = this.confAcc.getLocalConf("handler", true);
|
|
55
|
+
const nodeHandlerConf = (this.confAcc.getLocalConf("node-handler", false));
|
|
56
|
+
[handlerConf, nodeHandlerConf].forEach((iniSection) => {
|
|
57
57
|
Object.keys(iniSection).forEach((k) => {
|
|
58
58
|
if (!this.localConf)
|
|
59
|
-
throw new Error(
|
|
59
|
+
throw new Error("undef");
|
|
60
60
|
if (this.localConf[k] === undefined)
|
|
61
61
|
this.localConf[k] = iniSection[k];
|
|
62
62
|
});
|
|
63
63
|
});
|
|
64
64
|
if (this.localConf.nodeVhosts === undefined)
|
|
65
65
|
// istanbul ignore next
|
|
66
|
-
throw new Error(
|
|
66
|
+
throw new Error("No Virtualhosts configured for Node.js");
|
|
67
67
|
this.vhostList = this.localConf.nodeVhosts.split(/[,\s]+/);
|
|
68
68
|
}
|
|
69
69
|
reload() {
|
|
@@ -75,14 +75,14 @@ class HandlerInit {
|
|
|
75
75
|
this.confAcc
|
|
76
76
|
.getConf({})
|
|
77
77
|
.then((conf) => {
|
|
78
|
-
requiredFields.forEach(k => {
|
|
78
|
+
requiredFields.forEach((k) => {
|
|
79
79
|
// @ts-ignore: all fields not declared
|
|
80
80
|
this.tsv[k] = conf[k];
|
|
81
81
|
});
|
|
82
|
-
Object.keys(this.localConf).forEach(k => {
|
|
82
|
+
Object.keys(this.localConf).forEach((k) => {
|
|
83
83
|
conf[k] = this.localConf[k];
|
|
84
84
|
});
|
|
85
|
-
[
|
|
85
|
+
["https", "port", "maintenance"].forEach((k) => {
|
|
86
86
|
if (conf[k] !== undefined) {
|
|
87
87
|
// @ts-ignore; k is in TSV type
|
|
88
88
|
this.tsv[k] = { _: conf[k] };
|
|
@@ -99,7 +99,7 @@ class HandlerInit {
|
|
|
99
99
|
}
|
|
100
100
|
});
|
|
101
101
|
if (!conf.portal)
|
|
102
|
-
throw new Error(
|
|
102
|
+
throw new Error("portal should be defined");
|
|
103
103
|
//if ( conf.portal.match(/[\$\(&\|"']/) ) {
|
|
104
104
|
// this.tsv.portal = self.conditionSubs(conf.portal)[0];
|
|
105
105
|
//} else {
|
|
@@ -109,13 +109,13 @@ class HandlerInit {
|
|
|
109
109
|
* Sessions storage initialization
|
|
110
110
|
*/
|
|
111
111
|
//['global','oidc'].forEach
|
|
112
|
-
if (!conf[
|
|
112
|
+
if (!conf["globalStorage"] || !conf["globalStorageOptions"])
|
|
113
113
|
// istanbul ignore next
|
|
114
|
-
throw new Error(
|
|
114
|
+
throw new Error("Missing session storage configuration");
|
|
115
115
|
try {
|
|
116
116
|
this.sessionAcc = new Session({
|
|
117
|
-
storageModule: conf[
|
|
118
|
-
storageModuleOptions: conf[
|
|
117
|
+
storageModule: conf["globalStorage"],
|
|
118
|
+
storageModuleOptions: conf["globalStorageOptions"],
|
|
119
119
|
});
|
|
120
120
|
}
|
|
121
121
|
catch (e) {
|
|
@@ -130,10 +130,10 @@ class HandlerInit {
|
|
|
130
130
|
return;
|
|
131
131
|
this.tsv.locationCount[vhost] = 0;
|
|
132
132
|
[
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
].forEach(k => {
|
|
133
|
+
"locationRegexp",
|
|
134
|
+
"locationProtection",
|
|
135
|
+
"locationCondition",
|
|
136
|
+
].forEach((k) => {
|
|
137
137
|
// @ts-ignore: fields declared in TSV type
|
|
138
138
|
if (this.tsv[k][vhost] === undefined)
|
|
139
139
|
this.tsv[k][vhost] = [];
|
|
@@ -142,16 +142,16 @@ class HandlerInit {
|
|
|
142
142
|
this.safe[vhost] = new SafeLib(conf);
|
|
143
143
|
// @ts-ignore conf.locationRules[vhost] exists
|
|
144
144
|
const rules = conf.locationRules[vhost];
|
|
145
|
-
Object.keys(rules).forEach(url => {
|
|
145
|
+
Object.keys(rules).forEach((url) => {
|
|
146
146
|
const [cond, prot] = this.conditionSub(rules[url], this.safe[vhost]);
|
|
147
|
-
if (url ===
|
|
147
|
+
if (url === "default") {
|
|
148
148
|
this.tsv.defaultCondition[vhost] = cond;
|
|
149
149
|
this.tsv.defaultProtection[vhost] = prot;
|
|
150
150
|
}
|
|
151
151
|
else {
|
|
152
152
|
this.tsv.locationCondition[vhost].push(cond);
|
|
153
153
|
this.tsv.locationProtection[vhost].push(prot);
|
|
154
|
-
this.tsv.locationRegexp[vhost].push(new RE2(url.replace(/\(\?#.*?\)/,
|
|
154
|
+
this.tsv.locationRegexp[vhost].push(new RE2(url.replace(/\(\?#.*?\)/, "")));
|
|
155
155
|
this.tsv.locationCount[vhost]++;
|
|
156
156
|
}
|
|
157
157
|
});
|
|
@@ -175,12 +175,12 @@ class HandlerInit {
|
|
|
175
175
|
return;
|
|
176
176
|
if (!this.tsv.headerList[vhost])
|
|
177
177
|
this.tsv.headerList[vhost] = [];
|
|
178
|
-
let sub =
|
|
178
|
+
let sub = "";
|
|
179
179
|
Object.keys(headers).forEach((name) => {
|
|
180
180
|
this.tsv.headerList[vhost].push(name);
|
|
181
181
|
sub += `'${name}': ${this.substitute(headers[name])},`;
|
|
182
182
|
});
|
|
183
|
-
sub = sub.replace(/,$/,
|
|
183
|
+
sub = sub.replace(/,$/, "");
|
|
184
184
|
vm.runInContext(`fg = function(session) {return {${sub}};}`, this.safe[vhost]);
|
|
185
185
|
// @ts-ignore: fg is now defined
|
|
186
186
|
this.tsv.forgeHeaders[vhost] = this.safe[vhost].fg;
|
|
@@ -205,12 +205,12 @@ class HandlerInit {
|
|
|
205
205
|
this.sessionAcc.ready.then(() => {
|
|
206
206
|
resolve(true);
|
|
207
207
|
Logger(conf, true)
|
|
208
|
-
.then(logger => {
|
|
208
|
+
.then((logger) => {
|
|
209
209
|
this.userLogger = logger;
|
|
210
210
|
resolve(true);
|
|
211
211
|
})
|
|
212
|
-
.catch(e => {
|
|
213
|
-
console.error(
|
|
212
|
+
.catch((e) => {
|
|
213
|
+
console.error("Logger error", e);
|
|
214
214
|
// istanbul ignore next
|
|
215
215
|
throw new Error(e);
|
|
216
216
|
});
|
|
@@ -226,13 +226,13 @@ class HandlerInit {
|
|
|
226
226
|
conditionSub(cond, ctx) {
|
|
227
227
|
const OK = () => true;
|
|
228
228
|
const NOK = () => false;
|
|
229
|
-
if (cond ===
|
|
229
|
+
if (cond === "accept")
|
|
230
230
|
return [OK, 0];
|
|
231
|
-
if (cond ===
|
|
231
|
+
if (cond === "deny")
|
|
232
232
|
return [NOK, 0];
|
|
233
|
-
if (cond ===
|
|
233
|
+
if (cond === "unprotect")
|
|
234
234
|
return [OK, 1];
|
|
235
|
-
if (cond ===
|
|
235
|
+
if (cond === "skip")
|
|
236
236
|
return [OK, 2];
|
|
237
237
|
// TODO: manage app logout
|
|
238
238
|
const res = condMatch.exec(cond);
|
|
@@ -244,7 +244,7 @@ class HandlerInit {
|
|
|
244
244
|
session._logout = url;
|
|
245
245
|
return false;
|
|
246
246
|
},
|
|
247
|
-
0
|
|
247
|
+
0,
|
|
248
248
|
];
|
|
249
249
|
}
|
|
250
250
|
else {
|
|
@@ -253,7 +253,7 @@ class HandlerInit {
|
|
|
253
253
|
session._logout = this.tsv.portal();
|
|
254
254
|
return false;
|
|
255
255
|
},
|
|
256
|
-
0
|
|
256
|
+
0,
|
|
257
257
|
];
|
|
258
258
|
}
|
|
259
259
|
}
|
|
@@ -266,17 +266,17 @@ class HandlerInit {
|
|
|
266
266
|
}
|
|
267
267
|
else {
|
|
268
268
|
// istanbul ignore next
|
|
269
|
-
throw new Error(
|
|
269
|
+
throw new Error("Disabling safeJail is not supported");
|
|
270
270
|
}
|
|
271
271
|
}
|
|
272
272
|
substitute(expr) {
|
|
273
273
|
return (expr
|
|
274
274
|
// Special macros
|
|
275
|
-
.replace(/\$date\b/g,
|
|
276
|
-
.replace(/\$vhost\b/g,
|
|
277
|
-
.replace(/\$ip\b/g,
|
|
275
|
+
.replace(/\$date\b/g, "this.date()")
|
|
276
|
+
.replace(/\$vhost\b/g, "this.hostname(req)")
|
|
277
|
+
.replace(/\$ip\b/g, "this.remote_ip(req)")
|
|
278
278
|
// Session attributes: $xx is replaced by session.xx
|
|
279
|
-
.replace(/\$(_*[a-zA-Z]\w*)/g,
|
|
279
|
+
.replace(/\$(_*[a-zA-Z]\w*)/g, "session.$1"));
|
|
280
280
|
}
|
|
281
281
|
}
|
|
282
282
|
|
|
@@ -290,28 +290,28 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
290
290
|
run(req, res, next) {
|
|
291
291
|
const vhost = req.headers.host;
|
|
292
292
|
if (vhost === undefined) {
|
|
293
|
-
this.userLogger.error(
|
|
294
|
-
this.setError(res,
|
|
293
|
+
this.userLogger.error("Request without Host header");
|
|
294
|
+
this.setError(res, "/", 400, "Bad request");
|
|
295
295
|
return;
|
|
296
296
|
}
|
|
297
297
|
/* 1 - get data */
|
|
298
|
-
const uri = new URL(normalizeUrl(vhost + req.url)).pathname ||
|
|
298
|
+
const uri = new URL(normalizeUrl(vhost + req.url)).pathname || "/";
|
|
299
299
|
/* 2 - check for maintenance mode */
|
|
300
300
|
if (this.tsv.maintenance[vhost])
|
|
301
|
-
return this.setError(res,
|
|
301
|
+
return this.setError(res, "/", 503, "Service Temporarily Unavailable");
|
|
302
302
|
/* 3 - check if current URI is protected */
|
|
303
303
|
const protection = this.isUnprotected(req, uri);
|
|
304
304
|
if (protection === 2)
|
|
305
305
|
return next();
|
|
306
306
|
/* 4 - search for LLNG cookie */
|
|
307
307
|
const id = this.fetchId(req);
|
|
308
|
-
if (id !==
|
|
308
|
+
if (id !== "") {
|
|
309
309
|
this.retrieveSession(id)
|
|
310
|
-
.then(session => {
|
|
310
|
+
.then((session) => {
|
|
311
311
|
const user = session[this.tsv.whatToTrace];
|
|
312
312
|
this.userLogger.debug(`User ${user} identified`);
|
|
313
313
|
this.grant(req, uri, session)
|
|
314
|
-
.then(grantResult => {
|
|
314
|
+
.then((grantResult) => {
|
|
315
315
|
if (grantResult) {
|
|
316
316
|
this.sendHeaders(req, session);
|
|
317
317
|
this.hideCookie(req);
|
|
@@ -323,12 +323,13 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
323
323
|
this.userLogger.notice(`${vhost}: user ${user} was denied access to ${uri}`);
|
|
324
324
|
}
|
|
325
325
|
})
|
|
326
|
-
.catch(e => {
|
|
326
|
+
.catch((e) => {
|
|
327
327
|
console.error(e);
|
|
328
|
-
this.setError(res,
|
|
328
|
+
this.setError(res, "/", 503, "Service Unavailable");
|
|
329
329
|
});
|
|
330
330
|
})
|
|
331
|
-
|
|
331
|
+
// eslint-disable-next-line no-unused-vars
|
|
332
|
+
.catch((e) => {
|
|
332
333
|
/* Expired session */
|
|
333
334
|
this.goToPortal(res, this.selfUri(vhost, uri));
|
|
334
335
|
});
|
|
@@ -338,16 +339,16 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
338
339
|
}
|
|
339
340
|
}
|
|
340
341
|
selfUri(vhost, uri) {
|
|
341
|
-
return ((this.tsv.https && this.tsv.https[vhost] ?
|
|
342
|
-
|
|
342
|
+
return ((this.tsv.https && this.tsv.https[vhost] ? "https" : "http") +
|
|
343
|
+
"://" +
|
|
343
344
|
vhost +
|
|
344
345
|
uri);
|
|
345
346
|
}
|
|
346
347
|
nginxServer(fcgiOpt) {
|
|
347
|
-
fcgiOpt.mode || (fcgiOpt.mode =
|
|
348
|
+
fcgiOpt.mode || (fcgiOpt.mode = "fcgi");
|
|
348
349
|
fcgiOpt.port || (fcgiOpt.port = 9090);
|
|
349
|
-
fcgiOpt.ip || (fcgiOpt.ip =
|
|
350
|
-
const srv = fcgiOpt.mode ===
|
|
350
|
+
fcgiOpt.ip || (fcgiOpt.ip = "localhost");
|
|
351
|
+
const srv = fcgiOpt.mode === "fcgi" ? require("node-fastcgi") : http;
|
|
351
352
|
srv
|
|
352
353
|
.createServer((req, res) => {
|
|
353
354
|
const next = () => {
|
|
@@ -392,23 +393,24 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
392
393
|
return this.tsv.defaultProtection[vhost];
|
|
393
394
|
}
|
|
394
395
|
resolveAlias(req) {
|
|
396
|
+
var _a;
|
|
395
397
|
// @ts-ignore: req.headers.host has already been tested
|
|
396
|
-
const vhost = req.headers.host.
|
|
397
|
-
return this.tsv.vhostAlias[vhost] || vhost;
|
|
398
|
+
const vhost = (_a = req.headers.host) === null || _a === void 0 ? void 0 : _a.split(':')[0];
|
|
399
|
+
return vhost ? (this.tsv.vhostAlias[vhost] || vhost) : 'undef';
|
|
398
400
|
}
|
|
399
401
|
fetchId(req) {
|
|
400
402
|
if (req.headers.cookie) {
|
|
401
403
|
const res = this.tsv.cookieDetect.exec(req.headers.cookie);
|
|
402
|
-
if (res && res[1] !=
|
|
404
|
+
if (res && res[1] != "0")
|
|
403
405
|
return res[1];
|
|
404
406
|
}
|
|
405
|
-
return
|
|
407
|
+
return "";
|
|
406
408
|
}
|
|
407
409
|
retrieveSession(id) {
|
|
408
410
|
return new Promise((resolve, reject) => {
|
|
409
411
|
if (this.sessionAcc === undefined)
|
|
410
412
|
// istanbul ignore next
|
|
411
|
-
return reject(
|
|
413
|
+
return reject("Server not ready, please wait");
|
|
412
414
|
this.sessionAcc
|
|
413
415
|
.get(id)
|
|
414
416
|
.then((session) => {
|
|
@@ -452,7 +454,7 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
452
454
|
}
|
|
453
455
|
this.tsv.defaultCondition[vhost](req, session)
|
|
454
456
|
? resolve(true)
|
|
455
|
-
: reject(
|
|
457
|
+
: reject("Unauthorized");
|
|
456
458
|
});
|
|
457
459
|
}
|
|
458
460
|
sendHeaders(req, session) {
|
|
@@ -460,7 +462,7 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
460
462
|
try {
|
|
461
463
|
let i = 0;
|
|
462
464
|
// @ts-ignore
|
|
463
|
-
req.headers[
|
|
465
|
+
req.headers["Lm-Remote-User"] = session[this.tsv.whatToTrace];
|
|
464
466
|
const map = this.tsv.forgeHeaders[vhost](session);
|
|
465
467
|
Object.keys(map).forEach((k) => {
|
|
466
468
|
i++;
|
|
@@ -483,28 +485,28 @@ class LemonldapNGHandler extends HandlerInit {
|
|
|
483
485
|
}
|
|
484
486
|
hideCookie(req) {
|
|
485
487
|
if (req.headers.cookie)
|
|
486
|
-
req.headers.cookie = req.headers.cookie.replace(this.tsv.cookieDetect,
|
|
488
|
+
req.headers.cookie = req.headers.cookie.replace(this.tsv.cookieDetect, "");
|
|
487
489
|
}
|
|
488
490
|
forbidden(req, res, session) {
|
|
489
491
|
if (session._logout) {
|
|
490
|
-
this.goToPortal(res, session._logout,
|
|
492
|
+
this.goToPortal(res, session._logout, "logout=1");
|
|
491
493
|
}
|
|
492
494
|
else {
|
|
493
|
-
this.setError(res,
|
|
495
|
+
this.setError(res, "/", 403, "Forbidden");
|
|
494
496
|
}
|
|
495
497
|
}
|
|
496
|
-
goToPortal(res, uri, args =
|
|
497
|
-
if (typeof this.tsv.portal !==
|
|
498
|
-
this.setError(res, uri, 503,
|
|
498
|
+
goToPortal(res, uri, args = "") {
|
|
499
|
+
if (typeof this.tsv.portal !== "function") {
|
|
500
|
+
this.setError(res, uri, 503, "Waiting for configuration");
|
|
499
501
|
// istanbul ignore next
|
|
500
502
|
return;
|
|
501
503
|
}
|
|
502
504
|
const urlc = this.tsv.portal() +
|
|
503
505
|
(uri
|
|
504
506
|
? // @ts-ignore
|
|
505
|
-
|
|
506
|
-
:
|
|
507
|
-
(args ? (uri ?
|
|
507
|
+
"?url=" + new Buffer.from(encodeURI(uri)).toString("base64")
|
|
508
|
+
: "") +
|
|
509
|
+
(args ? (uri ? "&" : "?") + args : "");
|
|
508
510
|
// req.redirect is defined when running under express. If not
|
|
509
511
|
// we are running as FastCGI server
|
|
510
512
|
// @ts-ignore
|
|
@@ -537,7 +539,7 @@ class LemonldapNGHandlerDevOps extends LemonldapNGHandler {
|
|
|
537
539
|
req.cgiParams && req.cgiParams.RULES_URL
|
|
538
540
|
? // @ts-ignore: same
|
|
539
541
|
req.cgiParams.RULES_URL
|
|
540
|
-
: (this.tsv.loopBackUrl ||
|
|
542
|
+
: (this.tsv.loopBackUrl || "http://127.0.0.1") + "/rules.json";
|
|
541
543
|
if (!/^(https?):\/\/([^/:]+)(?::(\d+))?(.*)$/.test(base)) {
|
|
542
544
|
return new Promise((resolve, reject) => reject(`Bad loopBackUrl ${base}`));
|
|
543
545
|
}
|
|
@@ -545,26 +547,26 @@ class LemonldapNGHandlerDevOps extends LemonldapNGHandler {
|
|
|
545
547
|
prot: RegExp.$1,
|
|
546
548
|
host: RegExp.$2,
|
|
547
549
|
path: RegExp.$4,
|
|
548
|
-
port: RegExp.$3 || (RegExp.$1 ==
|
|
550
|
+
port: RegExp.$3 || (RegExp.$1 == "https" ? "443" : "80"),
|
|
549
551
|
// @ts-ignore: cgiParams is tested
|
|
550
|
-
lb: !(req.cgiParams && req.cgiParams.RULES_URL)
|
|
552
|
+
lb: !(req.cgiParams && req.cgiParams.RULES_URL),
|
|
551
553
|
};
|
|
552
554
|
return new Promise((resolve, reject) => {
|
|
553
555
|
this.loadVhostConfig(req, vhost, lvOpts)
|
|
554
556
|
.then(() => {
|
|
555
557
|
this.grant(req, uri, session)
|
|
556
|
-
.then(res => {
|
|
558
|
+
.then((res) => {
|
|
557
559
|
resolve(res);
|
|
558
560
|
})
|
|
559
|
-
.catch(e => reject(e));
|
|
561
|
+
.catch((e) => reject(e));
|
|
560
562
|
})
|
|
561
|
-
.catch(err => {
|
|
563
|
+
.catch((err) => {
|
|
562
564
|
console.error(err);
|
|
563
565
|
this.grant(req, uri, session)
|
|
564
|
-
.then(res => {
|
|
566
|
+
.then((res) => {
|
|
565
567
|
resolve(res);
|
|
566
568
|
})
|
|
567
|
-
.catch(e => reject(e));
|
|
569
|
+
.catch((e) => reject(e));
|
|
568
570
|
});
|
|
569
571
|
});
|
|
570
572
|
}
|
|
@@ -578,18 +580,18 @@ class LemonldapNGHandlerDevOps extends LemonldapNGHandler {
|
|
|
578
580
|
// @ts-ignore, property no more needed but required before
|
|
579
581
|
delete lvOpts.prot;
|
|
580
582
|
const req = http.request(lvOpts, (resp) => {
|
|
581
|
-
let str =
|
|
582
|
-
resp.on(
|
|
583
|
+
let str = "";
|
|
584
|
+
resp.on("data", (chunk) => {
|
|
583
585
|
str += chunk;
|
|
584
586
|
});
|
|
585
|
-
resp.on(
|
|
587
|
+
resp.on("end", () => {
|
|
586
588
|
// Blank vhost
|
|
587
589
|
this.tsv.locationCondition[vhost] = [];
|
|
588
590
|
this.tsv.locationProtection[vhost] = [];
|
|
589
591
|
this.tsv.locationRegexp[vhost] = [];
|
|
590
592
|
this.tsv.locationCount[vhost] = 0;
|
|
591
593
|
this.tsv.headerList[vhost] = [];
|
|
592
|
-
if (str ===
|
|
594
|
+
if (str === "") {
|
|
593
595
|
this.tsv.defaultCondition[vhost] = () => {
|
|
594
596
|
return true;
|
|
595
597
|
};
|
|
@@ -601,19 +603,18 @@ class LemonldapNGHandlerDevOps extends LemonldapNGHandler {
|
|
|
601
603
|
const vhostConfig = JSON.parse(str);
|
|
602
604
|
// @ts-expect-error: TODO
|
|
603
605
|
this.safe[vhost] = new SafeLib({ cipher: this.tsv.cipher });
|
|
604
|
-
if (vhostConfig.rules && typeof vhostConfig.rules ===
|
|
606
|
+
if (vhostConfig.rules && typeof vhostConfig.rules === "object") {
|
|
605
607
|
Object.keys(vhostConfig.rules).forEach((url) => {
|
|
606
608
|
const rule = new String(vhostConfig.rules[url]).valueOf();
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
if (url === 'default') {
|
|
609
|
+
const [cond, prot] = this.conditionSub(rule, this.safe[vhost]);
|
|
610
|
+
if (url === "default") {
|
|
610
611
|
this.tsv.defaultCondition[vhost] = cond;
|
|
611
612
|
this.tsv.defaultProtection[vhost] = prot;
|
|
612
613
|
}
|
|
613
614
|
else {
|
|
614
615
|
this.tsv.locationCondition[vhost].push(cond);
|
|
615
616
|
this.tsv.locationProtection[vhost].push(prot);
|
|
616
|
-
this.tsv.locationRegexp[vhost].push(new RE2(url.replace(/\(\?#.*?\)/,
|
|
617
|
+
this.tsv.locationRegexp[vhost].push(new RE2(url.replace(/\(\?#.*?\)/, "")));
|
|
617
618
|
this.tsv.locationCount[vhost]++;
|
|
618
619
|
}
|
|
619
620
|
});
|
|
@@ -623,13 +624,13 @@ class LemonldapNGHandlerDevOps extends LemonldapNGHandler {
|
|
|
623
624
|
}
|
|
624
625
|
}
|
|
625
626
|
if (vhostConfig.headers &&
|
|
626
|
-
typeof vhostConfig.headers ===
|
|
627
|
+
typeof vhostConfig.headers === "object") {
|
|
627
628
|
const sub = Object.keys(vhostConfig.headers)
|
|
628
|
-
.map(name => {
|
|
629
|
+
.map((name) => {
|
|
629
630
|
this.tsv.headerList[vhost].push(name);
|
|
630
631
|
return `'${name}': ${this.substitute(vhostConfig.headers[name])}`;
|
|
631
632
|
})
|
|
632
|
-
.join(
|
|
633
|
+
.join(",");
|
|
633
634
|
vm.runInContext(`fg = function(session) {return {${sub}}}`, this.safe[vhost]);
|
|
634
635
|
// @ts-ignore; fg is defined now
|
|
635
636
|
this.tsv.forgeHeaders[vhost] = this.safe[vhost].fg;
|
|
@@ -638,10 +639,10 @@ class LemonldapNGHandlerDevOps extends LemonldapNGHandler {
|
|
|
638
639
|
resolve(true);
|
|
639
640
|
}
|
|
640
641
|
catch (e) {
|
|
641
|
-
reject(
|
|
642
|
+
reject("Unable to getremote config: " + e);
|
|
642
643
|
}
|
|
643
644
|
});
|
|
644
|
-
req.on(
|
|
645
|
+
req.on("error", (e) => {
|
|
645
646
|
reject(e);
|
|
646
647
|
});
|
|
647
648
|
req.end();
|
|
@@ -656,35 +657,36 @@ class LemonldapNGHandlerServiceToken extends LemonldapNGHandler {
|
|
|
656
657
|
const token = req.cgiParams
|
|
657
658
|
? // @ts-ignore: node-fastcgi has no ts declarations
|
|
658
659
|
req.cgiParams.HTTP_X_LLNG_TOKEN
|
|
659
|
-
: req.headers[
|
|
660
|
+
: req.headers["x-llng-token"];
|
|
660
661
|
if (token) {
|
|
661
662
|
// Decrypt token
|
|
662
663
|
// Format: encrypt(time:_session_id:vhost1:vhost2,...)
|
|
663
664
|
let tokenElement;
|
|
664
665
|
try {
|
|
665
|
-
tokenElement = this.tsv.cipher.decrypt(token).split(
|
|
666
|
+
tokenElement = this.tsv.cipher.decrypt(token).split(":");
|
|
667
|
+
// eslint-disable-next-line no-unused-vars
|
|
666
668
|
}
|
|
667
669
|
catch (e) {
|
|
668
|
-
this.userLogger.error(
|
|
669
|
-
return
|
|
670
|
+
this.userLogger.error("Invalid token");
|
|
671
|
+
return "";
|
|
670
672
|
}
|
|
671
673
|
// At least one vhost is required
|
|
672
674
|
if (!tokenElement[2]) {
|
|
673
|
-
this.userLogger.error(
|
|
674
|
-
return
|
|
675
|
+
this.userLogger.error("Bad service token");
|
|
676
|
+
return "";
|
|
675
677
|
}
|
|
676
678
|
// Is token in good interval ? [ now - 30s, now ]
|
|
677
679
|
const now = Date.now() / 1000;
|
|
678
680
|
const tokenTime = parseInt(tokenElement[0]);
|
|
679
681
|
if (tokenTime < now - 30 || tokenTime > now) {
|
|
680
|
-
this.userLogger.warn(
|
|
681
|
-
return
|
|
682
|
+
this.userLogger.warn("Expired service token");
|
|
683
|
+
return "";
|
|
682
684
|
}
|
|
683
685
|
// Is vhost listed in token ?
|
|
684
686
|
const vhost = this.resolveAlias(req);
|
|
685
687
|
if (tokenElement.indexOf(vhost) < 2) {
|
|
686
688
|
console.warn(`${vhost} not authorizated in token`);
|
|
687
|
-
return
|
|
689
|
+
return "";
|
|
688
690
|
}
|
|
689
691
|
// Then use token sessionId
|
|
690
692
|
return tokenElement[1];
|
|
@@ -705,12 +707,12 @@ const knownHandlers = {
|
|
|
705
707
|
Main: LemonldapNGHandler,
|
|
706
708
|
DevOps: LemonldapNGHandlerDevOps,
|
|
707
709
|
ServiceToken: LemonldapNGHandlerServiceToken,
|
|
708
|
-
ServiceTokenST: LemonldapNGHandlerDevOpsST
|
|
710
|
+
ServiceTokenST: LemonldapNGHandlerDevOpsST,
|
|
709
711
|
};
|
|
710
712
|
const init = (args) => {
|
|
711
713
|
return new Promise((resolve, reject) => {
|
|
712
714
|
if (!args.type)
|
|
713
|
-
args.type =
|
|
715
|
+
args.type = "Main";
|
|
714
716
|
// @ts-ignore: args.type is defined now
|
|
715
717
|
if (knownHandlers[args.type]) {
|
|
716
718
|
// @ts-ignore
|
|
@@ -720,16 +722,16 @@ const init = (args) => {
|
|
|
720
722
|
currentHandler
|
|
721
723
|
.init()
|
|
722
724
|
.then(() => resolve(currentHandler))
|
|
723
|
-
.catch(e => reject(e));
|
|
725
|
+
.catch((e) => reject(e));
|
|
724
726
|
}
|
|
725
727
|
else {
|
|
726
|
-
import(
|
|
727
|
-
.then(mod => {
|
|
728
|
+
import("@lemonldap-ng/handler-" + args.type.toLowerCase())
|
|
729
|
+
.then((mod) => {
|
|
728
730
|
currentClass = mod.default;
|
|
729
731
|
currentHandler = new currentClass(args);
|
|
730
732
|
currentHandler.init().then(() => resolve(currentHandler));
|
|
731
733
|
})
|
|
732
|
-
.catch(e => reject(e));
|
|
734
|
+
.catch((e) => reject(e));
|
|
733
735
|
}
|
|
734
736
|
});
|
|
735
737
|
};
|