@miqro/core 1.2.4 → 1.3.3
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/README.md +43 -27
- package/dist/handler/api-handler.d.ts +5 -0
- package/dist/handler/api-handler.js +12 -4
- package/dist/handler/api-handler.js.map +1 -0
- package/dist/handler/api-router-utils.js +10 -2
- package/dist/handler/api-router-utils.js.map +1 -0
- package/dist/handler/api-router.d.ts +1 -2
- package/dist/handler/api-router.js +1 -0
- package/dist/handler/api-router.js.map +1 -0
- package/dist/handler/app.d.ts +1 -2
- package/dist/handler/app.js +96 -13
- package/dist/handler/app.js.map +1 -0
- package/dist/handler/common/context.d.ts +57 -0
- package/dist/handler/common/context.js +123 -0
- package/dist/handler/common/context.js.map +1 -0
- package/dist/handler/common/helpers.js +1 -0
- package/dist/handler/common/helpers.js.map +1 -0
- package/dist/handler/common/index.d.ts +2 -45
- package/dist/handler/common/index.js +13 -110
- package/dist/handler/common/index.js.map +1 -0
- package/dist/handler/common/router-utils.d.ts +26 -0
- package/dist/handler/common/router-utils.js +59 -0
- package/dist/handler/common/router-utils.js.map +1 -0
- package/dist/handler/{router.d.ts → common/router.d.ts} +7 -16
- package/dist/handler/common/router.js +76 -0
- package/dist/handler/common/router.js.map +1 -0
- package/dist/handler/common/tokenize-match.d.ts +18 -9
- package/dist/handler/common/tokenize-match.js +36 -28
- package/dist/handler/common/tokenize-match.js.map +1 -0
- package/dist/handler/feature-router.d.ts +1 -2
- package/dist/handler/feature-router.js +3 -2
- package/dist/handler/feature-router.js.map +1 -0
- package/dist/handler/index.d.ts +1 -5
- package/dist/handler/index.js +2 -6
- package/dist/handler/index.js.map +1 -0
- package/dist/{middleware → handler/middleware}/buffer.d.ts +1 -1
- package/dist/{middleware → handler/middleware}/buffer.js +3 -2
- package/dist/handler/middleware/buffer.js.map +1 -0
- package/dist/handler/middleware/cookie.d.ts +4 -0
- package/dist/handler/middleware/cookie.js +18 -0
- package/dist/handler/middleware/cookie.js.map +1 -0
- package/dist/{middleware → handler/middleware}/cors.d.ts +1 -1
- package/dist/{middleware → handler/middleware}/cors.js +3 -2
- package/dist/handler/middleware/cors.js.map +1 -0
- package/dist/{middleware → handler/middleware}/form.d.ts +1 -1
- package/dist/{middleware → handler/middleware}/form.js +8 -5
- package/dist/handler/middleware/form.js.map +1 -0
- package/dist/handler/middleware/group.d.ts +3 -0
- package/dist/handler/{group.js → middleware/group.js} +3 -2
- package/dist/handler/middleware/group.js.map +1 -0
- package/dist/handler/middleware/index.d.ts +13 -0
- package/dist/handler/middleware/index.js +64 -0
- package/dist/handler/middleware/index.js.map +1 -0
- package/dist/{middleware → handler/middleware}/json.d.ts +1 -1
- package/dist/{middleware → handler/middleware}/json.js +3 -2
- package/dist/handler/middleware/json.js.map +1 -0
- package/dist/{middleware → handler/middleware}/logger.d.ts +1 -1
- package/dist/{middleware → handler/middleware}/logger.js +1 -0
- package/dist/handler/middleware/logger.js.map +1 -0
- package/dist/handler/{parse.d.ts → middleware/parse.d.ts} +4 -3
- package/dist/handler/{parse.js → middleware/parse.js} +17 -7
- package/dist/handler/middleware/parse.js.map +1 -0
- package/dist/handler/middleware/session.d.ts +21 -0
- package/dist/handler/middleware/session.js +140 -0
- package/dist/handler/middleware/session.js.map +1 -0
- package/dist/handler/middleware/tag.d.ts +4 -0
- package/dist/handler/middleware/tag.js +14 -0
- package/dist/handler/middleware/tag.js.map +1 -0
- package/dist/{middleware → handler/middleware}/text.d.ts +1 -1
- package/dist/{middleware → handler/middleware}/text.js +3 -2
- package/dist/handler/middleware/text.js.map +1 -0
- package/dist/handler/proxy.js +5 -11
- package/dist/handler/proxy.js.map +1 -0
- package/dist/handler/{html.d.ts → response/html.d.ts} +1 -1
- package/dist/handler/{html.js → response/html.js} +1 -0
- package/dist/handler/response/html.js.map +1 -0
- package/dist/handler/response/index.d.ts +3 -0
- package/dist/{middleware → handler/response}/index.js +3 -7
- package/dist/handler/response/index.js.map +1 -0
- package/dist/handler/{response.d.ts → response/json.d.ts} +1 -1
- package/dist/handler/{response.js → response/json.js} +3 -3
- package/dist/handler/response/json.js.map +1 -0
- package/dist/handler/response/result.d.ts +4 -0
- package/dist/handler/{result.js → response/result.js} +8 -8
- package/dist/handler/response/result.js.map +1 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -0
- package/dist/util/cli/index.js +1 -0
- package/dist/util/cli/index.js.map +1 -0
- package/dist/util/config/browser.js +1 -0
- package/dist/util/config/browser.js.map +1 -0
- package/dist/util/config/config.js +1 -0
- package/dist/util/config/config.js.map +1 -0
- package/dist/util/config/featuretoggle.js +1 -0
- package/dist/util/config/featuretoggle.js.map +1 -0
- package/dist/util/config/index.js +1 -0
- package/dist/util/config/index.js.map +1 -0
- package/dist/util/cookie/cookie.d.ts +51 -0
- package/dist/util/cookie/cookie.js +168 -0
- package/dist/util/cookie/cookie.js.map +1 -0
- package/dist/util/cookie/index.d.ts +16 -0
- package/dist/util/cookie/index.js +9 -0
- package/dist/util/cookie/index.js.map +1 -0
- package/dist/util/error/configfilenotfound.js +1 -0
- package/dist/util/error/configfilenotfound.js.map +1 -0
- package/dist/util/error/forbiddenerror.js +1 -0
- package/dist/util/error/forbiddenerror.js.map +1 -0
- package/dist/util/error/index.js +1 -0
- package/dist/util/error/index.js.map +1 -0
- package/dist/util/error/named.js +1 -0
- package/dist/util/error/named.js.map +1 -0
- package/dist/util/error/notfound.js +1 -0
- package/dist/util/error/notfound.js.map +1 -0
- package/dist/util/error/notimplemented.js +1 -0
- package/dist/util/error/notimplemented.js.map +1 -0
- package/dist/util/error/parsererror.js +1 -0
- package/dist/util/error/parsererror.js.map +1 -0
- package/dist/util/error/unauthorizederror.js +1 -0
- package/dist/util/error/unauthorizederror.js.map +1 -0
- package/dist/util/index.js +3 -0
- package/dist/util/index.js.map +1 -0
- package/dist/util/loader/index.js +1 -0
- package/dist/util/loader/index.js.map +1 -0
- package/dist/util/logger/common.js +1 -0
- package/dist/util/logger/common.js.map +1 -0
- package/dist/util/logger/factory.js +1 -0
- package/dist/util/logger/factory.js.map +1 -0
- package/dist/util/logger/formatter.js +1 -0
- package/dist/util/logger/formatter.js.map +1 -0
- package/dist/util/logger/index.js +1 -0
- package/dist/util/logger/index.js.map +1 -0
- package/dist/util/logger/logger.js +6 -2
- package/dist/util/logger/logger.js.map +1 -0
- package/dist/util/logger/transport.js +1 -0
- package/dist/util/logger/transport.js.map +1 -0
- package/dist/util/parser/common.d.ts +5 -5
- package/dist/util/parser/common.js +1 -0
- package/dist/util/parser/common.js.map +1 -0
- package/dist/util/parser/get.js +7 -1
- package/dist/util/parser/get.js.map +1 -0
- package/dist/util/parser/index.js +1 -0
- package/dist/util/parser/index.js.map +1 -0
- package/dist/util/parser/parser.d.ts +3 -3
- package/dist/util/parser/parser.js +20 -11
- package/dist/util/parser/parser.js.map +1 -0
- package/dist/util/parser/types/any.js +1 -0
- package/dist/util/parser/types/any.js.map +1 -0
- package/dist/util/parser/types/array.js +1 -0
- package/dist/util/parser/types/array.js.map +1 -0
- package/dist/util/parser/types/boolean.js +1 -0
- package/dist/util/parser/types/boolean.js.map +1 -0
- package/dist/util/parser/types/email.js +1 -0
- package/dist/util/parser/types/email.js.map +1 -0
- package/dist/util/parser/types/enum.js +1 -0
- package/dist/util/parser/types/enum.js.map +1 -0
- package/dist/util/parser/types/index.js +1 -0
- package/dist/util/parser/types/index.js.map +1 -0
- package/dist/util/parser/types/multiple.js +6 -2
- package/dist/util/parser/types/multiple.js.map +1 -0
- package/dist/util/parser/types/nested.js +1 -0
- package/dist/util/parser/types/nested.js.map +1 -0
- package/dist/util/parser/types/number.js +1 -0
- package/dist/util/parser/types/number.js.map +1 -0
- package/dist/util/parser/types/object.js +1 -0
- package/dist/util/parser/types/object.js.map +1 -0
- package/dist/util/parser/types/regex.js +1 -0
- package/dist/util/parser/types/regex.js.map +1 -0
- package/dist/util/parser/types/string.js +1 -0
- package/dist/util/parser/types/string.js.map +1 -0
- package/dist/util/parser/types/url.js +1 -0
- package/dist/util/parser/types/url.js.map +1 -0
- package/dist/util/request/common.d.ts +10 -0
- package/dist/util/request/common.js +100 -1
- package/dist/util/request/common.js.map +1 -0
- package/dist/util/request/index.js +143 -199
- package/dist/util/request/index.js.map +1 -0
- package/dist/util/session/common.d.ts +16 -0
- package/dist/util/session/common.js +49 -0
- package/dist/util/session/common.js.map +1 -0
- package/dist/util/session/index.d.ts +2 -16
- package/dist/util/session/index.js +13 -46
- package/dist/util/session/index.js.map +1 -0
- package/dist/util/session/service.d.ts +26 -0
- package/dist/util/session/service.js +143 -0
- package/dist/util/session/service.js.map +1 -0
- package/dist/util/test/app.js +5 -1
- package/dist/util/test/app.js.map +1 -0
- package/dist/util/test/common.js +1 -0
- package/dist/util/test/common.js.map +1 -0
- package/dist/util/test/index.js +1 -0
- package/dist/util/test/index.js.map +1 -0
- package/dist/util/uuid/index.d.ts +7 -0
- package/dist/util/uuid/index.js +8 -0
- package/dist/util/uuid/index.js.map +1 -0
- package/dist/util/uuid/regex.d.ts +2 -0
- package/dist/util/uuid/regex.js +5 -0
- package/dist/util/uuid/regex.js.map +1 -0
- package/dist/util/uuid/rng.d.ts +1 -0
- package/dist/util/uuid/rng.js +18 -0
- package/dist/util/uuid/rng.js.map +1 -0
- package/dist/util/uuid/stringify.d.ts +2 -0
- package/dist/util/uuid/stringify.js +50 -0
- package/dist/util/uuid/stringify.js.map +1 -0
- package/dist/util/uuid/v4.d.ts +5 -0
- package/dist/util/uuid/v4.js +27 -0
- package/dist/util/uuid/v4.js.map +1 -0
- package/dist/util/uuid/validate.d.ts +2 -0
- package/dist/util/uuid/validate.js +12 -0
- package/dist/util/uuid/validate.js.map +1 -0
- package/package.json +12 -21
- package/src/handler/api-handler.ts +56 -0
- package/src/handler/api-router-utils.ts +101 -0
- package/src/handler/api-router.ts +60 -0
- package/src/handler/app.ts +138 -0
- package/src/handler/common/context.ts +161 -0
- package/src/handler/common/helpers.ts +11 -0
- package/src/handler/common/index.ts +3 -0
- package/src/handler/common/router-utils.ts +67 -0
- package/src/handler/common/router.ts +70 -0
- package/src/handler/common/tokenize-match.ts +152 -0
- package/src/handler/feature-router.ts +90 -0
- package/src/handler/index.ts +8 -0
- package/src/handler/middleware/buffer.ts +79 -0
- package/src/handler/middleware/cookie.ts +19 -0
- package/src/handler/middleware/cors.ts +98 -0
- package/src/handler/middleware/form.ts +53 -0
- package/src/handler/middleware/group.ts +28 -0
- package/src/handler/middleware/index.ts +52 -0
- package/src/handler/middleware/json.ts +50 -0
- package/src/handler/middleware/logger.ts +18 -0
- package/src/handler/middleware/parse.ts +93 -0
- package/src/handler/middleware/session.ts +155 -0
- package/src/handler/middleware/tag.ts +15 -0
- package/src/handler/middleware/text.ts +40 -0
- package/src/handler/proxy.ts +44 -0
- package/src/handler/response/html.ts +36 -0
- package/src/handler/response/index.ts +3 -0
- package/src/handler/response/json.ts +19 -0
- package/src/handler/response/result.ts +40 -0
- package/src/index.ts +2 -0
- package/src/util/cli/index.ts +57 -0
- package/src/util/config/browser.ts +5 -0
- package/src/util/config/config.ts +93 -0
- package/src/util/config/featuretoggle.ts +6 -0
- package/src/util/config/index.ts +3 -0
- package/src/util/cookie/cookie.ts +207 -0
- package/src/util/cookie/index.ts +6 -0
- package/src/util/error/configfilenotfound.ts +8 -0
- package/src/util/error/forbiddenerror.ts +8 -0
- package/src/util/error/index.ts +6 -0
- package/src/util/error/named.ts +3 -0
- package/src/util/error/notfound.ts +8 -0
- package/src/util/error/notimplemented.ts +8 -0
- package/src/util/error/parsererror.ts +10 -0
- package/src/util/error/unauthorizederror.ts +8 -0
- package/src/util/index.ts +11 -0
- package/src/util/loader/index.ts +67 -0
- package/src/util/logger/common.ts +6 -0
- package/src/util/logger/factory.ts +45 -0
- package/src/util/logger/formatter.ts +9 -0
- package/src/util/logger/index.ts +6 -0
- package/src/util/logger/logger.ts +114 -0
- package/src/util/logger/transport.ts +69 -0
- package/src/util/parser/common.ts +81 -0
- package/src/util/parser/get.ts +42 -0
- package/src/util/parser/index.ts +6 -0
- package/src/util/parser/parser.ts +235 -0
- package/src/util/parser/types/any.ts +8 -0
- package/src/util/parser/types/array.ts +73 -0
- package/src/util/parser/types/boolean.ts +13 -0
- package/src/util/parser/types/email.ts +26 -0
- package/src/util/parser/types/enum.ts +29 -0
- package/src/util/parser/types/index.ts +12 -0
- package/src/util/parser/types/multiple.ts +36 -0
- package/src/util/parser/types/nested.ts +19 -0
- package/src/util/parser/types/number.ts +34 -0
- package/src/util/parser/types/object.ts +10 -0
- package/src/util/parser/types/regex.ts +17 -0
- package/src/util/parser/types/string.ts +23 -0
- package/src/util/parser/types/url.ts +35 -0
- package/src/util/request/common.ts +193 -0
- package/src/util/request/index.ts +230 -0
- package/src/util/session/common.ts +63 -0
- package/src/util/session/index.ts +2 -0
- package/src/util/session/service.ts +154 -0
- package/src/util/test/app.ts +44 -0
- package/src/util/test/common.ts +17 -0
- package/src/util/test/index.ts +2 -0
- package/src/util/uuid/index.ts +5 -0
- package/src/util/uuid/regex.ts +2 -0
- package/src/util/uuid/rng.ts +14 -0
- package/src/util/uuid/stringify.ts +53 -0
- package/src/util/uuid/v4.ts +30 -0
- package/src/util/uuid/validate.ts +8 -0
- package/tsconfig.json +32 -0
- package/dist/handler/common/response.d.ts +0 -13
- package/dist/handler/common/response.js +0 -54
- package/dist/handler/error.d.ts +0 -2
- package/dist/handler/error.js +0 -46
- package/dist/handler/group.d.ts +0 -3
- package/dist/handler/result.d.ts +0 -4
- package/dist/handler/router.js +0 -119
- package/dist/middleware/index.d.ts +0 -6
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { newURL } from "../../../handler/common/helpers";
|
|
2
|
+
import { ParseValueArgs, ParseValueValidator } from "../common";
|
|
3
|
+
|
|
4
|
+
export const parseURL: ParseValueValidator = ({
|
|
5
|
+
value,
|
|
6
|
+
stringMinLength,
|
|
7
|
+
stringMaxLength
|
|
8
|
+
}: ParseValueArgs) => {
|
|
9
|
+
let isType = typeof value === "string";
|
|
10
|
+
const parsedValue = value;
|
|
11
|
+
if (isType && stringMinLength !== undefined && parsedValue.length < stringMinLength) {
|
|
12
|
+
isType = false;
|
|
13
|
+
}
|
|
14
|
+
if (isType && stringMaxLength !== undefined && parsedValue.length > stringMaxLength) {
|
|
15
|
+
isType = false;
|
|
16
|
+
}
|
|
17
|
+
if (isType) {
|
|
18
|
+
try {
|
|
19
|
+
const url = newURL(value);
|
|
20
|
+
return {
|
|
21
|
+
isType,
|
|
22
|
+
parsedValue: url
|
|
23
|
+
}
|
|
24
|
+
} catch (e) {
|
|
25
|
+
return {
|
|
26
|
+
isType: false,
|
|
27
|
+
parsedValue: value
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
isType: false,
|
|
33
|
+
parsedValue: value
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import http from "http";
|
|
2
|
+
import { NamedError } from "../error/named";
|
|
3
|
+
import { mergeSearchParams } from "../../handler/common/tokenize-match";
|
|
4
|
+
import { SimpleMap } from "../parser/common";
|
|
5
|
+
import { URL } from "url";
|
|
6
|
+
import { newURL } from "../../handler/common/helpers";
|
|
7
|
+
import { Logger } from "../logger/logger";
|
|
8
|
+
|
|
9
|
+
export const readResponseBuffer = ({
|
|
10
|
+
res,
|
|
11
|
+
readTimeout,
|
|
12
|
+
options,
|
|
13
|
+
req,
|
|
14
|
+
reject,
|
|
15
|
+
logger
|
|
16
|
+
}: {
|
|
17
|
+
res: http.IncomingMessage;
|
|
18
|
+
readTimeout: NodeJS.Timeout | null,
|
|
19
|
+
options: RequestOptions,
|
|
20
|
+
req: http.ClientRequest,
|
|
21
|
+
reject: (error: Error) => void;
|
|
22
|
+
logger?: Logger;
|
|
23
|
+
}, cb: (buffers: Buffer[]) => void): void => {
|
|
24
|
+
try {
|
|
25
|
+
const buffers: Buffer[] = [];
|
|
26
|
+
|
|
27
|
+
if (readTimeout) {
|
|
28
|
+
clearTimeout(readTimeout);
|
|
29
|
+
}
|
|
30
|
+
const responseTimeout = options.timeout ? setTimeout(() => {
|
|
31
|
+
res.removeListener("data", chunkListener);
|
|
32
|
+
res.removeListener("error", errorListener)
|
|
33
|
+
res.removeListener("end", endListener);
|
|
34
|
+
req.end(() => {
|
|
35
|
+
try {
|
|
36
|
+
req.destroy();
|
|
37
|
+
res.destroy();
|
|
38
|
+
} catch (e) {
|
|
39
|
+
if (logger) {
|
|
40
|
+
logger.error(e);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
reject(new Error(`response timeout ${options.timeout}`));
|
|
44
|
+
return;
|
|
45
|
+
});
|
|
46
|
+
}, options.timeout) : null;
|
|
47
|
+
|
|
48
|
+
let responseLength = 0;
|
|
49
|
+
const chunkListener = (chunk: Buffer) => {
|
|
50
|
+
responseLength += chunk.length;
|
|
51
|
+
if (options.maxResponse && options.maxResponse < responseLength) {
|
|
52
|
+
res.removeListener("data", chunkListener);
|
|
53
|
+
res.removeListener("error", errorListener)
|
|
54
|
+
res.removeListener("end", endListener);
|
|
55
|
+
req.end(() => {
|
|
56
|
+
try {
|
|
57
|
+
req.destroy();
|
|
58
|
+
res.destroy();
|
|
59
|
+
} catch (e) {
|
|
60
|
+
if (logger) {
|
|
61
|
+
logger.error(e);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
reject(new Error(`response too big maxResponse ${options.maxResponse} < ${responseLength}`));
|
|
65
|
+
return;
|
|
66
|
+
});
|
|
67
|
+
} else {
|
|
68
|
+
buffers.push(chunk);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const errorListener = (e2: Error) => {
|
|
72
|
+
res.removeListener("data", chunkListener);
|
|
73
|
+
res.removeListener("end", endListener);
|
|
74
|
+
res.removeListener("error", errorListener);
|
|
75
|
+
try {
|
|
76
|
+
req.destroy();
|
|
77
|
+
res.destroy();
|
|
78
|
+
} catch (e) {
|
|
79
|
+
if (logger) {
|
|
80
|
+
logger.error(e);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
reject(e2);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const endListener = () => {
|
|
87
|
+
try {
|
|
88
|
+
if (responseTimeout) {
|
|
89
|
+
clearTimeout(responseTimeout);
|
|
90
|
+
}
|
|
91
|
+
res.removeListener("data", chunkListener);
|
|
92
|
+
res.removeListener("error", errorListener);
|
|
93
|
+
cb(buffers);
|
|
94
|
+
} catch (e: any) {
|
|
95
|
+
reject(e);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
// setup listeners
|
|
101
|
+
res.on("data", chunkListener);
|
|
102
|
+
res.once("error", errorListener)
|
|
103
|
+
res.once("end", endListener);
|
|
104
|
+
} catch (e3: any) {
|
|
105
|
+
// remove listeners
|
|
106
|
+
res.removeListener("data", chunkListener);
|
|
107
|
+
res.removeListener("end", endListener);
|
|
108
|
+
res.removeListener("error", errorListener);
|
|
109
|
+
reject(e3);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
} catch (e: any) {
|
|
113
|
+
reject(e);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const parseRedirectLocation = (url: string, extraQuery?: SimpleMap<string | string[] | number | boolean | number[] | boolean[]>, socketPath?: string): {
|
|
119
|
+
protocol?: string;
|
|
120
|
+
queryStr: string;
|
|
121
|
+
pathname: string;
|
|
122
|
+
hash: string;
|
|
123
|
+
hostname?: string;
|
|
124
|
+
socketPath?: string;
|
|
125
|
+
port?: string;
|
|
126
|
+
url: string;
|
|
127
|
+
} => {
|
|
128
|
+
const ret = (urlO: URL, url: string, ignoreHostPort?: boolean) => {
|
|
129
|
+
return {
|
|
130
|
+
queryStr: mergeSearchParams(urlO.searchParams, extraQuery).toString(),
|
|
131
|
+
protocol: ignoreHostPort ? "http:" : urlO.protocol,
|
|
132
|
+
hash: urlO.hash,
|
|
133
|
+
pathname: urlO.pathname,
|
|
134
|
+
socketPath: ignoreHostPort ? socketPath : undefined,
|
|
135
|
+
hostname: ignoreHostPort ? undefined : urlO.hostname,
|
|
136
|
+
port: ignoreHostPort ? undefined : urlO.port,
|
|
137
|
+
url: ignoreHostPort ? url : urlO.toString()
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
return ret(newURL(url), url);
|
|
142
|
+
} catch (eU) {
|
|
143
|
+
if (url && url.length > 0 && url[0] === "/") {
|
|
144
|
+
return ret(newURL(`http://localhost${url}`), url, true);
|
|
145
|
+
} else {
|
|
146
|
+
throw eU;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export class ResponseError extends NamedError implements RequestResponse {
|
|
152
|
+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
153
|
+
constructor(
|
|
154
|
+
public readonly status: number,
|
|
155
|
+
public readonly headers: http.IncomingHttpHeaders,
|
|
156
|
+
public readonly url: string,
|
|
157
|
+
public readonly redirectedUrl: string | null,
|
|
158
|
+
public readonly data: any,
|
|
159
|
+
public readonly buffer: Buffer,
|
|
160
|
+
public readonly locations: string[]
|
|
161
|
+
) {
|
|
162
|
+
super(`request ended with ${status ? `status [${status}]` : "no status"}`);
|
|
163
|
+
this.name = "ResponseError";
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export interface RequestOptions {
|
|
168
|
+
url: string;
|
|
169
|
+
method?: string;
|
|
170
|
+
query?: SimpleMap<string | string[] | number | boolean | number[] | boolean[]>;
|
|
171
|
+
socketPath?: string;
|
|
172
|
+
followRedirect?: boolean;
|
|
173
|
+
disableUserAgent?: true;
|
|
174
|
+
maxRedirects?: number;
|
|
175
|
+
maxResponse?: number;
|
|
176
|
+
locations?: string[];
|
|
177
|
+
timeout?: number;
|
|
178
|
+
disableThrow?: boolean;
|
|
179
|
+
headers?: http.OutgoingHttpHeaders;
|
|
180
|
+
data?: any;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export interface RequestResponse {
|
|
184
|
+
url: string;
|
|
185
|
+
redirectedUrl: string | null;
|
|
186
|
+
locations: string[];
|
|
187
|
+
headers: http.IncomingHttpHeaders,
|
|
188
|
+
status: number;
|
|
189
|
+
data: any;
|
|
190
|
+
buffer: Buffer;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { Logger } from "../logger";
|
|
2
|
+
import http from "http";
|
|
3
|
+
import https from "https";
|
|
4
|
+
import { parseRedirectLocation, RequestOptions, RequestResponse, ResponseError, readResponseBuffer } from "./common";
|
|
5
|
+
import { gunzipSync } from "zlib";
|
|
6
|
+
import { isBrowser } from "../config/browser";
|
|
7
|
+
|
|
8
|
+
export { RequestOptions, RequestResponse, ResponseError } from "./common";
|
|
9
|
+
|
|
10
|
+
const DEFAULT_USER_AGENT = "curl/7.69.1";
|
|
11
|
+
const CONTENT_TYPE_HEADER = "Content-Type";
|
|
12
|
+
const JSON_TYPE = "application/json;charset=utf-8";
|
|
13
|
+
const TEXT_TYPE = "plain/text;charset=utf-8";
|
|
14
|
+
|
|
15
|
+
export const request = (options: RequestOptions, logger?: Logger): Promise<RequestResponse> => {
|
|
16
|
+
if ((options.method && options.method.toLowerCase() === "get" || !options.method) && options.data !== undefined) {
|
|
17
|
+
return Promise.reject(new Error("cannot send data on method get"));
|
|
18
|
+
} else {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
try {
|
|
21
|
+
if (!options.headers) {
|
|
22
|
+
options.headers = {};
|
|
23
|
+
}
|
|
24
|
+
if (typeof options.maxRedirects === "undefined") {
|
|
25
|
+
options.maxRedirects = 10;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const contentType = options.headers[CONTENT_TYPE_HEADER] || options.headers[CONTENT_TYPE_HEADER.toLowerCase()] || undefined || options.headers[CONTENT_TYPE_HEADER.toUpperCase()];
|
|
29
|
+
const isJSONType = contentType ? contentType.toString().toLocaleLowerCase().indexOf("application/json") === 0 : undefined;
|
|
30
|
+
|
|
31
|
+
const noType = !contentType;
|
|
32
|
+
|
|
33
|
+
const isText: boolean = typeof options.data === "string";
|
|
34
|
+
const JsonStringify: boolean = (!isText && (noType || isJSONType as boolean));
|
|
35
|
+
|
|
36
|
+
if (JsonStringify && noType) {
|
|
37
|
+
options.headers["Content-Type"] = JSON_TYPE;
|
|
38
|
+
} else if (isText && noType) {
|
|
39
|
+
options.headers["Content-Type"] = TEXT_TYPE;
|
|
40
|
+
}
|
|
41
|
+
const data = options.data ? JsonStringify ? JSON.stringify(options.data) : options.data : undefined;
|
|
42
|
+
const contentLength = data ? Buffer.from(data).length : 0;
|
|
43
|
+
|
|
44
|
+
// { socketPath, protocol, queryStr, hash, pathname, hostname, port }
|
|
45
|
+
const parsed = parseRedirectLocation(options.url, options.query, options.socketPath);
|
|
46
|
+
|
|
47
|
+
if (!parsed.socketPath && !parsed.hostname) {
|
|
48
|
+
throw new Error(`Bad url ${options.url}`);
|
|
49
|
+
}
|
|
50
|
+
switch (parsed.protocol) {
|
|
51
|
+
case "https:":
|
|
52
|
+
case "http:":
|
|
53
|
+
const readTimeout = options.timeout ? setTimeout(() => {
|
|
54
|
+
try {
|
|
55
|
+
req.end(() => {
|
|
56
|
+
try {
|
|
57
|
+
req.destroy();
|
|
58
|
+
} catch (e) {
|
|
59
|
+
if (logger) {
|
|
60
|
+
logger.error(e);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
reject(new Error(`response timeout ${options.timeout}`));
|
|
64
|
+
return;
|
|
65
|
+
});
|
|
66
|
+
} catch (e) {
|
|
67
|
+
reject(e);
|
|
68
|
+
}
|
|
69
|
+
}, options.timeout) : null;
|
|
70
|
+
const req: http.ClientRequest = (parsed.protocol === "https:" ? https.request : http.request)({
|
|
71
|
+
agent: false,
|
|
72
|
+
path: `${parsed.pathname}${parsed.queryStr ? `?${parsed.queryStr}` : ""}${parsed.hash ? parsed.hash : ""}`,
|
|
73
|
+
method: options.method,
|
|
74
|
+
socketPath: options.socketPath,
|
|
75
|
+
headers: options.disableUserAgent ? {
|
|
76
|
+
["Content-Length"]: contentLength,
|
|
77
|
+
...options.headers
|
|
78
|
+
} : {
|
|
79
|
+
["User-Agent"]: DEFAULT_USER_AGENT,
|
|
80
|
+
["Content-Length"]: contentLength,
|
|
81
|
+
...options.headers
|
|
82
|
+
},
|
|
83
|
+
timeout: options.timeout,
|
|
84
|
+
hostname: parsed.hostname,
|
|
85
|
+
port: parsed.port
|
|
86
|
+
}, (res: http.IncomingMessage) => readResponseBuffer({
|
|
87
|
+
options,
|
|
88
|
+
req,
|
|
89
|
+
readTimeout,
|
|
90
|
+
logger,
|
|
91
|
+
reject,
|
|
92
|
+
res
|
|
93
|
+
}, (buffers) => {
|
|
94
|
+
try {
|
|
95
|
+
const responseBuffer: Buffer = res.headers["content-encoding"] === "gzip" && !isBrowser() ?
|
|
96
|
+
gunzipSync(Buffer.concat(buffers)) : Buffer.concat(buffers);
|
|
97
|
+
let data: any = responseBuffer.toString();
|
|
98
|
+
|
|
99
|
+
const contentType = res.headers["content-type"];
|
|
100
|
+
if (contentType && data && (contentType.indexOf("json") !== -1)) {
|
|
101
|
+
data = JSON.parse(responseBuffer.toString());
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const status = res.statusCode;
|
|
105
|
+
if (!status) {
|
|
106
|
+
const err = new ResponseError(status as any, res.headers, options.url, null, data, responseBuffer, options.locations ? options.locations : []);
|
|
107
|
+
reject(err);
|
|
108
|
+
return;
|
|
109
|
+
} else if (status >= 300 && status < 400 && options.followRedirect) {
|
|
110
|
+
// follow redirect
|
|
111
|
+
const location = res.headers["location"];
|
|
112
|
+
try {
|
|
113
|
+
if (!location) {
|
|
114
|
+
reject(new Error(`[${location}] not valid from ${options.url}`));
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
// parse location and add query data from current location
|
|
118
|
+
const locationData = parseRedirectLocation(location, options.query, options.socketPath);
|
|
119
|
+
|
|
120
|
+
// if no host is set in location header set the current header and protocol
|
|
121
|
+
// protocol is infered from current location
|
|
122
|
+
if (!locationData.hostname && parsed.hostname && !locationData.socketPath) {
|
|
123
|
+
locationData.url = `${parsed.protocol}//${parsed.hostname}${parsed.port ? `:${parsed.port}` : ""}${locationData.url}`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (logger) {
|
|
127
|
+
logger.debug(`redirecting to [${locationData.url}] from [${options.url}][${status}]`);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// avoid loop redirect
|
|
131
|
+
if (options.url === locationData.url || (options.locations && options.locations.indexOf(locationData.url) !== -1)) {
|
|
132
|
+
reject(new Error(`loop redirect to [${location}] from [${options.url}][${status}] locations ${options.locations ? options.locations.join(",") : "[]"}`));
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// append new location
|
|
137
|
+
options.locations = options.locations ? options.locations.concat([locationData.url]) : [options.url, locationData.url];
|
|
138
|
+
|
|
139
|
+
// check options.maxRedirects
|
|
140
|
+
if (options.maxRedirects !== undefined && (options.locations.length - 1) > options.maxRedirects) {
|
|
141
|
+
reject(new Error(`too many redirects to [${location}] from [${options.url}][${status}]`));
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// request new location pass current options
|
|
146
|
+
request({
|
|
147
|
+
...options,
|
|
148
|
+
socketPath: locationData.socketPath,
|
|
149
|
+
url: locationData.url,
|
|
150
|
+
locations: options.locations
|
|
151
|
+
}, logger).then((ret) => {
|
|
152
|
+
resolve({
|
|
153
|
+
...ret,
|
|
154
|
+
url: options.url,
|
|
155
|
+
redirectedUrl: ret.locations && ret.locations.length > 0 ? ret.locations[ret.locations.length - 1] : ret.url
|
|
156
|
+
});
|
|
157
|
+
return;
|
|
158
|
+
}).catch((e4: any) => {
|
|
159
|
+
// bad redirect
|
|
160
|
+
if (e4.url && e4.status && e4.headers && e4.data) {
|
|
161
|
+
const err = new ResponseError(e4.status, e4.headers, e4.url, e4.redirectedUrl, e4.data, e4.buffer, e4.locations ? e4.locations : (options.locations ? options.locations : []));
|
|
162
|
+
err.stack = e4.stack;
|
|
163
|
+
reject(err);
|
|
164
|
+
return;
|
|
165
|
+
} else {
|
|
166
|
+
(e4 as any).redirectedUrl = locationData.url;
|
|
167
|
+
(e4 as any).locations = options.locations;
|
|
168
|
+
(e4 as any).url = options.url;
|
|
169
|
+
reject(e4);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
return;
|
|
174
|
+
} catch (e5) {
|
|
175
|
+
reject(new ResponseError(status, res.headers, options.url, location ? location : null, data, responseBuffer, options.locations ? options.locations : []));
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
} else if (status >= 200 && status < 300) {
|
|
179
|
+
// always resolve with 2xx
|
|
180
|
+
resolve({
|
|
181
|
+
url: options.url,
|
|
182
|
+
status,
|
|
183
|
+
locations: options.locations ? options.locations : [],
|
|
184
|
+
redirectedUrl: null,
|
|
185
|
+
headers: res.headers,
|
|
186
|
+
data,
|
|
187
|
+
buffer: responseBuffer
|
|
188
|
+
});
|
|
189
|
+
return;
|
|
190
|
+
} else {
|
|
191
|
+
if (options.disableThrow && status !== undefined) {
|
|
192
|
+
// disable throw and resolve with 4xx 5xx etc
|
|
193
|
+
resolve({
|
|
194
|
+
url: options.url,
|
|
195
|
+
status,
|
|
196
|
+
locations: options.locations ? options.locations : [],
|
|
197
|
+
redirectedUrl: null,
|
|
198
|
+
headers: res.headers,
|
|
199
|
+
data,
|
|
200
|
+
buffer: responseBuffer
|
|
201
|
+
});
|
|
202
|
+
} else {
|
|
203
|
+
// throw with 4xx 5xx etc
|
|
204
|
+
const err = new ResponseError(status, res.headers, options.url, null, data, responseBuffer, options.locations ? options.locations : []);
|
|
205
|
+
reject(err);
|
|
206
|
+
}
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
} catch (e: any) {
|
|
210
|
+
reject(e);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
}))
|
|
214
|
+
req.once("error", (e: Error) => {
|
|
215
|
+
if ((e as any).code === "ECONNREFUSED") {
|
|
216
|
+
e.name = "ResponseConnectionRefusedError";
|
|
217
|
+
}
|
|
218
|
+
reject(e);
|
|
219
|
+
});
|
|
220
|
+
req.end(data);
|
|
221
|
+
break;
|
|
222
|
+
default:
|
|
223
|
+
reject(new Error(`unknown protocol [${parsed.protocol}]`))
|
|
224
|
+
}
|
|
225
|
+
} catch (e) {
|
|
226
|
+
reject(e);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { ParseOptionsError } from "../error";
|
|
2
|
+
import { SimpleMap, SimpleTypes } from "../parser/common";
|
|
3
|
+
|
|
4
|
+
export interface NoTokenSession extends SimpleMap<SimpleTypes> {
|
|
5
|
+
account: string;
|
|
6
|
+
username: string;
|
|
7
|
+
groups: string[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface Session extends NoTokenSession {
|
|
11
|
+
token: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type GroupPolicyType = "at_least_one" | "all";
|
|
15
|
+
|
|
16
|
+
export type GroupPolicyGroups = string | string[];
|
|
17
|
+
|
|
18
|
+
export interface GroupPolicy {
|
|
19
|
+
groups: GroupPolicyGroups[];
|
|
20
|
+
groupPolicy: GroupPolicyType;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const policyCheck = (session: Session, options: GroupPolicy): boolean => {
|
|
24
|
+
switch (options.groupPolicy) {
|
|
25
|
+
case "at_least_one":
|
|
26
|
+
for (const group of options.groups) {
|
|
27
|
+
if (group instanceof Array) {
|
|
28
|
+
let ret = true;
|
|
29
|
+
for (const g of group) {
|
|
30
|
+
if (session.groups.indexOf(g) === -1) {
|
|
31
|
+
ret = false;
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (ret) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
} else {
|
|
39
|
+
if (session.groups.indexOf(group) !== -1) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
case "all":
|
|
46
|
+
for (const group of options.groups) {
|
|
47
|
+
if (group instanceof Array) {
|
|
48
|
+
for (const g of group) {
|
|
49
|
+
if (session.groups.indexOf(g) === -1) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
if (session.groups.indexOf(group) === -1) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return true;
|
|
60
|
+
default:
|
|
61
|
+
throw new ParseOptionsError(`policy [${options.groupPolicy}] not implemented!!`);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { Session, NoTokenSession } from "./common";
|
|
2
|
+
import { Context } from "../../handler/common";
|
|
3
|
+
import { checkEnvVariables } from "../config";
|
|
4
|
+
import { UnAuthorizedError } from "../error";
|
|
5
|
+
import { request, RequestOptions, RequestResponse } from "../request";
|
|
6
|
+
import { parse } from "../parser";
|
|
7
|
+
import { COOKIE } from "../cookie";
|
|
8
|
+
|
|
9
|
+
const DEFAULT_TOKEN_LOCATION = "header";
|
|
10
|
+
const DEFAULT_TOKEN_HEADER = "Authorization";
|
|
11
|
+
const DEFAULT_TOKEN_QUERY = "token";
|
|
12
|
+
const DEFAULT_TOKEN_COOKIE = "Authorization";
|
|
13
|
+
|
|
14
|
+
export interface VerifyTokenService {
|
|
15
|
+
verify(args: { token: string; ctx: Context }): Promise<Session | null>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface VerifyEndpointServiceOptions {
|
|
19
|
+
url: string;
|
|
20
|
+
method: string;
|
|
21
|
+
tokenLocation: "header" | "query" | "cookie";
|
|
22
|
+
tokenLocationName: string | ((ctx: Context) => Promise<string>);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class VerifyEndpointService implements VerifyTokenService {
|
|
26
|
+
|
|
27
|
+
protected options: VerifyEndpointServiceOptions;
|
|
28
|
+
|
|
29
|
+
constructor(options?: VerifyEndpointServiceOptions) {
|
|
30
|
+
if (!options) {
|
|
31
|
+
const [url, method] = checkEnvVariables(["TOKEN_VERIFY_ENDPOINT", "TOKEN_VERIFY_ENDPOINT_METHOD"]);
|
|
32
|
+
const [tokenVerifyLocation] = checkEnvVariables(["TOKEN_VERIFY_LOCATION"], [DEFAULT_TOKEN_LOCATION]);
|
|
33
|
+
let tokenLocationName;
|
|
34
|
+
switch (tokenVerifyLocation) {
|
|
35
|
+
case "header":
|
|
36
|
+
tokenLocationName = checkEnvVariables(["TOKEN_HEADER"], [DEFAULT_TOKEN_HEADER])[0];
|
|
37
|
+
break;
|
|
38
|
+
case "query":
|
|
39
|
+
tokenLocationName = checkEnvVariables(["TOKEN_QUERY"], [DEFAULT_TOKEN_QUERY])[0];
|
|
40
|
+
break;
|
|
41
|
+
case "cookie":
|
|
42
|
+
tokenLocationName = checkEnvVariables(["TOKEN_COOKIE"], [DEFAULT_TOKEN_COOKIE])[0];
|
|
43
|
+
break;
|
|
44
|
+
default:
|
|
45
|
+
throw new Error(`TOKEN_VERIFY_LOCATION=${tokenVerifyLocation} not supported use (header or query)`);
|
|
46
|
+
}
|
|
47
|
+
this.options = {
|
|
48
|
+
url, method,
|
|
49
|
+
tokenLocation: tokenVerifyLocation,
|
|
50
|
+
tokenLocationName
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
this.options = parse("options", options as any, [
|
|
54
|
+
{ name: "url", required: true, type: "string", stringMinLength: 1 },
|
|
55
|
+
{ name: "method", required: true, type: "string", stringMinLength: 1 },
|
|
56
|
+
{ name: "tokenLocation", required: true, type: "enum", enumValues: ["header", "query", "cookie"] },
|
|
57
|
+
{ name: "tokenLocationName", required: true, type: "any" }
|
|
58
|
+
], "no_extra") as any;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public async verify({ token, ctx }: { token: string; ctx: Context }): Promise<Session | null> {
|
|
63
|
+
try {
|
|
64
|
+
let response = null;
|
|
65
|
+
const tokenVerifyLocation = this.options.tokenLocation;
|
|
66
|
+
const url = this.options.url;
|
|
67
|
+
const method = this.options.method;
|
|
68
|
+
switch (tokenVerifyLocation) {
|
|
69
|
+
case "header":
|
|
70
|
+
const tokenHeaderLocation = typeof this.options.tokenLocationName === "string" ? this.options.tokenLocationName : await this.options.tokenLocationName(ctx);
|
|
71
|
+
ctx.logger.trace(`verifying [${token}] on TOKEN_VERIFY_ENDPOINT=[${process.env.TOKEN_VERIFY_ENDPOINT}] TOKEN_HEADER=[${tokenHeaderLocation}]`);
|
|
72
|
+
response = await this.getResponse({
|
|
73
|
+
url,
|
|
74
|
+
headers: {
|
|
75
|
+
[tokenHeaderLocation]: token
|
|
76
|
+
},
|
|
77
|
+
method
|
|
78
|
+
}, ctx);
|
|
79
|
+
break;
|
|
80
|
+
case "query":
|
|
81
|
+
const tokenQueryLocation = typeof this.options.tokenLocationName === "string" ? this.options.tokenLocationName : await this.options.tokenLocationName(ctx);
|
|
82
|
+
ctx.logger.trace(`verifying [${token}] on TOKEN_VERIFY_ENDPOINT=[${process.env.TOKEN_VERIFY_ENDPOINT}] TOKEN_QUERY=[${tokenQueryLocation}]`);
|
|
83
|
+
response = await this.getResponse({
|
|
84
|
+
url,
|
|
85
|
+
query: {
|
|
86
|
+
[tokenQueryLocation]: token
|
|
87
|
+
},
|
|
88
|
+
method
|
|
89
|
+
}, ctx);
|
|
90
|
+
break;
|
|
91
|
+
case "cookie":
|
|
92
|
+
const tokenCookieLocation = typeof this.options.tokenLocationName === "string" ? this.options.tokenLocationName : await this.options.tokenLocationName(ctx);
|
|
93
|
+
ctx.logger.trace(`verifying [${token}] on TOKEN_VERIFY_ENDPOINT=[${process.env.TOKEN_VERIFY_ENDPOINT}] TOKEN_COOKIE=[${tokenCookieLocation}]`);
|
|
94
|
+
response = await this.getResponse({
|
|
95
|
+
url,
|
|
96
|
+
method,
|
|
97
|
+
headers: {
|
|
98
|
+
Cookie: `${tokenCookieLocation}=${token}; Path=/; HttpOnly;`
|
|
99
|
+
}
|
|
100
|
+
}, ctx);
|
|
101
|
+
break;
|
|
102
|
+
default:
|
|
103
|
+
throw new Error(`TOKEN_VERIFY_LOCATION=${tokenVerifyLocation} not supported use (header, query or cookie)`);
|
|
104
|
+
}
|
|
105
|
+
if (response) {
|
|
106
|
+
const session = await this.decodeSession(response, token, ctx);
|
|
107
|
+
if (!this.checkSession(session, ctx)) {
|
|
108
|
+
ctx.logger.warn(`unauthorized token not valid [${token}]`);
|
|
109
|
+
return null;
|
|
110
|
+
} else {
|
|
111
|
+
ctx.logger.trace("token [%s]authorized!", token);
|
|
112
|
+
|
|
113
|
+
if (tokenVerifyLocation === "cookie" && response.headers["set-cookie"]) {
|
|
114
|
+
const tokenCookieLocation = typeof this.options.tokenLocationName === "string" ? this.options.tokenLocationName : await this.options.tokenLocationName(ctx);
|
|
115
|
+
const cookies = response.headers["set-cookie"].map(c => COOKIE.parse(c)).filter(c => c[tokenCookieLocation] !== undefined);
|
|
116
|
+
if (cookies.length === 1) {
|
|
117
|
+
const value = cookies[0][tokenCookieLocation];
|
|
118
|
+
if (value) {
|
|
119
|
+
session.token = value; // replace token because token update via set-cookie
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
token,
|
|
125
|
+
...session
|
|
126
|
+
} as Session;
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
ctx.logger.warn(`unauthorized token not valid [${token}]`);
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
} catch (e: any) {
|
|
133
|
+
throw new UnAuthorizedError(`Fail to authenticate token! error verifying [${token}] [${e.status}][${e.config ? e.config.url : ""}]`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
137
|
+
protected async getResponse(config: RequestOptions, ctx?: Context): Promise<RequestResponse> {
|
|
138
|
+
return request(config);
|
|
139
|
+
}
|
|
140
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
141
|
+
protected checkSession(session: NoTokenSession, ctx?: Context): boolean {
|
|
142
|
+
parse("session", session, [
|
|
143
|
+
{ name: "username", required: true, type: "string" },
|
|
144
|
+
{ name: "account", required: true, type: "string" },
|
|
145
|
+
{ name: "groups", required: true, type: "array", arrayType: "string" }
|
|
146
|
+
], "add_extra");
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
150
|
+
protected async decodeSession(response: RequestResponse, token: string, ctx?: Context): Promise<NoTokenSession> {
|
|
151
|
+
const session = response.data;
|
|
152
|
+
return session as NoTokenSession;
|
|
153
|
+
}
|
|
154
|
+
}
|