@miqro/core 1.3.9 → 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/dist/handler/common/context.d.ts +2 -10
- package/dist/handler/common/context.js +4 -4
- package/dist/handler/common/context.js.map +1 -1
- package/dist/handler/middleware/cookie.js +2 -2
- package/dist/handler/middleware/cookie.js.map +1 -1
- package/dist/util/index.js +0 -2
- package/dist/util/index.js.map +1 -1
- package/dist/util/session/service.js +2 -2
- package/dist/util/session/service.js.map +1 -1
- package/package.json +8 -2
- package/src/handler/common/context.ts +5 -14
- package/src/handler/middleware/cookie.ts +12 -12
- package/src/util/index.ts +0 -2
- package/src/util/session/service.ts +126 -126
- 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/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/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
- package/test.md +0 -0
- package/tsconfig-test.json +0 -13
|
@@ -3,6 +3,7 @@ import { Logger } from "../../util/logger";
|
|
|
3
3
|
import { Session } from "../../util/session";
|
|
4
4
|
import { IncomingHttpHeaders, IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "http";
|
|
5
5
|
import { URL } from "url";
|
|
6
|
+
import { CookieSerializeOptions } from "cookie";
|
|
6
7
|
export declare type Method = "get" | "post" | "put" | "delete" | "patch" | "options" | "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS";
|
|
7
8
|
export declare type Handler<T = boolean | void | any> = ((ctx: Context) => Promise<T>) | ((ctx: Context) => T);
|
|
8
9
|
export declare type ErrorHandler<T = boolean | void | any> = ((e: unknown, ctx: Context) => Promise<T>) | ((e: unknown, ctx: Context) => T);
|
|
@@ -35,16 +36,7 @@ export declare class Context {
|
|
|
35
36
|
close(): Promise<void>;
|
|
36
37
|
addVaryHeader(value: number | string | ReadonlyArray<string>): ServerResponse;
|
|
37
38
|
setHeader(name: string, value: number | string | ReadonlyArray<string>): ServerResponse;
|
|
38
|
-
setCookie(name: string, value: string, options?:
|
|
39
|
-
encode?: (uriComponent: string | number | boolean) => string;
|
|
40
|
-
maxAge?: number | null;
|
|
41
|
-
domain?: string;
|
|
42
|
-
httpOnly?: boolean;
|
|
43
|
-
secure?: boolean;
|
|
44
|
-
path?: string;
|
|
45
|
-
expires?: Date;
|
|
46
|
-
sameSite?: "lax" | "strict" | "none" | true;
|
|
47
|
-
}): ServerResponse;
|
|
39
|
+
setCookie(name: string, value: string, options?: CookieSerializeOptions): ServerResponse;
|
|
48
40
|
end({ status, headers, body }: {
|
|
49
41
|
body?: any;
|
|
50
42
|
status: number;
|
|
@@ -5,8 +5,8 @@ const logger_1 = require("../../util/logger");
|
|
|
5
5
|
const tokenize_match_1 = require("./tokenize-match");
|
|
6
6
|
const formatter_1 = require("../../util/logger/formatter");
|
|
7
7
|
const helpers_1 = require("./helpers");
|
|
8
|
-
const uuid_1 = require("
|
|
9
|
-
const cookie_1 = require("
|
|
8
|
+
const uuid_1 = require("uuid");
|
|
9
|
+
const cookie_1 = require("cookie");
|
|
10
10
|
class Context {
|
|
11
11
|
constructor(req, res, logger) {
|
|
12
12
|
this.req = req;
|
|
@@ -24,7 +24,7 @@ class Context {
|
|
|
24
24
|
this.cookies = {}; // a middleware will fill
|
|
25
25
|
this.headers = req.headers;
|
|
26
26
|
this.startMS = Date.now();
|
|
27
|
-
this.uuid = uuid_1.
|
|
27
|
+
this.uuid = (0, uuid_1.v4)().toString();
|
|
28
28
|
this.results = []; // handlers will fill this
|
|
29
29
|
const pathToEnv = this.path.replace(/\//ig, "_").toUpperCase();
|
|
30
30
|
const identifier = `${pathToEnv === "_" ? "" : pathToEnv.substring(1)}${this.method.toUpperCase()}`;
|
|
@@ -57,7 +57,7 @@ class Context {
|
|
|
57
57
|
return this.res.setHeader(name, value);
|
|
58
58
|
}
|
|
59
59
|
setCookie(name, value, options) {
|
|
60
|
-
return this.setHeader('Set-Cookie', cookie_1.
|
|
60
|
+
return this.setHeader('Set-Cookie', (0, cookie_1.serialize)(name, String(value), options));
|
|
61
61
|
}
|
|
62
62
|
async end({ status, headers, body }) {
|
|
63
63
|
return new Promise((resolve, reject) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../src/handler/common/context.ts"],"names":[],"mappings":";;;AAAA,8CAAsD;AAGtD,qDAAoE;AAEpE,2DAAqE;AACrE,uCAAmC;AACnC
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../src/handler/common/context.ts"],"names":[],"mappings":";;;AAAA,8CAAsD;AAGtD,qDAAoE;AAEpE,2DAAqE;AACrE,uCAAmC;AACnC,+BAA0B;AAC1B,mCAA2D;AAU3D,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AG;YAC3C,OAAO,EAAE;gBACP,CAAC,cAAc,CAAC,EAAE,2BAA2B;gBAC7C,GAAG,OAAO;aACX;YACD,IAAI,EAAE,IAAI;SACX,CAAC,CAAC;IACL,CAAC;IACM,KAAK,CAAC,IAAI,CAAC,IAAY,EAAE,OAA6B,EAAE,MAAe;QAC5E,OAAO,IAAI,CAAC,GAAG,CAAC;YACd,MAAM,EAAE,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG;YAC3C,OAAO,EAAE;gBACP,CAAC,cAAc,CAAC,EAAE,0BAA0B;gBAC5C,GAAG,OAAO;aACX;YACD,IAAI,EAAE,IAAI;SACX,CAAC,CAAA;IACJ,CAAC;IACM,KAAK,CAAC,QAAQ,CAAC,GAAW,EAAE,OAA6B,EAAE,MAAe;QAC/E,OAAO,IAAI,CAAC,GAAG,CAAC;YACd,MAAM,EAAE,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG;YAC3C,OAAO,EAAE;gBACP,CAAC,UAAU,CAAC,EAAE,GAAG;gBACjB,GAAG,OAAO;aACX;SACF,CAAC,CAAA;IACJ,CAAC;CACF;AAxID,0BAwIC"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CookieParser = void 0;
|
|
4
|
-
const cookie_1 = require("
|
|
4
|
+
const cookie_1 = require("cookie");
|
|
5
5
|
const CookieParser = (options) => {
|
|
6
6
|
return async (ctx) => {
|
|
7
|
-
const cookies = cookie_1.
|
|
7
|
+
const cookies = (0, cookie_1.parse)(ctx.headers.cookie || '', options);
|
|
8
8
|
const cookieList = Object.keys(cookies);
|
|
9
9
|
for (const name of cookieList) {
|
|
10
10
|
const value = cookies[name];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../src/handler/middleware/cookie.ts"],"names":[],"mappings":";;;AAEA,
|
|
1
|
+
{"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../src/handler/middleware/cookie.ts"],"names":[],"mappings":";;;AAEA,mCAA+B;AAExB,MAAM,YAAY,GAAG,CAAC,OAE5B,EAAiB,EAAE;IAClB,OAAO,KAAK,EAAE,GAAY,EAAiB,EAAE;QAC3C,MAAM,OAAO,GAAG,IAAA,cAAK,EAAC,GAAG,CAAC,OAAO,CAAC,MAAM,IAAI,EAAE,EAAE,OAAO,CAAC,CAAC;QACzD,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACxC,KAAK,MAAM,IAAI,IAAI,UAAU,EAAE;YAC7B,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;YAC5B,IAAI,KAAK,EAAE;gBACT,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;aAC3B;SACF;IACH,CAAC,CAAC;AACJ,CAAC,CAAA;AAbY,QAAA,YAAY,gBAaxB"}
|
package/dist/util/index.js
CHANGED
|
@@ -18,6 +18,4 @@ __exportStar(require("./config"), exports);
|
|
|
18
18
|
__exportStar(require("./logger"), exports);
|
|
19
19
|
__exportStar(require("./session"), exports);
|
|
20
20
|
__exportStar(require("./loader"), exports);
|
|
21
|
-
//export * from "./uuid";
|
|
22
|
-
//export * from "./cookie";
|
|
23
21
|
//# sourceMappingURL=index.js.map
|
package/dist/util/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/util/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,0CAAwB;AACxB,2CAAyB;AACzB,4CAA0B;AAC1B,wCAAsB;AACtB,2CAAyB;AACzB,2CAAyB;AACzB,4CAA0B;AAC1B,2CAAyB
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/util/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,0CAAwB;AACxB,2CAAyB;AACzB,4CAA0B;AAC1B,wCAAsB;AACtB,2CAAyB;AACzB,2CAAyB;AACzB,4CAA0B;AAC1B,2CAAyB"}
|
|
@@ -5,7 +5,7 @@ const config_1 = require("../config");
|
|
|
5
5
|
const error_1 = require("../error");
|
|
6
6
|
const request_1 = require("../request");
|
|
7
7
|
const parser_1 = require("../parser");
|
|
8
|
-
const cookie_1 = require("
|
|
8
|
+
const cookie_1 = require("cookie");
|
|
9
9
|
const DEFAULT_TOKEN_LOCATION = "header";
|
|
10
10
|
const DEFAULT_TOKEN_HEADER = "Authorization";
|
|
11
11
|
const DEFAULT_TOKEN_QUERY = "token";
|
|
@@ -97,7 +97,7 @@ class VerifyEndpointService {
|
|
|
97
97
|
ctx.logger.trace("token [%s]authorized!", token);
|
|
98
98
|
if (tokenVerifyLocation === "cookie" && response.headers["set-cookie"]) {
|
|
99
99
|
const tokenCookieLocation = typeof this.options.tokenLocationName === "string" ? this.options.tokenLocationName : await this.options.tokenLocationName(ctx);
|
|
100
|
-
const cookies = response.headers["set-cookie"].map(c => cookie_1.
|
|
100
|
+
const cookies = response.headers["set-cookie"].map(c => (0, cookie_1.parse)(c)).filter(c => c[tokenCookieLocation] !== undefined);
|
|
101
101
|
if (cookies.length === 1) {
|
|
102
102
|
const value = cookies[0][tokenCookieLocation];
|
|
103
103
|
if (value) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.js","sourceRoot":"","sources":["../../../src/util/session/service.ts"],"names":[],"mappings":";;;AAEA,sCAA8C;AAC9C,oCAA6C;AAC7C,wCAAsE;AACtE,sCAAkC;AAClC,
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../../../src/util/session/service.ts"],"names":[],"mappings":";;;AAEA,sCAA8C;AAC9C,oCAA6C;AAC7C,wCAAsE;AACtE,sCAAkC;AAClC,mCAA8C;AAE9C,MAAM,sBAAsB,GAAG,QAAQ,CAAC;AACxC,M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
"version": "1.3.
|
|
3
|
+
"version": "1.3.11",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -20,11 +20,17 @@
|
|
|
20
20
|
"author": "claukers",
|
|
21
21
|
"license": "ISC",
|
|
22
22
|
"devDependencies": {
|
|
23
|
+
"@types/cookie": "^0.4.1",
|
|
23
24
|
"@types/node": "^16.7.10",
|
|
24
|
-
"
|
|
25
|
+
"@types/uuid": "^8.3.3",
|
|
26
|
+
"typescript": "^4.5.4"
|
|
25
27
|
},
|
|
26
28
|
"engines": {
|
|
27
29
|
"node": ">=14.0.0",
|
|
28
30
|
"npm": ">=6.0.0"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"cookie": "0.4.1",
|
|
34
|
+
"uuid": "8.3.2"
|
|
29
35
|
}
|
|
30
36
|
}
|
|
@@ -5,8 +5,8 @@ import { normalizePath, parseSearchParams } from "./tokenize-match";
|
|
|
5
5
|
import { URL } from "url";
|
|
6
6
|
import { defaultLoggerFormatter } from "../../util/logger/formatter";
|
|
7
7
|
import { newURL } from "./helpers";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
8
|
+
import { v4 } from "uuid";
|
|
9
|
+
import { CookieSerializeOptions, serialize } from "cookie";
|
|
10
10
|
|
|
11
11
|
export type Method = "get" | "post" | "put" | "delete" | "patch" | "options" | "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS";
|
|
12
12
|
|
|
@@ -46,7 +46,7 @@ export class Context {
|
|
|
46
46
|
this.cookies = {}; // a middleware will fill
|
|
47
47
|
this.headers = req.headers;
|
|
48
48
|
this.startMS = Date.now();
|
|
49
|
-
this.uuid =
|
|
49
|
+
this.uuid = v4().toString();
|
|
50
50
|
this.results = []; // handlers will fill this
|
|
51
51
|
const pathToEnv = this.path.replace(/\//ig, "_").toUpperCase();
|
|
52
52
|
const identifier = `${pathToEnv === "_" ? "" : pathToEnv.substring(1)}${this.method.toUpperCase()}`;
|
|
@@ -83,17 +83,8 @@ export class Context {
|
|
|
83
83
|
public setHeader(name: string, value: number | string | ReadonlyArray<string>): ServerResponse {
|
|
84
84
|
return this.res.setHeader(name, value);
|
|
85
85
|
}
|
|
86
|
-
public setCookie(name: string, value: string, options?: {
|
|
87
|
-
|
|
88
|
-
maxAge?: number | null;
|
|
89
|
-
domain?: string;
|
|
90
|
-
httpOnly?: boolean;
|
|
91
|
-
secure?: boolean;
|
|
92
|
-
path?: string;
|
|
93
|
-
expires?: Date;
|
|
94
|
-
sameSite?: "lax" | "strict" | "none" | true;
|
|
95
|
-
}): ServerResponse {
|
|
96
|
-
return this.setHeader('Set-Cookie', COOKIE.serialize(name, String(value), options));
|
|
86
|
+
public setCookie(name: string, value: string, options?: CookieSerializeOptions): ServerResponse {
|
|
87
|
+
return this.setHeader('Set-Cookie', serialize(name, String(value), options));
|
|
97
88
|
}
|
|
98
89
|
public async end({ status, headers, body }: {
|
|
99
90
|
body?: any;
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { Handler, Context } from "../common";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { parse } from "cookie";
|
|
4
4
|
|
|
5
5
|
export const CookieParser = (options?: {
|
|
6
|
-
|
|
6
|
+
decode?(value: string): string;
|
|
7
7
|
}): Handler<void> => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
return async (ctx: Context): Promise<void> => {
|
|
9
|
+
const cookies = parse(ctx.headers.cookie || '', options);
|
|
10
|
+
const cookieList = Object.keys(cookies);
|
|
11
|
+
for (const name of cookieList) {
|
|
12
|
+
const value = cookies[name];
|
|
13
|
+
if (value) {
|
|
14
|
+
ctx.cookies[name] = value;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
18
|
}
|
|
19
19
|
|
package/src/util/index.ts
CHANGED
|
@@ -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
|
+
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* cookie
|
|
3
|
-
* Copyright(c) 2012-2014 Roman Shtylman
|
|
4
|
-
* Copyright(c) 2015 Douglas Christopher Wilson
|
|
5
|
-
* MIT Licensed
|
|
6
|
-
*/
|
|
7
|
-
/// <reference types="node" />
|
|
8
|
-
/**
|
|
9
|
-
* Module exports.
|
|
10
|
-
* @public
|
|
11
|
-
*/
|
|
12
|
-
/**
|
|
13
|
-
* Parse a cookie header.
|
|
14
|
-
*
|
|
15
|
-
* Parse the given cookie header string into an object
|
|
16
|
-
* The object has the various cookies as keys(names) => values
|
|
17
|
-
*
|
|
18
|
-
* @param {string} str
|
|
19
|
-
* @param {object} [options]
|
|
20
|
-
* @return {object}
|
|
21
|
-
* @public
|
|
22
|
-
*/
|
|
23
|
-
export declare const parse: (str: string, options?: {
|
|
24
|
-
decode?: ((value: string) => string) | undefined;
|
|
25
|
-
} | undefined) => NodeJS.Dict<string>;
|
|
26
|
-
/**
|
|
27
|
-
* Serialize data into a cookie header.
|
|
28
|
-
*
|
|
29
|
-
* Serialize the a name value pair into a cookie string suitable for
|
|
30
|
-
* http headers. An optional options object specified cookie parameters.
|
|
31
|
-
*
|
|
32
|
-
* serialize('foo', 'bar', { httpOnly: true })
|
|
33
|
-
* => "foo=bar; httpOnly"
|
|
34
|
-
*
|
|
35
|
-
* @param {string} name
|
|
36
|
-
* @param {string} val
|
|
37
|
-
* @param {object} [options]
|
|
38
|
-
* @return {string}
|
|
39
|
-
* @public
|
|
40
|
-
*/
|
|
41
|
-
export declare const serialize: (name: string, val: string, options?: {
|
|
42
|
-
encode?: ((uriComponent: string | number | boolean) => string) | undefined;
|
|
43
|
-
maxAge?: number | null | undefined;
|
|
44
|
-
domain?: string | undefined;
|
|
45
|
-
httpOnly?: boolean | undefined;
|
|
46
|
-
secure?: boolean | undefined;
|
|
47
|
-
path?: string | undefined;
|
|
48
|
-
expires?: Date | undefined;
|
|
49
|
-
sameSite?: true | "lax" | "strict" | "none" | undefined;
|
|
50
|
-
} | undefined) => string;
|