@grandlinex/kernel 0.27.0 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/dist/Kernel.d.ts +2 -2
- package/dist/actions/ApiAuthTestAction.d.ts +2 -2
- package/dist/actions/ApiVersionAction.d.ts +2 -2
- package/dist/actions/GetTokenAction.d.ts +2 -2
- package/dist/classes/BaseAction.d.ts +3 -3
- package/dist/classes/BaseAction.js +4 -1
- package/dist/classes/BaseAuthProvider.d.ts +5 -8
- package/dist/classes/BaseEndpoint.d.ts +6 -3
- package/dist/classes/BaseEndpoint.js +13 -1
- package/dist/classes/index.d.ts +2 -2
- package/dist/classes/index.js +15 -2
- package/dist/classes/timing/ExpressServerTiming.d.ts +3 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/lib/express.d.ts +6 -0
- package/dist/lib/express.js +2 -0
- package/dist/lib/index.d.ts +9 -8
- package/dist/modules/crypto/CryptoClient.d.ts +7 -5
- package/dist/modules/crypto/CryptoClient.js +40 -12
- package/dist/modules/crypto/utils/cors.d.ts +2 -2
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
All notable changes to this project will be documented in this file.
|
|
3
3
|
|
|
4
|
+
## [v0.27.1] - 2023-01-26
|
|
5
|
+
### Added
|
|
6
|
+
- Add raw body field for express calls
|
|
7
|
+
- change BaseEndpoint variables from private to protected
|
|
8
|
+
|
|
4
9
|
## [v0.25.2] - 2022-08-07
|
|
5
10
|
### Added
|
|
6
11
|
- Add express timing api in dev mode
|
package/dist/Kernel.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import CoreKernel, { CoreLogger } from '@grandlinex/core';
|
|
2
|
-
import { Request } from 'express';
|
|
3
2
|
import { ICClient, IKernel } from './lib';
|
|
3
|
+
import { XRequest } from './lib/express';
|
|
4
4
|
/**
|
|
5
5
|
* @class Kernel
|
|
6
6
|
*/
|
|
@@ -23,6 +23,6 @@ export default class Kernel extends CoreKernel<ICClient> implements IKernel {
|
|
|
23
23
|
setAppServerPort(port: number): void;
|
|
24
24
|
responseCodeFunction(data: {
|
|
25
25
|
code: number;
|
|
26
|
-
req:
|
|
26
|
+
req: XRequest;
|
|
27
27
|
}): void;
|
|
28
28
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import e from 'express';
|
|
2
1
|
import { IBaseKernelModule } from '../lib';
|
|
3
2
|
import { BaseApiAction, JwtToken } from '../classes';
|
|
3
|
+
import { XRequest, XResponse } from '../lib/express';
|
|
4
4
|
/**
|
|
5
5
|
* @name ApiAuthTestAction
|
|
6
6
|
*
|
|
@@ -23,5 +23,5 @@ import { BaseApiAction, JwtToken } from '../classes';
|
|
|
23
23
|
*/
|
|
24
24
|
export default class ApiAuthTestAction extends BaseApiAction {
|
|
25
25
|
constructor(module: IBaseKernelModule<any, any, any, any>);
|
|
26
|
-
handler(req:
|
|
26
|
+
handler(req: XRequest, res: XResponse, next: () => void, data: JwtToken): Promise<void>;
|
|
27
27
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import e from 'express';
|
|
2
1
|
import { IBaseKernelModule } from '../lib';
|
|
3
2
|
import { BaseApiAction } from '../classes';
|
|
4
3
|
import { IExtensionInterface } from '../classes/timing/ExpressServerTiming';
|
|
4
|
+
import { XRequest, XResponse } from '../lib/express';
|
|
5
5
|
/**
|
|
6
6
|
* @name ApiVersionAction
|
|
7
7
|
*
|
|
@@ -27,5 +27,5 @@ import { IExtensionInterface } from '../classes/timing/ExpressServerTiming';
|
|
|
27
27
|
*/
|
|
28
28
|
export default class ApiVersionAction extends BaseApiAction {
|
|
29
29
|
constructor(module: IBaseKernelModule);
|
|
30
|
-
handler(req:
|
|
30
|
+
handler(req: XRequest, res: XResponse, next: () => void, data: any, ex: IExtensionInterface): Promise<void>;
|
|
31
31
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Request, Response } from 'express';
|
|
2
1
|
import { IBaseKernelModule } from '../lib';
|
|
3
2
|
import { BaseApiAction } from '../classes';
|
|
4
3
|
import { IExtensionInterface } from '../classes/timing/ExpressServerTiming';
|
|
4
|
+
import { XRequest, XResponse } from '../lib/express';
|
|
5
5
|
/**
|
|
6
6
|
* @openapi
|
|
7
7
|
* /token:
|
|
@@ -40,5 +40,5 @@ export default class GetTokenAction extends BaseApiAction {
|
|
|
40
40
|
* @param module Parent Module
|
|
41
41
|
*/
|
|
42
42
|
constructor(module: IBaseKernelModule<any, any, any, any>);
|
|
43
|
-
handler(req:
|
|
43
|
+
handler(req: XRequest, res: XResponse, next: () => void, data: any, ex: IExtensionInterface): Promise<void>;
|
|
44
44
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Request, Response } from 'express';
|
|
2
1
|
import { CoreAction, IDataBase } from '@grandlinex/core';
|
|
3
2
|
import { IBaseAction, IBaseCache, IBaseClient, IBaseKernelModule, IBasePresenter, IKernel } from '../lib';
|
|
4
3
|
import { JwtToken } from './BaseAuthProvider';
|
|
5
4
|
import { IExtensionInterface } from './timing/ExpressServerTiming';
|
|
5
|
+
import { XNextFc, XRequest, XResponse } from '../lib/express';
|
|
6
6
|
export declare enum ActionMode {
|
|
7
7
|
'DEFAULT' = 0,
|
|
8
8
|
'DMZ' = 1,
|
|
@@ -11,8 +11,8 @@ export declare enum ActionMode {
|
|
|
11
11
|
export default abstract class BaseAction<K extends IKernel = IKernel, T extends IDataBase<any, any> | null = any, P extends IBaseClient | null = any, C extends IBaseCache | null = any, E extends IBasePresenter | null = any> extends CoreAction<K, T, P, C, E> implements IBaseAction<K, T, P, C, E> {
|
|
12
12
|
mode: ActionMode;
|
|
13
13
|
constructor(chanel: string, module: IBaseKernelModule<K, T, P, C, E>);
|
|
14
|
-
abstract handler(req:
|
|
15
|
-
secureHandler(req:
|
|
14
|
+
abstract handler(req: XRequest, res: XResponse, next: XNextFc, data: JwtToken | null, extension: IExtensionInterface): Promise<void>;
|
|
15
|
+
secureHandler(req: XRequest, res: XResponse, next: () => void): Promise<void>;
|
|
16
16
|
setMode(mode: ActionMode): void;
|
|
17
17
|
abstract register(): void;
|
|
18
18
|
private initExtension;
|
|
@@ -46,12 +46,15 @@ class BaseAction extends core_1.CoreAction {
|
|
|
46
46
|
}
|
|
47
47
|
const dat = yield cc.bearerTokenValidation(req);
|
|
48
48
|
auth.stop();
|
|
49
|
-
if (dat) {
|
|
49
|
+
if (dat && typeof dat !== 'number') {
|
|
50
50
|
yield this.handler(req, res, next, dat, extension);
|
|
51
51
|
}
|
|
52
52
|
else if (this.mode === ActionMode.DMZ_WITH_USER) {
|
|
53
53
|
yield this.handler(req, res, next, null, extension);
|
|
54
54
|
}
|
|
55
|
+
else if (dat) {
|
|
56
|
+
res.sendStatus(dat);
|
|
57
|
+
}
|
|
55
58
|
else {
|
|
56
59
|
res.status(401).send('no no no ...');
|
|
57
60
|
}
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { JwtPayload } from 'jsonwebtoken';
|
|
2
|
+
import { XRequest } from '../lib/express';
|
|
3
|
+
export interface JwtToken extends JwtPayload {
|
|
3
4
|
username: string;
|
|
4
5
|
userid: string;
|
|
5
6
|
}
|
|
6
|
-
export interface JwtToken extends JwtTokenData {
|
|
7
|
-
exp: number;
|
|
8
|
-
iat: number;
|
|
9
|
-
}
|
|
10
7
|
export type AuthResult = {
|
|
11
8
|
valid: boolean;
|
|
12
9
|
userId: string | null;
|
|
@@ -14,10 +11,10 @@ export type AuthResult = {
|
|
|
14
11
|
export interface IAuthProvider {
|
|
15
12
|
authorizeToken(userid: string, token: string, requestType: string): Promise<AuthResult>;
|
|
16
13
|
validateAccess(token: JwtToken, requestType: string): Promise<boolean>;
|
|
17
|
-
bearerTokenValidation(req:
|
|
14
|
+
bearerTokenValidation(req: XRequest): Promise<JwtToken | number>;
|
|
18
15
|
}
|
|
19
16
|
export default abstract class BaseAuthProvider implements IAuthProvider {
|
|
20
17
|
abstract authorizeToken(username: string, token: string, requestType: string): Promise<AuthResult>;
|
|
21
18
|
abstract validateAccess(token: JwtToken, requestType: string): Promise<boolean>;
|
|
22
|
-
abstract bearerTokenValidation(req:
|
|
19
|
+
abstract bearerTokenValidation(req: XRequest): Promise<JwtToken | number>;
|
|
23
20
|
}
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
2
3
|
import express from 'express';
|
|
3
4
|
import http from 'http';
|
|
4
5
|
import { CorePresenter, IDataBase } from '@grandlinex/core';
|
|
5
6
|
import { IBaseCache, IBaseClient, IBaseKernelModule, IBasePresenter, IKernel } from '../lib';
|
|
7
|
+
import { XRequest, XResponse } from '../lib/express';
|
|
8
|
+
export declare function keepRawBody(req: XRequest, res: XResponse, buf: Buffer, encoding: string): void;
|
|
6
9
|
export default abstract class BaseEndpoint<K extends IKernel = IKernel, T extends IDataBase<any, any> | null = any, P extends IBaseClient | null = any, C extends IBaseCache | null = any, E extends IBasePresenter | null = any> extends CorePresenter<express.Express, K, T, P, C, E> implements IBasePresenter {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
protected appServer: express.Express;
|
|
11
|
+
protected httpServer: http.Server;
|
|
12
|
+
protected port: number;
|
|
10
13
|
constructor(chanel: string, module: IBaseKernelModule<any, any, any, any>, port: number);
|
|
11
14
|
start(): Promise<boolean>;
|
|
12
15
|
stop(): Promise<boolean>;
|
|
@@ -3,16 +3,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.keepRawBody = void 0;
|
|
6
7
|
const express_1 = __importDefault(require("express"));
|
|
7
8
|
const http_1 = __importDefault(require("http"));
|
|
8
9
|
const body_parser_1 = require("body-parser");
|
|
9
10
|
const core_1 = require("@grandlinex/core");
|
|
11
|
+
function keepRawBody(req, res, buf, encoding) {
|
|
12
|
+
if (buf && buf.length) {
|
|
13
|
+
try {
|
|
14
|
+
req.rawBody = buf.toString(encoding || 'utf8');
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
req.rawBody = null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.keepRawBody = keepRawBody;
|
|
10
22
|
class BaseEndpoint extends core_1.CorePresenter {
|
|
11
23
|
constructor(chanel, module, port) {
|
|
12
24
|
super(`endpoint-${chanel}`, module);
|
|
13
25
|
this.port = port;
|
|
14
26
|
this.appServer = (0, express_1.default)();
|
|
15
|
-
this.appServer.use((0, body_parser_1.json)());
|
|
27
|
+
this.appServer.use((0, body_parser_1.json)({ verify: keepRawBody }));
|
|
16
28
|
this.httpServer = http_1.default.createServer(this.appServer);
|
|
17
29
|
}
|
|
18
30
|
start() {
|
package/dist/classes/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { CoreBridge as BaseBridge, CoreCache as BaseCache, CoreClient as BaseClient, CoreElement as BaseElement, CoreLoopService as BaseLoopService, CoreService as BaseService } from '@grandlinex/core';
|
|
2
2
|
import BaseAction from './BaseAction';
|
|
3
|
-
import BaseEndpoint from './BaseEndpoint';
|
|
3
|
+
import BaseEndpoint, { keepRawBody } from './BaseEndpoint';
|
|
4
4
|
import BaseKernelModule from './BaseKernelModule';
|
|
5
5
|
import BaseApiAction from './BaseApiAction';
|
|
6
6
|
import BaseAuthProvider from './BaseAuthProvider';
|
|
7
7
|
export * from './BaseAuthProvider';
|
|
8
8
|
export * from './timing';
|
|
9
|
-
export { BaseLoopService, BaseAuthProvider, BaseKernelModule, BaseService, BaseApiAction, BaseEndpoint, BaseElement, BaseCache, BaseAction, BaseClient, BaseBridge, };
|
|
9
|
+
export { BaseLoopService, BaseAuthProvider, BaseKernelModule, BaseService, BaseApiAction, BaseEndpoint, BaseElement, BaseCache, BaseAction, BaseClient, BaseBridge, keepRawBody, };
|
package/dist/classes/index.js
CHANGED
|
@@ -10,6 +10,18 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
13
25
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
26
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
27
|
};
|
|
@@ -17,7 +29,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
17
29
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
30
|
};
|
|
19
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.BaseBridge = exports.BaseClient = exports.BaseAction = exports.BaseCache = exports.BaseElement = exports.BaseEndpoint = exports.BaseApiAction = exports.BaseService = exports.BaseKernelModule = exports.BaseAuthProvider = exports.BaseLoopService = void 0;
|
|
32
|
+
exports.keepRawBody = exports.BaseBridge = exports.BaseClient = exports.BaseAction = exports.BaseCache = exports.BaseElement = exports.BaseEndpoint = exports.BaseApiAction = exports.BaseService = exports.BaseKernelModule = exports.BaseAuthProvider = exports.BaseLoopService = void 0;
|
|
21
33
|
const core_1 = require("@grandlinex/core");
|
|
22
34
|
Object.defineProperty(exports, "BaseBridge", { enumerable: true, get: function () { return core_1.CoreBridge; } });
|
|
23
35
|
Object.defineProperty(exports, "BaseCache", { enumerable: true, get: function () { return core_1.CoreCache; } });
|
|
@@ -27,8 +39,9 @@ Object.defineProperty(exports, "BaseLoopService", { enumerable: true, get: funct
|
|
|
27
39
|
Object.defineProperty(exports, "BaseService", { enumerable: true, get: function () { return core_1.CoreService; } });
|
|
28
40
|
const BaseAction_1 = __importDefault(require("./BaseAction"));
|
|
29
41
|
exports.BaseAction = BaseAction_1.default;
|
|
30
|
-
const BaseEndpoint_1 =
|
|
42
|
+
const BaseEndpoint_1 = __importStar(require("./BaseEndpoint"));
|
|
31
43
|
exports.BaseEndpoint = BaseEndpoint_1.default;
|
|
44
|
+
Object.defineProperty(exports, "keepRawBody", { enumerable: true, get: function () { return BaseEndpoint_1.keepRawBody; } });
|
|
32
45
|
const BaseKernelModule_1 = __importDefault(require("./BaseKernelModule"));
|
|
33
46
|
exports.BaseKernelModule = BaseKernelModule_1.default;
|
|
34
47
|
const BaseApiAction_1 = __importDefault(require("./BaseApiAction"));
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Response } from 'express';
|
|
2
1
|
import { CoreElement } from '@grandlinex/core';
|
|
2
|
+
import { XResponse } from '../../lib/express';
|
|
3
3
|
export type IExtensionInterface = {
|
|
4
4
|
done: () => void;
|
|
5
5
|
timing: ExpressServerTiming;
|
|
@@ -12,6 +12,6 @@ export default class ExpressServerTiming {
|
|
|
12
12
|
startFunc<T>(chanel: string, fc: () => Promise<T>): Promise<T>;
|
|
13
13
|
dbQuery<T>(fc: () => Promise<T>): Promise<T>;
|
|
14
14
|
getHeader(): string;
|
|
15
|
-
addHeader(res:
|
|
16
|
-
static init(baseApiAction: CoreElement<any>, res:
|
|
15
|
+
addHeader(res: XResponse): void;
|
|
16
|
+
static init(baseApiAction: CoreElement<any>, res: XResponse): [ExpressServerTiming, () => void];
|
|
17
17
|
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -31,5 +31,6 @@ __exportStar(require("./api"), exports);
|
|
|
31
31
|
__exportStar(require("./classes"), exports);
|
|
32
32
|
__exportStar(require("./modules/crypto"), exports);
|
|
33
33
|
__exportStar(require("./lib"), exports);
|
|
34
|
+
__exportStar(require("./lib/express"), exports);
|
|
34
35
|
__exportStar(require("@grandlinex/core"), exports);
|
|
35
36
|
exports.default = Kernel_1.default;
|
package/dist/lib/index.d.ts
CHANGED
|
@@ -1,27 +1,28 @@
|
|
|
1
|
-
import express, { Request, Response } from 'express';
|
|
2
1
|
import { ICoreAction, ICoreBridge, ICoreCache, ICoreCClient, ICoreClient, ICoreElement, ICoreKernel, ICoreKernelModule, ICorePresenter, ICoreService, IDataBase } from '@grandlinex/core';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import { JwtPayload } from 'jsonwebtoken';
|
|
3
4
|
import { IAuthProvider, JwtToken } from '../classes/BaseAuthProvider';
|
|
4
5
|
import { IExtensionInterface } from '../classes/timing/ExpressServerTiming';
|
|
6
|
+
import { XNextFc, XRequest, XResponse } from './express';
|
|
5
7
|
export type ActionTypes = 'POST' | 'GET' | 'USE' | 'PATCH' | 'DELETE';
|
|
6
8
|
export interface ICClient extends ICoreCClient {
|
|
7
9
|
setAuthProvider(provider: IAuthProvider): boolean;
|
|
8
|
-
jwtVerifyAccessToken(token: string): Promise<JwtToken |
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}): string;
|
|
10
|
+
jwtVerifyAccessToken(token: string): Promise<JwtToken | number>;
|
|
11
|
+
jwtDecodeAccessToken(token: string): JwtPayload | null;
|
|
12
|
+
jwtGenerateAccessToken(data: JwtToken, expire?: string | number): string;
|
|
12
13
|
apiTokenValidation(username: string, token: string, requestType: string): Promise<{
|
|
13
14
|
valid: boolean;
|
|
14
15
|
userId: string | null;
|
|
15
16
|
}>;
|
|
16
17
|
permissionValidation(token: JwtToken, requestType: string): Promise<boolean>;
|
|
17
|
-
bearerTokenValidation(req:
|
|
18
|
+
bearerTokenValidation(req: XRequest): Promise<JwtToken | number>;
|
|
18
19
|
}
|
|
19
20
|
export interface IKernel extends ICoreKernel<ICClient> {
|
|
20
21
|
getAppServerPort(): number;
|
|
21
22
|
setAppServerPort(port: number): void;
|
|
22
23
|
responseCodeFunction(data: {
|
|
23
24
|
code: number;
|
|
24
|
-
req:
|
|
25
|
+
req: XRequest;
|
|
25
26
|
}): void;
|
|
26
27
|
}
|
|
27
28
|
export type IBaseKernelModule<K extends IKernel = IKernel, T extends IDataBase<any, any> | null = any, P extends IBaseClient | null = any, C extends IBaseCache | null = any, E extends IBasePresenter | null = any> = ICoreKernelModule<K, T, P, C, E>;
|
|
@@ -32,5 +33,5 @@ export type IBaseBrige = ICoreBridge;
|
|
|
32
33
|
export type IBaseCache<K extends IKernel = IKernel, T extends IDataBase<any, any> | null = any, P extends IBaseClient | null = any, C extends IBaseCache | null = any, E extends IBasePresenter | null = any> = ICoreCache<K, T, P, C, E>;
|
|
33
34
|
export type IBaseElement<K extends IKernel = IKernel, T extends IDataBase<any, any> | null = any, P extends IBaseClient | null = any, C extends IBaseCache | null = any, E extends IBasePresenter | null = any> = ICoreElement<K, T, P, C, E>;
|
|
34
35
|
export interface IBaseAction<K extends IKernel = IKernel, T extends IDataBase<any, any> | null = any, P extends IBaseClient | null = any, C extends IBaseCache | null = any, E extends IBasePresenter | null = any> extends ICoreAction<K, T, P, C, E> {
|
|
35
|
-
handler(req:
|
|
36
|
+
handler(req: XRequest, res: XResponse, next: XNextFc, data: JwtToken | null, extension: IExtensionInterface): Promise<void>;
|
|
36
37
|
}
|
|
@@ -1,19 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { JwtPayload } from 'jsonwebtoken';
|
|
2
2
|
import { CoreCryptoClient } from '@grandlinex/core';
|
|
3
3
|
import { ICClient, IKernel } from '../../lib';
|
|
4
|
-
import { IAuthProvider, JwtToken
|
|
4
|
+
import { IAuthProvider, JwtToken } from '../../classes/BaseAuthProvider';
|
|
5
|
+
import { XRequest } from '../../lib/express';
|
|
5
6
|
export default class CryptoClient extends CoreCryptoClient implements ICClient {
|
|
6
7
|
protected authProvider: IAuthProvider | null;
|
|
7
8
|
protected kernel: IKernel;
|
|
8
9
|
protected expiresIn: string;
|
|
9
10
|
constructor(key: string, kernel: IKernel);
|
|
10
11
|
setAuthProvider(provider: IAuthProvider): boolean;
|
|
11
|
-
jwtVerifyAccessToken(token: string): Promise<JwtToken |
|
|
12
|
-
|
|
12
|
+
jwtVerifyAccessToken(token: string): Promise<JwtToken | number>;
|
|
13
|
+
jwtDecodeAccessToken(token: string): JwtPayload | null;
|
|
14
|
+
jwtGenerateAccessToken(data: JwtToken, expire?: string | number): string;
|
|
13
15
|
apiTokenValidation(username: string, token: string, requestType: string): Promise<{
|
|
14
16
|
valid: boolean;
|
|
15
17
|
userId: string | null;
|
|
16
18
|
}>;
|
|
17
19
|
permissionValidation(token: JwtToken, requestType: string): Promise<boolean>;
|
|
18
|
-
bearerTokenValidation(req:
|
|
20
|
+
bearerTokenValidation(req: XRequest): Promise<JwtToken | number>;
|
|
19
21
|
}
|
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
26
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
27
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -8,11 +31,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
31
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
32
|
});
|
|
10
33
|
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const jsonwebtoken_1 =
|
|
35
|
+
const jsonwebtoken_1 = __importStar(require("jsonwebtoken"));
|
|
16
36
|
const core_1 = require("@grandlinex/core");
|
|
17
37
|
class CryptoClient extends core_1.CoreCryptoClient {
|
|
18
38
|
constructor(key, kernel) {
|
|
@@ -31,8 +51,11 @@ class CryptoClient extends core_1.CoreCryptoClient {
|
|
|
31
51
|
jwtVerifyAccessToken(token) {
|
|
32
52
|
return new Promise((resolve) => {
|
|
33
53
|
jsonwebtoken_1.default.verify(token, this.AesKey, (err, user) => {
|
|
34
|
-
if (err
|
|
35
|
-
resolve(
|
|
54
|
+
if (err instanceof jsonwebtoken_1.TokenExpiredError) {
|
|
55
|
+
resolve(498);
|
|
56
|
+
}
|
|
57
|
+
else if (err || user === null) {
|
|
58
|
+
resolve(403);
|
|
36
59
|
}
|
|
37
60
|
else {
|
|
38
61
|
resolve(user);
|
|
@@ -40,8 +63,11 @@ class CryptoClient extends core_1.CoreCryptoClient {
|
|
|
40
63
|
});
|
|
41
64
|
});
|
|
42
65
|
}
|
|
43
|
-
|
|
44
|
-
return jsonwebtoken_1.default.
|
|
66
|
+
jwtDecodeAccessToken(token) {
|
|
67
|
+
return jsonwebtoken_1.default.decode(token, { json: true });
|
|
68
|
+
}
|
|
69
|
+
jwtGenerateAccessToken(data, expire) {
|
|
70
|
+
return jsonwebtoken_1.default.sign(data, this.AesKey, { expiresIn: expire !== null && expire !== void 0 ? expire : this.expiresIn });
|
|
45
71
|
}
|
|
46
72
|
apiTokenValidation(username, token, requestType) {
|
|
47
73
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -49,10 +75,12 @@ class CryptoClient extends core_1.CoreCryptoClient {
|
|
|
49
75
|
return this.authProvider.authorizeToken(username, token, requestType);
|
|
50
76
|
}
|
|
51
77
|
const store = this.kernel.getConfigStore();
|
|
78
|
+
const cc = this.kernel.getCryptoClient();
|
|
52
79
|
if (!store.has('SERVER_PASSWORD')) {
|
|
53
80
|
return { valid: false, userId: null };
|
|
54
81
|
}
|
|
55
|
-
if (
|
|
82
|
+
if ((cc === null || cc === void 0 ? void 0 : cc.timeSavePWValidation(token, store.get('SERVER_PASSWORD') || '')) ||
|
|
83
|
+
(token === store.get('SERVER_PASSWORD') && username === 'admin')) {
|
|
56
84
|
return {
|
|
57
85
|
valid: true,
|
|
58
86
|
userId: 'admin',
|
|
@@ -79,14 +107,14 @@ class CryptoClient extends core_1.CoreCryptoClient {
|
|
|
79
107
|
}
|
|
80
108
|
const authHeader = req.headers.authorization;
|
|
81
109
|
const token = authHeader && authHeader.split(' ')[1];
|
|
82
|
-
if (token
|
|
83
|
-
return
|
|
110
|
+
if (!token) {
|
|
111
|
+
return 401;
|
|
84
112
|
}
|
|
85
113
|
const tokenData = yield this.jwtVerifyAccessToken(token);
|
|
86
114
|
if (tokenData) {
|
|
87
115
|
return tokenData;
|
|
88
116
|
}
|
|
89
|
-
return
|
|
117
|
+
return 403;
|
|
90
118
|
});
|
|
91
119
|
}
|
|
92
120
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export type CorsMiddleWare = (req:
|
|
1
|
+
import { XNextFc, XRequest, XResponse } from '../../../lib/express';
|
|
2
|
+
export type CorsMiddleWare = (req: XRequest, res: XResponse, next: XNextFc) => void;
|
|
3
3
|
export declare const cors: CorsMiddleWare;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grandlinex/kernel",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.28.0",
|
|
4
4
|
"description": "GrandLineX is an out-of-the-box server framework on top of ExpressJs.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
},
|
|
28
28
|
"license": "BSD-3-Clause",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@grandlinex/core": "
|
|
31
|
-
"axios": "
|
|
32
|
-
"body-parser": "
|
|
33
|
-
"express": "
|
|
34
|
-
"jsonwebtoken": "
|
|
30
|
+
"@grandlinex/core": "0.28.0",
|
|
31
|
+
"axios": "0.27.2",
|
|
32
|
+
"body-parser": "1.20.1",
|
|
33
|
+
"express": "4.18.2",
|
|
34
|
+
"jsonwebtoken": "9.0.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/express": "^4.17.14",
|