@miqro/core 1.3.5 → 1.3.11
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 +81 -253
- package/dist/handler/api-router-utils.d.ts +13 -2
- package/dist/handler/api-router-utils.js +86 -1
- package/dist/handler/api-router-utils.js.map +1 -1
- package/dist/handler/api-router.d.ts +29 -2
- package/dist/handler/api-router.js +2 -2
- package/dist/handler/api-router.js.map +1 -1
- package/dist/handler/app.d.ts +4 -9
- package/dist/handler/app.js +10 -9
- package/dist/handler/app.js.map +1 -1
- package/dist/handler/common/context.d.ts +3 -11
- package/dist/handler/common/context.js +18 -13
- package/dist/handler/common/context.js.map +1 -1
- package/dist/handler/common/router-utils.d.ts +18 -10
- package/dist/handler/common/router-utils.js +97 -15
- package/dist/handler/common/router-utils.js.map +1 -1
- package/dist/handler/common/router.d.ts +18 -8
- package/dist/handler/common/router.js +12 -1
- package/dist/handler/common/router.js.map +1 -1
- package/dist/handler/index.d.ts +0 -3
- package/dist/handler/index.js +0 -3
- package/dist/handler/index.js.map +1 -1
- package/dist/handler/middleware/cookie.js +2 -2
- package/dist/handler/middleware/cookie.js.map +1 -1
- package/dist/handler/middleware/form.js +7 -1
- package/dist/handler/middleware/form.js.map +1 -1
- package/dist/handler/middleware/group.js +44 -2
- package/dist/handler/middleware/group.js.map +1 -1
- package/dist/handler/middleware/index.js +2 -6
- package/dist/handler/middleware/index.js.map +1 -1
- package/dist/handler/middleware/json.js +8 -2
- package/dist/handler/middleware/json.js.map +1 -1
- package/dist/handler/middleware/text.js +8 -1
- package/dist/handler/middleware/text.js.map +1 -1
- package/dist/handler/proxy.d.ts +2 -5
- package/dist/handler/proxy.js +2 -3
- package/dist/handler/proxy.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/{util/test → test/common}/app.d.ts +2 -2
- package/dist/{util/test → test/common}/app.js +3 -2
- package/dist/test/common/app.js.map +1 -0
- package/dist/{util/test → test/common}/common.d.ts +0 -0
- package/dist/{util/test → test/common}/common.js +0 -0
- package/dist/test/common/common.js.map +1 -0
- package/dist/{util/test → test/common}/index.d.ts +1 -0
- package/dist/{util/test → test/common}/index.js +1 -0
- package/dist/test/common/index.js.map +1 -0
- package/dist/test/common/runner.d.ts +28 -0
- package/dist/test/common/runner.js +88 -0
- package/dist/test/common/runner.js.map +1 -0
- package/dist/test/index.d.ts +1 -0
- package/dist/{handler/response → test}/index.js +1 -3
- package/dist/test/index.js.map +1 -0
- package/dist/test/main.d.ts +1 -0
- package/dist/test/main.js +31 -0
- package/dist/test/main.js.map +1 -0
- package/dist/util/cli/index.d.ts +5 -1
- package/dist/util/cli/index.js +26 -0
- package/dist/util/cli/index.js.map +1 -1
- package/dist/util/error/parsererror.d.ts +2 -1
- package/dist/util/error/parsererror.js +5 -1
- package/dist/util/error/parsererror.js.map +1 -1
- package/dist/util/index.d.ts +0 -1
- package/dist/util/index.js +0 -3
- package/dist/util/index.js.map +1 -1
- package/dist/util/loader/index.d.ts +0 -3
- package/dist/util/loader/index.js +2 -9
- package/dist/util/loader/index.js.map +1 -1
- package/dist/util/logger/formatter.js +1 -1
- package/dist/util/logger/formatter.js.map +1 -1
- package/dist/util/parser/get.js +3 -6
- package/dist/util/parser/get.js.map +1 -1
- package/dist/util/request/index.d.ts +1 -1
- package/dist/util/request/index.js +1 -3
- package/dist/util/request/index.js.map +1 -1
- package/dist/util/session/common.d.ts +0 -1
- package/dist/util/session/common.js +0 -46
- package/dist/util/session/common.js.map +1 -1
- package/dist/util/session/service.js +2 -2
- package/dist/util/session/service.js.map +1 -1
- package/package.json +13 -11
- package/src/handler/api-router-utils.ts +91 -2
- package/src/handler/api-router.ts +30 -4
- package/src/handler/app.ts +10 -15
- package/src/handler/common/context.ts +19 -24
- package/src/handler/common/router-utils.ts +207 -108
- package/src/handler/common/router.ts +30 -8
- package/src/handler/index.ts +0 -3
- package/src/handler/middleware/cookie.ts +12 -12
- package/src/handler/middleware/form.ts +8 -2
- package/src/handler/middleware/group.ts +44 -2
- package/src/handler/middleware/index.ts +0 -4
- package/src/handler/middleware/json.ts +9 -3
- package/src/handler/middleware/text.ts +8 -1
- package/src/handler/proxy.ts +3 -8
- package/src/index.ts +1 -0
- package/src/{util/test → test/common}/app.ts +6 -5
- package/src/{util/test → test/common}/common.ts +0 -0
- package/src/{util/test → test/common}/index.ts +1 -0
- package/src/test/common/runner.ts +116 -0
- package/src/test/index.ts +1 -0
- package/src/test/main.ts +32 -0
- package/src/util/cli/index.ts +24 -1
- package/src/util/error/parsererror.ts +6 -1
- package/src/util/index.ts +0 -3
- package/src/util/loader/index.ts +1 -7
- package/src/util/logger/formatter.ts +1 -1
- package/src/util/parser/get.ts +3 -5
- package/src/util/request/index.ts +1 -1
- package/src/util/session/common.ts +0 -43
- package/src/util/session/service.ts +126 -126
- package/dist/handler/api-handler.d.ts +0 -29
- package/dist/handler/api-handler.js +0 -42
- package/dist/handler/api-handler.js.map +0 -1
- package/dist/handler/feature-router.d.ts +0 -12
- package/dist/handler/feature-router.js +0 -90
- package/dist/handler/feature-router.js.map +0 -1
- package/dist/handler/response/html.d.ts +0 -10
- package/dist/handler/response/html.js +0 -34
- package/dist/handler/response/html.js.map +0 -1
- package/dist/handler/response/index.d.ts +0 -3
- package/dist/handler/response/index.js.map +0 -1
- package/dist/handler/response/json.d.ts +0 -2
- package/dist/handler/response/json.js +0 -21
- package/dist/handler/response/json.js.map +0 -1
- package/dist/handler/response/result.d.ts +0 -4
- package/dist/handler/response/result.js +0 -46
- package/dist/handler/response/result.js.map +0 -1
- package/dist/util/cookie/cookie.d.ts +0 -50
- package/dist/util/cookie/cookie.js +0 -168
- package/dist/util/cookie/cookie.js.map +0 -1
- package/dist/util/cookie/index.d.ts +0 -16
- package/dist/util/cookie/index.js +0 -9
- package/dist/util/cookie/index.js.map +0 -1
- package/dist/util/test/app.js.map +0 -1
- package/dist/util/test/common.js.map +0 -1
- package/dist/util/test/index.js.map +0 -1
- package/dist/util/uuid/index.d.ts +0 -7
- package/dist/util/uuid/index.js +0 -8
- package/dist/util/uuid/index.js.map +0 -1
- package/dist/util/uuid/regex.d.ts +0 -2
- package/dist/util/uuid/regex.js +0 -5
- package/dist/util/uuid/regex.js.map +0 -1
- package/dist/util/uuid/rng.d.ts +0 -1
- package/dist/util/uuid/rng.js +0 -18
- package/dist/util/uuid/rng.js.map +0 -1
- package/dist/util/uuid/stringify.d.ts +0 -2
- package/dist/util/uuid/stringify.js +0 -50
- package/dist/util/uuid/stringify.js.map +0 -1
- package/dist/util/uuid/v4.d.ts +0 -5
- package/dist/util/uuid/v4.js +0 -27
- package/dist/util/uuid/v4.js.map +0 -1
- package/dist/util/uuid/validate.d.ts +0 -2
- package/dist/util/uuid/validate.js +0 -12
- package/dist/util/uuid/validate.js.map +0 -1
- package/src/handler/api-handler.ts +0 -55
- package/src/handler/feature-router.ts +0 -92
- package/src/handler/response/html.ts +0 -36
- package/src/handler/response/index.ts +0 -3
- package/src/handler/response/json.ts +0 -19
- package/src/handler/response/result.ts +0 -40
- package/src/util/cookie/cookie.ts +0 -207
- package/src/util/cookie/index.ts +0 -6
- package/src/util/uuid/index.ts +0 -5
- package/src/util/uuid/regex.ts +0 -2
- package/src/util/uuid/rng.ts +0 -14
- package/src/util/uuid/stringify.ts +0 -53
- package/src/util/uuid/v4.ts +0 -30
- package/src/util/uuid/validate.ts +0 -8
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import { request, RequestOptions, RequestResponse
|
|
1
|
+
import { request, RequestOptions, RequestResponse } from "../../util/request";
|
|
2
2
|
import { App } from "../../handler/app";
|
|
3
3
|
import { existsSync, unlinkSync } from "fs";
|
|
4
4
|
import { createServer } from "http";
|
|
5
5
|
|
|
6
|
-
export const TestHelper = async (app: App, options: RequestOptions, cb?: (response: RequestResponse) => void): Promise<RequestResponse
|
|
6
|
+
export const TestHelper = async (app: App, options: RequestOptions, cb?: (response: RequestResponse) => void): Promise<RequestResponse> => {
|
|
7
7
|
const unixSocket = `/tmp/socket.test.helper${Date.now()}`;
|
|
8
8
|
if (existsSync(unixSocket)) {
|
|
9
9
|
unlinkSync(unixSocket);
|
|
10
10
|
}
|
|
11
11
|
const server = createServer(app.listener);
|
|
12
|
-
return new Promise<RequestResponse
|
|
12
|
+
return new Promise<RequestResponse>((resolve, reject) => {
|
|
13
13
|
server.listen(unixSocket, () => {
|
|
14
14
|
request({
|
|
15
|
+
disableThrow: true,
|
|
15
16
|
...options,
|
|
16
17
|
socketPath: unixSocket
|
|
17
18
|
}).then((response) => {
|
|
@@ -25,7 +26,7 @@ export const TestHelper = async (app: App, options: RequestOptions, cb?: (respon
|
|
|
25
26
|
}
|
|
26
27
|
resolve(response);
|
|
27
28
|
});
|
|
28
|
-
}).catch((e:
|
|
29
|
+
}).catch((e: any) => {
|
|
29
30
|
server.close(() => {
|
|
30
31
|
if (cb) {
|
|
31
32
|
try {
|
|
@@ -33,7 +34,7 @@ export const TestHelper = async (app: App, options: RequestOptions, cb?: (respon
|
|
|
33
34
|
} catch (ee) {
|
|
34
35
|
reject(ee);
|
|
35
36
|
}
|
|
36
|
-
resolve();
|
|
37
|
+
resolve(undefined as any);
|
|
37
38
|
} else {
|
|
38
39
|
reject(e);
|
|
39
40
|
}
|
|
File without changes
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { Console, count } from "console";
|
|
2
|
+
import { format } from "util";
|
|
3
|
+
import { Logger } from "../../util";
|
|
4
|
+
|
|
5
|
+
export type TestFunction = () => void | Promise<void>;
|
|
6
|
+
const DEFAULT_TIMEOUT = 2000;
|
|
7
|
+
|
|
8
|
+
interface Test { run: TestFunction; title: string; category?: string; fullName: string; }
|
|
9
|
+
|
|
10
|
+
const tests: Test[] = [];
|
|
11
|
+
|
|
12
|
+
export const getTestCount = () => tests.length;
|
|
13
|
+
|
|
14
|
+
export const it = (title: string, impl: TestFunction, options?: {
|
|
15
|
+
category?: string;
|
|
16
|
+
timeout?: number;
|
|
17
|
+
before?: TestFunction;
|
|
18
|
+
after?: TestFunction;
|
|
19
|
+
}, logger: Logger | Console = new Console(process.stdout)): void => {
|
|
20
|
+
const category = options && options.category ? options.category : undefined;
|
|
21
|
+
const fullName = `${category ? `${category} [` : ""}${title}${category ? "]" : ""}`;
|
|
22
|
+
tests.push({
|
|
23
|
+
run: () => new Promise((resolve, reject) => {
|
|
24
|
+
const timeoutMS = options && options.timeout ? options.timeout : DEFAULT_TIMEOUT;
|
|
25
|
+
const timeout = setTimeout(() => {
|
|
26
|
+
reject(new Error(format("%s failed\x1b[31m\n%s timeout %o\x1b", fullName, fullName, timeoutMS)));
|
|
27
|
+
}, timeoutMS);
|
|
28
|
+
try {
|
|
29
|
+
(async () => {
|
|
30
|
+
if (options && options.before) {
|
|
31
|
+
await options.before();
|
|
32
|
+
}
|
|
33
|
+
const startMS = Date.now();
|
|
34
|
+
await impl();
|
|
35
|
+
const took = Date.now() - startMS;
|
|
36
|
+
if (options && options.after) {
|
|
37
|
+
await options.after();
|
|
38
|
+
}
|
|
39
|
+
return took;
|
|
40
|
+
})().then((took) => {
|
|
41
|
+
clearTimeout(timeout);
|
|
42
|
+
logger.log("\x1b[32mpassed %s (took %sms)\x1b[0m", fullName, took);
|
|
43
|
+
resolve();
|
|
44
|
+
}).catch(e => {
|
|
45
|
+
clearTimeout(timeout);
|
|
46
|
+
reject(new Error(format("%s failed\n%s finished with errors %o", fullName, fullName, e)));
|
|
47
|
+
});
|
|
48
|
+
} catch (e) {
|
|
49
|
+
clearTimeout(timeout);
|
|
50
|
+
reject(new Error(format("%s failed\n%s finished with errors %o", fullName, fullName, e)));
|
|
51
|
+
}
|
|
52
|
+
}), title,
|
|
53
|
+
category,
|
|
54
|
+
fullName
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const runTests = async (title?: string | string[], logger: Logger | Console = new Console(process.stdout)): Promise<{
|
|
59
|
+
passed: number;
|
|
60
|
+
total: number;
|
|
61
|
+
ignored: number;
|
|
62
|
+
failed: {
|
|
63
|
+
error: any;
|
|
64
|
+
fullName: string;
|
|
65
|
+
}[];
|
|
66
|
+
}> => {
|
|
67
|
+
const titles = title ? title instanceof Array ? title : [title] : [undefined];
|
|
68
|
+
const ret: {
|
|
69
|
+
passed: number;
|
|
70
|
+
total: number;
|
|
71
|
+
ignored: number;
|
|
72
|
+
failed: {
|
|
73
|
+
error: any;
|
|
74
|
+
fullName: string;
|
|
75
|
+
}[];
|
|
76
|
+
} = {
|
|
77
|
+
total: tests.length,
|
|
78
|
+
ignored: 0,
|
|
79
|
+
passed: 0,
|
|
80
|
+
failed: []
|
|
81
|
+
};
|
|
82
|
+
for (const test of tests) {
|
|
83
|
+
for (const title of titles) {
|
|
84
|
+
if (title && test.fullName.indexOf(title) !== -1 || !title) {
|
|
85
|
+
try {
|
|
86
|
+
await test.run();
|
|
87
|
+
ret.passed++;
|
|
88
|
+
} catch (e) {
|
|
89
|
+
logger.error("\x1b[31m%s\x1b[0m", e);
|
|
90
|
+
ret.failed.push({
|
|
91
|
+
error: e,
|
|
92
|
+
fullName: test.fullName
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
} else {
|
|
96
|
+
ret.ignored++;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return ret;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export const runTestModules = async (modules: string[], title?: string | string[], logger: Logger | Console = new Console(process.stdout)): Promise<{
|
|
104
|
+
passed: number;
|
|
105
|
+
total: number;
|
|
106
|
+
ignored: number;
|
|
107
|
+
failed: {
|
|
108
|
+
error: any;
|
|
109
|
+
fullName: string;
|
|
110
|
+
}[];
|
|
111
|
+
}> => {
|
|
112
|
+
for (const path of modules) {
|
|
113
|
+
require(path);
|
|
114
|
+
}
|
|
115
|
+
return await runTests(title, logger);
|
|
116
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./common";
|
package/src/test/main.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { resolve } from "path";
|
|
2
|
+
import { CLIUtil, getLogger } from "../util";
|
|
3
|
+
import { runTestModules } from "./common";
|
|
4
|
+
|
|
5
|
+
const startMS = Date.now();
|
|
6
|
+
|
|
7
|
+
const args = CLIUtil.extractFlags(process.argv.slice(2));
|
|
8
|
+
const name = args.flags.n ? args.flags.n : "all";
|
|
9
|
+
|
|
10
|
+
const logger = console;
|
|
11
|
+
|
|
12
|
+
const modules = args.cmds;
|
|
13
|
+
runTestModules(modules.map(m => resolve(process.cwd(), m)), typeof name === "string" && name.toLowerCase() === "all" ? undefined : name, console).then((ret) => {
|
|
14
|
+
const took = Date.now() - startMS;
|
|
15
|
+
logger.log("");
|
|
16
|
+
logger.log("");
|
|
17
|
+
logger.log(ret.passed + " tests pased");
|
|
18
|
+
logger.log(ret.failed.length + " failed");
|
|
19
|
+
logger.log("took " + took + "ms");
|
|
20
|
+
logger.log("");
|
|
21
|
+
logger.log("");
|
|
22
|
+
if (ret.failed.length > 0) {
|
|
23
|
+
ret.failed.forEach(e => {
|
|
24
|
+
logger.error("\x1b[31m%s\x1b[0m", e.fullName);
|
|
25
|
+
logger.error("\x1b[31m%s\x1b[0m", e.error);
|
|
26
|
+
});
|
|
27
|
+
process.exit(1);
|
|
28
|
+
}
|
|
29
|
+
}).catch(e => {
|
|
30
|
+
logger.error(e);
|
|
31
|
+
process.exit(1);
|
|
32
|
+
});
|
package/src/util/cli/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SimpleMap } from "../parser/common";
|
|
2
2
|
import { Logger } from "../logger";
|
|
3
|
-
import { Callback } from "
|
|
3
|
+
import { Callback } from "../../test/common";
|
|
4
4
|
|
|
5
5
|
// noinspection SpellCheckingInspection
|
|
6
6
|
const routeCMDModule = async (cmdArg: string | undefined, cmds: CMDMapType, logger: Logger | Console, exit = true): Promise<void> => {
|
|
@@ -28,6 +28,29 @@ const routeCMDModule = async (cmdArg: string | undefined, cmds: CMDMapType, logg
|
|
|
28
28
|
export type CMDMapType = SimpleMap<{ cb: Callback<void> | Callback<Promise<void>>; description: string; section?: string; }>
|
|
29
29
|
|
|
30
30
|
export abstract class CLIUtil {
|
|
31
|
+
public static extractFlags(args: string[]): { flags: SimpleMap<string | string[]>; cmds: string[]; } {
|
|
32
|
+
const flags: SimpleMap<string | string[]> = {};
|
|
33
|
+
const cmds: string[] = [];
|
|
34
|
+
for (let i = 0; i < args.length; i++) {
|
|
35
|
+
const arg = args[i];
|
|
36
|
+
if (arg.indexOf("-") === 0) {
|
|
37
|
+
const argName = arg.substring(arg.indexOf("--") === 0 ? 2 : 1);
|
|
38
|
+
const argValue = args.length > i + 1 && args[i + 1] && args[i + 1].indexOf("-") != 0 ? args[i + 1] : "";
|
|
39
|
+
const flag = flags[argName];
|
|
40
|
+
if (flag instanceof Array) {
|
|
41
|
+
flag.push(argValue);
|
|
42
|
+
} else if (flag) {
|
|
43
|
+
flags[argName] = [flag, argValue];
|
|
44
|
+
} else {
|
|
45
|
+
flags[argName] = argValue;
|
|
46
|
+
}
|
|
47
|
+
i++;
|
|
48
|
+
} else {
|
|
49
|
+
cmds.push(arg);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return { flags, cmds };
|
|
53
|
+
}
|
|
31
54
|
// noinspection SpellCheckingInspection
|
|
32
55
|
public static cliFlow(cmds: CMDMapType, usage: string, logger: Logger | Console, cmdArg = process.argv[2], exit = true): void {
|
|
33
56
|
const flow = async () => {
|
|
@@ -7,4 +7,9 @@ export class ParseOptionsError extends NamedError {
|
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
export class BadRequestError extends
|
|
10
|
+
export class BadRequestError extends NamedError {
|
|
11
|
+
constructor(message = "BAD REQUEST") {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "BadRequestError";
|
|
14
|
+
}
|
|
15
|
+
}
|
package/src/util/index.ts
CHANGED
package/src/util/loader/index.ts
CHANGED
|
@@ -3,9 +3,8 @@ import { Logger } from "../logger";
|
|
|
3
3
|
import { existsSync, readdirSync, readFileSync } from "fs";
|
|
4
4
|
import { ConfigOutput, ConfigPathResolver, ConfigStore, LoadConfigOut } from "../config";
|
|
5
5
|
import { ConfigFileNotFoundError } from "../error";
|
|
6
|
-
import { RequestOptions, request } from "../request";
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
const loadConfigString = (envFileContent: string, combined?: ConfigOutput): ConfigOutput => {
|
|
9
8
|
const ret: ConfigOutput = {};
|
|
10
9
|
envFileContent.split("\n")
|
|
11
10
|
.filter(value => value && value.length > 0 && value.substr(0, 1) !== "#")
|
|
@@ -26,11 +25,6 @@ export const loadConfigString = (envFileContent: string, combined?: ConfigOutput
|
|
|
26
25
|
return ret;
|
|
27
26
|
}
|
|
28
27
|
|
|
29
|
-
export const loadRemoteConfigFile = async (options: RequestOptions, logger?: Logger): Promise<ConfigOutput> => {
|
|
30
|
-
const { data } = await request(options, logger);
|
|
31
|
-
return loadConfigString(data);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
28
|
export const loadConfigFile = (envFilePath: string, combined?: ConfigOutput, logger?: Logger): ConfigOutput => {
|
|
35
29
|
if (!existsSync(envFilePath)) {
|
|
36
30
|
throw new ConfigFileNotFoundError(`config file [${envFilePath}] doesnt exists!`);
|
|
@@ -4,6 +4,6 @@ export type LoggerFormatter = (args: { identifier: string, level: LogLevel, mess
|
|
|
4
4
|
|
|
5
5
|
export const defaultLoggerFormatter: LoggerFormatter =
|
|
6
6
|
({ identifier, level, message }) => `${new Date().toISOString()} ${process.pid} ` +
|
|
7
|
-
`[${identifier}] ` +
|
|
7
|
+
`${identifier ? `[${identifier}] ` : ""}` +
|
|
8
8
|
`${level !== "info" ? (level === "error" || level === "warn" ? `[${level.toUpperCase()}] ` : `[${level}] `) : ""}` +
|
|
9
9
|
`${message}`;
|
package/src/util/parser/get.ts
CHANGED
|
@@ -3,14 +3,12 @@ import { ParseOptionsError } from "../error";
|
|
|
3
3
|
import { BasicParseOption, ParseValueArgs } from "./common";
|
|
4
4
|
|
|
5
5
|
/*
|
|
6
|
-
usage const name = get(obj, "user.info.name", {type: "string"});
|
|
6
|
+
usage const name = get(obj, "user.info.name", defaultValue, {type: "string"}, parser);
|
|
7
7
|
*/
|
|
8
8
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
9
9
|
export const get = (obj: any, attrPath: string, defaultValue?: any, option?: BasicParseOption, parser?: Parser): any | undefined => {
|
|
10
|
-
if (typeof obj !== "object"
|
|
11
|
-
|
|
12
|
-
} else if (!obj && defaultValue !== undefined) {
|
|
13
|
-
return defaultValue;
|
|
10
|
+
if (!obj || typeof obj !== "object") {
|
|
11
|
+
return defaultValue !== undefined ? defaultValue : undefined
|
|
14
12
|
}
|
|
15
13
|
if (typeof attrPath !== "string") {
|
|
16
14
|
throw new Error(`attrPath must be typeof string`);
|
|
@@ -5,7 +5,7 @@ import { parseRedirectLocation, RequestOptions, RequestResponse, ResponseError,
|
|
|
5
5
|
import { gunzipSync } from "zlib";
|
|
6
6
|
import { isBrowser } from "../config/browser";
|
|
7
7
|
|
|
8
|
-
export { RequestOptions, RequestResponse
|
|
8
|
+
export { RequestOptions, RequestResponse } from "./common";
|
|
9
9
|
|
|
10
10
|
const DEFAULT_USER_AGENT = "curl/7.69.1";
|
|
11
11
|
const CONTENT_TYPE_HEADER = "Content-Type";
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { ParseOptionsError } from "../error";
|
|
2
1
|
import { SimpleMap, SimpleTypes } from "../parser/common";
|
|
3
2
|
|
|
4
3
|
export interface NoTokenSession extends SimpleMap<SimpleTypes> {
|
|
@@ -19,45 +18,3 @@ export interface GroupPolicy {
|
|
|
19
18
|
groups: GroupPolicyGroups[];
|
|
20
19
|
groupPolicy: GroupPolicyType;
|
|
21
20
|
}
|
|
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
|
-
};
|
|
@@ -4,7 +4,7 @@ import { checkEnvVariables } from "../config";
|
|
|
4
4
|
import { UnAuthorizedError } from "../error";
|
|
5
5
|
import { request, RequestOptions, RequestResponse } from "../request";
|
|
6
6
|
import { parse } from "../parser";
|
|
7
|
-
import {
|
|
7
|
+
import { parse as cookieParse } from "cookie";
|
|
8
8
|
|
|
9
9
|
const DEFAULT_TOKEN_LOCATION = "header";
|
|
10
10
|
const DEFAULT_TOKEN_HEADER = "Authorization";
|
|
@@ -12,143 +12,143 @@ const DEFAULT_TOKEN_QUERY = "token";
|
|
|
12
12
|
const DEFAULT_TOKEN_COOKIE = "Authorization";
|
|
13
13
|
|
|
14
14
|
export interface VerifyTokenService {
|
|
15
|
-
|
|
15
|
+
verify(args: { token: string; ctx: Context }): Promise<Session | null>;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export interface VerifyEndpointServiceOptions {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
url: string;
|
|
20
|
+
method: string;
|
|
21
|
+
tokenLocation: "header" | "query" | "cookie";
|
|
22
|
+
tokenLocationName: string | ((ctx: Context) => Promise<string>);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export class VerifyEndpointService implements VerifyTokenService {
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
protected options: VerifyEndpointServiceOptions;
|
|
28
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
|
-
}
|
|
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;
|
|
60
59
|
}
|
|
60
|
+
}
|
|
61
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
|
-
}, ctx);
|
|
101
|
-
break;
|
|
102
|
-
default:
|
|
103
|
-
throw new Error(`TOKEN_VERIFY_LOCATION=${tokenVerifyLocation} not supported use (header, query or cookie)`);
|
|
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;`
|
|
104
99
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
112
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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;
|
|
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 => cookieParse(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
|
+
}
|
|
131
121
|
}
|
|
132
|
-
|
|
133
|
-
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
token,
|
|
125
|
+
...session
|
|
126
|
+
} as Session;
|
|
134
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 : ""}]`);
|
|
135
134
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
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
|
+
}
|