@grandlinex/kernel 0.30.5 → 0.31.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 +6 -0
- package/dist/cjs/actions/GetTokenAction.d.ts +0 -32
- package/dist/cjs/actions/GetTokenAction.js +1 -33
- package/dist/cjs/classes/BaseAction.d.ts +1 -0
- package/dist/cjs/classes/BaseAction.js +2 -1
- package/dist/cjs/classes/BaseAuthProvider.d.ts +11 -8
- package/dist/cjs/classes/BaseAuthProvider.js +3 -0
- package/dist/cjs/classes/timing/ExpressServerTiming.js +2 -1
- package/dist/cjs/lib/index.d.ts +8 -8
- package/dist/cjs/modules/crypto/CryptoClient.d.ts +9 -10
- package/dist/cjs/modules/crypto/CryptoClient.js +11 -2
- package/dist/mjs/actions/GetTokenAction.d.ts +0 -32
- package/dist/mjs/actions/GetTokenAction.js +1 -33
- package/dist/mjs/classes/BaseAction.d.ts +1 -0
- package/dist/mjs/classes/BaseAction.js +2 -1
- package/dist/mjs/classes/BaseAuthProvider.d.ts +11 -8
- package/dist/mjs/classes/BaseAuthProvider.js +3 -0
- package/dist/mjs/classes/timing/ExpressServerTiming.js +2 -1
- package/dist/mjs/lib/index.d.ts +8 -8
- package/dist/mjs/modules/crypto/CryptoClient.d.ts +9 -10
- package/dist/mjs/modules/crypto/CryptoClient.js +11 -2
- package/package.json +18 -18
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
All notable changes to this project will be documented in this file.
|
|
3
3
|
|
|
4
|
+
## [v0.31.0] - 2023-07-06
|
|
5
|
+
### Added
|
|
6
|
+
- JTW Token now can be extended with custom payload
|
|
7
|
+
- Update to core 0.31.0
|
|
8
|
+
- Add forceDebug flag for BaseAction to force Server-Timing in production mode
|
|
9
|
+
|
|
4
10
|
## [v0.30.0] - 2023-04-03
|
|
5
11
|
### Added
|
|
6
12
|
- Switch to ESM
|
|
@@ -1,38 +1,6 @@
|
|
|
1
1
|
import { IBaseKernelModule } from '../lib/index.js';
|
|
2
2
|
import { BaseApiAction, IExtensionInterface } from '../classes/index.js';
|
|
3
3
|
import { XRequest, XResponse } from '../lib/express.js';
|
|
4
|
-
/**
|
|
5
|
-
* @openapi
|
|
6
|
-
* /token:
|
|
7
|
-
* post:
|
|
8
|
-
* summary: Get Bearer for user.
|
|
9
|
-
* tags:
|
|
10
|
-
* - Kernel
|
|
11
|
-
* description: Returns JWT.
|
|
12
|
-
* responses:
|
|
13
|
-
* '200':
|
|
14
|
-
* description: OK
|
|
15
|
-
* content:
|
|
16
|
-
* application/json:
|
|
17
|
-
* schema:
|
|
18
|
-
* type: object
|
|
19
|
-
* properties:
|
|
20
|
-
* token:
|
|
21
|
-
* type: string
|
|
22
|
-
* '403':
|
|
23
|
-
* description: Not Authorized
|
|
24
|
-
* requestBody:
|
|
25
|
-
* required: true
|
|
26
|
-
* content:
|
|
27
|
-
* application/json:
|
|
28
|
-
* schema:
|
|
29
|
-
* type: object
|
|
30
|
-
* properties:
|
|
31
|
-
* username:
|
|
32
|
-
* type: string
|
|
33
|
-
* token:
|
|
34
|
-
* type: string
|
|
35
|
-
*/
|
|
36
4
|
export default class GetTokenAction extends BaseApiAction {
|
|
37
5
|
/**
|
|
38
6
|
*
|
|
@@ -1,38 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const index_js_1 = require("../classes/index.js");
|
|
4
|
-
/**
|
|
5
|
-
* @openapi
|
|
6
|
-
* /token:
|
|
7
|
-
* post:
|
|
8
|
-
* summary: Get Bearer for user.
|
|
9
|
-
* tags:
|
|
10
|
-
* - Kernel
|
|
11
|
-
* description: Returns JWT.
|
|
12
|
-
* responses:
|
|
13
|
-
* '200':
|
|
14
|
-
* description: OK
|
|
15
|
-
* content:
|
|
16
|
-
* application/json:
|
|
17
|
-
* schema:
|
|
18
|
-
* type: object
|
|
19
|
-
* properties:
|
|
20
|
-
* token:
|
|
21
|
-
* type: string
|
|
22
|
-
* '403':
|
|
23
|
-
* description: Not Authorized
|
|
24
|
-
* requestBody:
|
|
25
|
-
* required: true
|
|
26
|
-
* content:
|
|
27
|
-
* application/json:
|
|
28
|
-
* schema:
|
|
29
|
-
* type: object
|
|
30
|
-
* properties:
|
|
31
|
-
* username:
|
|
32
|
-
* type: string
|
|
33
|
-
* token:
|
|
34
|
-
* type: string
|
|
35
|
-
*/
|
|
36
4
|
class GetTokenAction extends index_js_1.BaseApiAction {
|
|
37
5
|
/**
|
|
38
6
|
*
|
|
@@ -56,7 +24,7 @@ class GetTokenAction extends index_js_1.BaseApiAction {
|
|
|
56
24
|
const { username, token } = req.body;
|
|
57
25
|
const valid = await ex.timing.startFunc('validation', () => cc.apiTokenValidation(username, token, 'api'));
|
|
58
26
|
if (valid.valid && valid.userId) {
|
|
59
|
-
const jwt = cc.jwtGenerateAccessToken({
|
|
27
|
+
const jwt = await cc.jwtGenerateAccessToken({
|
|
60
28
|
userid: valid.userId,
|
|
61
29
|
username,
|
|
62
30
|
});
|
|
@@ -10,6 +10,7 @@ export declare enum ActionMode {
|
|
|
10
10
|
}
|
|
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
|
+
forceDebug: boolean;
|
|
13
14
|
constructor(chanel: string, module: IBaseKernelModule<K, T, P, C, E>);
|
|
14
15
|
abstract handler(req: XRequest, res: XResponse, next: XNextFc, data: JwtToken | null, extension: IExtensionInterface): Promise<void>;
|
|
15
16
|
secureHandler(req: XRequest, res: XResponse, next: () => void): Promise<void>;
|
|
@@ -8,12 +8,13 @@ var ActionMode;
|
|
|
8
8
|
ActionMode[ActionMode["DEFAULT"] = 0] = "DEFAULT";
|
|
9
9
|
ActionMode[ActionMode["DMZ"] = 1] = "DMZ";
|
|
10
10
|
ActionMode[ActionMode["DMZ_WITH_USER"] = 2] = "DMZ_WITH_USER";
|
|
11
|
-
})(ActionMode
|
|
11
|
+
})(ActionMode || (exports.ActionMode = ActionMode = {}));
|
|
12
12
|
class BaseAction extends core_1.CoreAction {
|
|
13
13
|
constructor(chanel, module) {
|
|
14
14
|
super(chanel, module);
|
|
15
15
|
this.secureHandler = this.secureHandler.bind(this);
|
|
16
16
|
this.mode = ActionMode.DEFAULT;
|
|
17
|
+
this.forceDebug = false;
|
|
17
18
|
}
|
|
18
19
|
async secureHandler(req, res, next) {
|
|
19
20
|
const extension = this.initExtension(res);
|
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
import * as jwt from 'jsonwebtoken';
|
|
2
2
|
import { XRequest } from '../lib/express.js';
|
|
3
|
-
export
|
|
3
|
+
export type JwtExtend = {
|
|
4
4
|
username: string;
|
|
5
5
|
userid: string;
|
|
6
|
-
}
|
|
6
|
+
} & Partial<Record<string, string | number | number[] | string[]>>;
|
|
7
|
+
export type JwtToken<T extends JwtExtend = JwtExtend> = T & jwt.JwtPayload;
|
|
7
8
|
export type AuthResult = {
|
|
8
9
|
valid: boolean;
|
|
9
10
|
userId: string | null;
|
|
10
11
|
};
|
|
11
|
-
export interface IAuthProvider {
|
|
12
|
+
export interface IAuthProvider<T extends JwtExtend> {
|
|
12
13
|
authorizeToken(userid: string, token: string, requestType: string): Promise<AuthResult>;
|
|
13
|
-
validateAccess(token: JwtToken
|
|
14
|
-
bearerTokenValidation(req: XRequest): Promise<JwtToken | number>;
|
|
14
|
+
validateAccess(token: JwtToken<T>, requestType: string): Promise<boolean>;
|
|
15
|
+
bearerTokenValidation(req: XRequest): Promise<JwtToken<T> | number>;
|
|
16
|
+
jwtAddData(token: JwtToken<T>): Promise<JwtToken<T>>;
|
|
15
17
|
}
|
|
16
|
-
export default abstract class BaseAuthProvider implements IAuthProvider {
|
|
18
|
+
export default abstract class BaseAuthProvider<T extends JwtExtend = JwtExtend> implements IAuthProvider<T> {
|
|
17
19
|
abstract authorizeToken(username: string, token: string, requestType: string): Promise<AuthResult>;
|
|
18
|
-
abstract validateAccess(token: JwtToken
|
|
19
|
-
abstract bearerTokenValidation(req: XRequest): Promise<JwtToken | number>;
|
|
20
|
+
abstract validateAccess(token: JwtToken<T>, requestType: string): Promise<boolean>;
|
|
21
|
+
abstract bearerTokenValidation(req: XRequest): Promise<JwtToken<T> | number>;
|
|
22
|
+
jwtAddData(token: JwtToken<T>): Promise<JwtToken<T>>;
|
|
20
23
|
}
|
|
@@ -31,7 +31,8 @@ class ExpressServerTiming {
|
|
|
31
31
|
}
|
|
32
32
|
addHeader(res) {
|
|
33
33
|
if (this.timing.map.size > 0 &&
|
|
34
|
-
this.baseApiAction.getKernel().getDevMode()
|
|
34
|
+
(this.baseApiAction.getKernel().getDevMode() ||
|
|
35
|
+
this.baseApiAction.forceDebug)) {
|
|
35
36
|
const a = ['Server-Timing', this.getHeader()];
|
|
36
37
|
res.setHeader(a[0], a[1]);
|
|
37
38
|
this.baseApiAction.debug(a);
|
package/dist/cjs/lib/index.d.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { ICoreAction, ICoreBridge, ICoreCache, ICoreCClient, ICoreClient, ICoreElement, ICoreKernel, ICoreKernelModule, ICorePresenter, ICoreService, IDataBase } from '@grandlinex/core';
|
|
2
2
|
import express from 'express';
|
|
3
3
|
import * as jwt from 'jsonwebtoken';
|
|
4
|
-
import { IAuthProvider, JwtToken, IExtensionInterface } from '../classes/index.js';
|
|
4
|
+
import { IAuthProvider, JwtToken, IExtensionInterface, JwtExtend } from '../classes/index.js';
|
|
5
5
|
import { XNextFc, XRequest, XResponse } from './express.js';
|
|
6
6
|
export type ActionTypes = 'POST' | 'GET' | 'USE' | 'PATCH' | 'DELETE';
|
|
7
|
-
export interface ICClient extends ICoreCClient {
|
|
8
|
-
setAuthProvider(provider: IAuthProvider): boolean;
|
|
9
|
-
jwtVerifyAccessToken(token: string): Promise<JwtToken | number>;
|
|
7
|
+
export interface ICClient<T extends JwtExtend = JwtExtend> extends ICoreCClient {
|
|
8
|
+
setAuthProvider(provider: IAuthProvider<T>): boolean;
|
|
9
|
+
jwtVerifyAccessToken(token: string): Promise<JwtToken<T> | number>;
|
|
10
10
|
jwtDecodeAccessToken(token: string): jwt.JwtPayload | null;
|
|
11
|
-
jwtGenerateAccessToken(data: JwtToken
|
|
11
|
+
jwtGenerateAccessToken(data: JwtToken<T>, expire?: string | number): Promise<string>;
|
|
12
12
|
apiTokenValidation(username: string, token: string, requestType: string): Promise<{
|
|
13
13
|
valid: boolean;
|
|
14
14
|
userId: string | null;
|
|
15
15
|
}>;
|
|
16
|
-
permissionValidation(token: JwtToken
|
|
17
|
-
bearerTokenValidation(req: XRequest): Promise<JwtToken | number>;
|
|
16
|
+
permissionValidation(token: JwtToken<T>, requestType: string): Promise<boolean>;
|
|
17
|
+
bearerTokenValidation(req: XRequest): Promise<JwtToken<T> | number>;
|
|
18
18
|
}
|
|
19
|
-
export interface IKernel extends ICoreKernel<ICClient
|
|
19
|
+
export interface IKernel<T extends JwtExtend = JwtExtend> extends ICoreKernel<ICClient<T>> {
|
|
20
20
|
getAppServerPort(): number;
|
|
21
21
|
setAppServerPort(port: number): void;
|
|
22
22
|
responseCodeFunction(data: {
|
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import { CoreCryptoClient } from '@grandlinex/core';
|
|
2
|
-
import * as jwt from 'jsonwebtoken';
|
|
3
2
|
import { ICClient, IKernel } from '../../lib/index.js';
|
|
4
|
-
import { IAuthProvider, JwtToken } from '../../classes/index.js';
|
|
3
|
+
import { IAuthProvider, JwtExtend, JwtToken } from '../../classes/index.js';
|
|
5
4
|
import { XRequest } from '../../lib/express.js';
|
|
6
|
-
export default class CryptoClient extends CoreCryptoClient implements ICClient {
|
|
7
|
-
protected authProvider: IAuthProvider | null;
|
|
5
|
+
export default class CryptoClient<T extends JwtExtend = JwtExtend> extends CoreCryptoClient implements ICClient<T> {
|
|
6
|
+
protected authProvider: IAuthProvider<T> | null;
|
|
8
7
|
protected kernel: IKernel;
|
|
9
8
|
protected expiresIn: string;
|
|
10
9
|
constructor(key: string, kernel: IKernel);
|
|
11
|
-
setAuthProvider(provider: IAuthProvider): boolean;
|
|
12
|
-
jwtVerifyAccessToken(token: string): Promise<JwtToken | number>;
|
|
13
|
-
jwtDecodeAccessToken(token: string):
|
|
14
|
-
jwtGenerateAccessToken(data: JwtToken
|
|
10
|
+
setAuthProvider(provider: IAuthProvider<T>): boolean;
|
|
11
|
+
jwtVerifyAccessToken(token: string): Promise<JwtToken<T> | number>;
|
|
12
|
+
jwtDecodeAccessToken(token: string): JwtToken<T> | null;
|
|
13
|
+
jwtGenerateAccessToken(data: JwtToken<T>, expire?: string | number): Promise<string>;
|
|
15
14
|
apiTokenValidation(username: string, token: string, requestType: string): Promise<{
|
|
16
15
|
valid: boolean;
|
|
17
16
|
userId: string | null;
|
|
18
17
|
}>;
|
|
19
|
-
permissionValidation(token: JwtToken
|
|
20
|
-
bearerTokenValidation(req: XRequest): Promise<JwtToken | number>;
|
|
18
|
+
permissionValidation(token: JwtToken<T>, requestType: string): Promise<boolean>;
|
|
19
|
+
bearerTokenValidation(req: XRequest): Promise<JwtToken<T> | number>;
|
|
21
20
|
}
|
|
@@ -59,8 +59,17 @@ class CryptoClient extends core_1.CoreCryptoClient {
|
|
|
59
59
|
const mod = jwt.default || jwt;
|
|
60
60
|
return mod.decode(token, { json: true });
|
|
61
61
|
}
|
|
62
|
-
jwtGenerateAccessToken(data, expire) {
|
|
63
|
-
|
|
62
|
+
async jwtGenerateAccessToken(data, expire) {
|
|
63
|
+
let sData;
|
|
64
|
+
if (this.authProvider) {
|
|
65
|
+
sData = await this.authProvider.jwtAddData(data);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
sData = data;
|
|
69
|
+
}
|
|
70
|
+
return jwt.sign(sData, this.AesKey, {
|
|
71
|
+
expiresIn: expire ?? this.expiresIn,
|
|
72
|
+
});
|
|
64
73
|
}
|
|
65
74
|
async apiTokenValidation(username, token, requestType) {
|
|
66
75
|
if (this.authProvider) {
|
|
@@ -1,38 +1,6 @@
|
|
|
1
1
|
import { IBaseKernelModule } from '../lib/index.js';
|
|
2
2
|
import { BaseApiAction, IExtensionInterface } from '../classes/index.js';
|
|
3
3
|
import { XRequest, XResponse } from '../lib/express.js';
|
|
4
|
-
/**
|
|
5
|
-
* @openapi
|
|
6
|
-
* /token:
|
|
7
|
-
* post:
|
|
8
|
-
* summary: Get Bearer for user.
|
|
9
|
-
* tags:
|
|
10
|
-
* - Kernel
|
|
11
|
-
* description: Returns JWT.
|
|
12
|
-
* responses:
|
|
13
|
-
* '200':
|
|
14
|
-
* description: OK
|
|
15
|
-
* content:
|
|
16
|
-
* application/json:
|
|
17
|
-
* schema:
|
|
18
|
-
* type: object
|
|
19
|
-
* properties:
|
|
20
|
-
* token:
|
|
21
|
-
* type: string
|
|
22
|
-
* '403':
|
|
23
|
-
* description: Not Authorized
|
|
24
|
-
* requestBody:
|
|
25
|
-
* required: true
|
|
26
|
-
* content:
|
|
27
|
-
* application/json:
|
|
28
|
-
* schema:
|
|
29
|
-
* type: object
|
|
30
|
-
* properties:
|
|
31
|
-
* username:
|
|
32
|
-
* type: string
|
|
33
|
-
* token:
|
|
34
|
-
* type: string
|
|
35
|
-
*/
|
|
36
4
|
export default class GetTokenAction extends BaseApiAction {
|
|
37
5
|
/**
|
|
38
6
|
*
|
|
@@ -1,36 +1,4 @@
|
|
|
1
1
|
import { BaseApiAction, ActionMode, } from '../classes/index.js';
|
|
2
|
-
/**
|
|
3
|
-
* @openapi
|
|
4
|
-
* /token:
|
|
5
|
-
* post:
|
|
6
|
-
* summary: Get Bearer for user.
|
|
7
|
-
* tags:
|
|
8
|
-
* - Kernel
|
|
9
|
-
* description: Returns JWT.
|
|
10
|
-
* responses:
|
|
11
|
-
* '200':
|
|
12
|
-
* description: OK
|
|
13
|
-
* content:
|
|
14
|
-
* application/json:
|
|
15
|
-
* schema:
|
|
16
|
-
* type: object
|
|
17
|
-
* properties:
|
|
18
|
-
* token:
|
|
19
|
-
* type: string
|
|
20
|
-
* '403':
|
|
21
|
-
* description: Not Authorized
|
|
22
|
-
* requestBody:
|
|
23
|
-
* required: true
|
|
24
|
-
* content:
|
|
25
|
-
* application/json:
|
|
26
|
-
* schema:
|
|
27
|
-
* type: object
|
|
28
|
-
* properties:
|
|
29
|
-
* username:
|
|
30
|
-
* type: string
|
|
31
|
-
* token:
|
|
32
|
-
* type: string
|
|
33
|
-
*/
|
|
34
2
|
export default class GetTokenAction extends BaseApiAction {
|
|
35
3
|
/**
|
|
36
4
|
*
|
|
@@ -54,7 +22,7 @@ export default class GetTokenAction extends BaseApiAction {
|
|
|
54
22
|
const { username, token } = req.body;
|
|
55
23
|
const valid = await ex.timing.startFunc('validation', () => cc.apiTokenValidation(username, token, 'api'));
|
|
56
24
|
if (valid.valid && valid.userId) {
|
|
57
|
-
const jwt = cc.jwtGenerateAccessToken({
|
|
25
|
+
const jwt = await cc.jwtGenerateAccessToken({
|
|
58
26
|
userid: valid.userId,
|
|
59
27
|
username,
|
|
60
28
|
});
|
|
@@ -10,6 +10,7 @@ export declare enum ActionMode {
|
|
|
10
10
|
}
|
|
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
|
+
forceDebug: boolean;
|
|
13
14
|
constructor(chanel: string, module: IBaseKernelModule<K, T, P, C, E>);
|
|
14
15
|
abstract handler(req: XRequest, res: XResponse, next: XNextFc, data: JwtToken | null, extension: IExtensionInterface): Promise<void>;
|
|
15
16
|
secureHandler(req: XRequest, res: XResponse, next: () => void): Promise<void>;
|
|
@@ -5,12 +5,13 @@ export var ActionMode;
|
|
|
5
5
|
ActionMode[ActionMode["DEFAULT"] = 0] = "DEFAULT";
|
|
6
6
|
ActionMode[ActionMode["DMZ"] = 1] = "DMZ";
|
|
7
7
|
ActionMode[ActionMode["DMZ_WITH_USER"] = 2] = "DMZ_WITH_USER";
|
|
8
|
-
})(ActionMode
|
|
8
|
+
})(ActionMode || (ActionMode = {}));
|
|
9
9
|
export default class BaseAction extends CoreAction {
|
|
10
10
|
constructor(chanel, module) {
|
|
11
11
|
super(chanel, module);
|
|
12
12
|
this.secureHandler = this.secureHandler.bind(this);
|
|
13
13
|
this.mode = ActionMode.DEFAULT;
|
|
14
|
+
this.forceDebug = false;
|
|
14
15
|
}
|
|
15
16
|
async secureHandler(req, res, next) {
|
|
16
17
|
const extension = this.initExtension(res);
|
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
import * as jwt from 'jsonwebtoken';
|
|
2
2
|
import { XRequest } from '../lib/express.js';
|
|
3
|
-
export
|
|
3
|
+
export type JwtExtend = {
|
|
4
4
|
username: string;
|
|
5
5
|
userid: string;
|
|
6
|
-
}
|
|
6
|
+
} & Partial<Record<string, string | number | number[] | string[]>>;
|
|
7
|
+
export type JwtToken<T extends JwtExtend = JwtExtend> = T & jwt.JwtPayload;
|
|
7
8
|
export type AuthResult = {
|
|
8
9
|
valid: boolean;
|
|
9
10
|
userId: string | null;
|
|
10
11
|
};
|
|
11
|
-
export interface IAuthProvider {
|
|
12
|
+
export interface IAuthProvider<T extends JwtExtend> {
|
|
12
13
|
authorizeToken(userid: string, token: string, requestType: string): Promise<AuthResult>;
|
|
13
|
-
validateAccess(token: JwtToken
|
|
14
|
-
bearerTokenValidation(req: XRequest): Promise<JwtToken | number>;
|
|
14
|
+
validateAccess(token: JwtToken<T>, requestType: string): Promise<boolean>;
|
|
15
|
+
bearerTokenValidation(req: XRequest): Promise<JwtToken<T> | number>;
|
|
16
|
+
jwtAddData(token: JwtToken<T>): Promise<JwtToken<T>>;
|
|
15
17
|
}
|
|
16
|
-
export default abstract class BaseAuthProvider implements IAuthProvider {
|
|
18
|
+
export default abstract class BaseAuthProvider<T extends JwtExtend = JwtExtend> implements IAuthProvider<T> {
|
|
17
19
|
abstract authorizeToken(username: string, token: string, requestType: string): Promise<AuthResult>;
|
|
18
|
-
abstract validateAccess(token: JwtToken
|
|
19
|
-
abstract bearerTokenValidation(req: XRequest): Promise<JwtToken | number>;
|
|
20
|
+
abstract validateAccess(token: JwtToken<T>, requestType: string): Promise<boolean>;
|
|
21
|
+
abstract bearerTokenValidation(req: XRequest): Promise<JwtToken<T> | number>;
|
|
22
|
+
jwtAddData(token: JwtToken<T>): Promise<JwtToken<T>>;
|
|
20
23
|
}
|
|
@@ -26,7 +26,8 @@ export default class ExpressServerTiming {
|
|
|
26
26
|
}
|
|
27
27
|
addHeader(res) {
|
|
28
28
|
if (this.timing.map.size > 0 &&
|
|
29
|
-
this.baseApiAction.getKernel().getDevMode()
|
|
29
|
+
(this.baseApiAction.getKernel().getDevMode() ||
|
|
30
|
+
this.baseApiAction.forceDebug)) {
|
|
30
31
|
const a = ['Server-Timing', this.getHeader()];
|
|
31
32
|
res.setHeader(a[0], a[1]);
|
|
32
33
|
this.baseApiAction.debug(a);
|
package/dist/mjs/lib/index.d.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { ICoreAction, ICoreBridge, ICoreCache, ICoreCClient, ICoreClient, ICoreElement, ICoreKernel, ICoreKernelModule, ICorePresenter, ICoreService, IDataBase } from '@grandlinex/core';
|
|
2
2
|
import express from 'express';
|
|
3
3
|
import * as jwt from 'jsonwebtoken';
|
|
4
|
-
import { IAuthProvider, JwtToken, IExtensionInterface } from '../classes/index.js';
|
|
4
|
+
import { IAuthProvider, JwtToken, IExtensionInterface, JwtExtend } from '../classes/index.js';
|
|
5
5
|
import { XNextFc, XRequest, XResponse } from './express.js';
|
|
6
6
|
export type ActionTypes = 'POST' | 'GET' | 'USE' | 'PATCH' | 'DELETE';
|
|
7
|
-
export interface ICClient extends ICoreCClient {
|
|
8
|
-
setAuthProvider(provider: IAuthProvider): boolean;
|
|
9
|
-
jwtVerifyAccessToken(token: string): Promise<JwtToken | number>;
|
|
7
|
+
export interface ICClient<T extends JwtExtend = JwtExtend> extends ICoreCClient {
|
|
8
|
+
setAuthProvider(provider: IAuthProvider<T>): boolean;
|
|
9
|
+
jwtVerifyAccessToken(token: string): Promise<JwtToken<T> | number>;
|
|
10
10
|
jwtDecodeAccessToken(token: string): jwt.JwtPayload | null;
|
|
11
|
-
jwtGenerateAccessToken(data: JwtToken
|
|
11
|
+
jwtGenerateAccessToken(data: JwtToken<T>, expire?: string | number): Promise<string>;
|
|
12
12
|
apiTokenValidation(username: string, token: string, requestType: string): Promise<{
|
|
13
13
|
valid: boolean;
|
|
14
14
|
userId: string | null;
|
|
15
15
|
}>;
|
|
16
|
-
permissionValidation(token: JwtToken
|
|
17
|
-
bearerTokenValidation(req: XRequest): Promise<JwtToken | number>;
|
|
16
|
+
permissionValidation(token: JwtToken<T>, requestType: string): Promise<boolean>;
|
|
17
|
+
bearerTokenValidation(req: XRequest): Promise<JwtToken<T> | number>;
|
|
18
18
|
}
|
|
19
|
-
export interface IKernel extends ICoreKernel<ICClient
|
|
19
|
+
export interface IKernel<T extends JwtExtend = JwtExtend> extends ICoreKernel<ICClient<T>> {
|
|
20
20
|
getAppServerPort(): number;
|
|
21
21
|
setAppServerPort(port: number): void;
|
|
22
22
|
responseCodeFunction(data: {
|
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import { CoreCryptoClient } from '@grandlinex/core';
|
|
2
|
-
import * as jwt from 'jsonwebtoken';
|
|
3
2
|
import { ICClient, IKernel } from '../../lib/index.js';
|
|
4
|
-
import { IAuthProvider, JwtToken } from '../../classes/index.js';
|
|
3
|
+
import { IAuthProvider, JwtExtend, JwtToken } from '../../classes/index.js';
|
|
5
4
|
import { XRequest } from '../../lib/express.js';
|
|
6
|
-
export default class CryptoClient extends CoreCryptoClient implements ICClient {
|
|
7
|
-
protected authProvider: IAuthProvider | null;
|
|
5
|
+
export default class CryptoClient<T extends JwtExtend = JwtExtend> extends CoreCryptoClient implements ICClient<T> {
|
|
6
|
+
protected authProvider: IAuthProvider<T> | null;
|
|
8
7
|
protected kernel: IKernel;
|
|
9
8
|
protected expiresIn: string;
|
|
10
9
|
constructor(key: string, kernel: IKernel);
|
|
11
|
-
setAuthProvider(provider: IAuthProvider): boolean;
|
|
12
|
-
jwtVerifyAccessToken(token: string): Promise<JwtToken | number>;
|
|
13
|
-
jwtDecodeAccessToken(token: string):
|
|
14
|
-
jwtGenerateAccessToken(data: JwtToken
|
|
10
|
+
setAuthProvider(provider: IAuthProvider<T>): boolean;
|
|
11
|
+
jwtVerifyAccessToken(token: string): Promise<JwtToken<T> | number>;
|
|
12
|
+
jwtDecodeAccessToken(token: string): JwtToken<T> | null;
|
|
13
|
+
jwtGenerateAccessToken(data: JwtToken<T>, expire?: string | number): Promise<string>;
|
|
15
14
|
apiTokenValidation(username: string, token: string, requestType: string): Promise<{
|
|
16
15
|
valid: boolean;
|
|
17
16
|
userId: string | null;
|
|
18
17
|
}>;
|
|
19
|
-
permissionValidation(token: JwtToken
|
|
20
|
-
bearerTokenValidation(req: XRequest): Promise<JwtToken | number>;
|
|
18
|
+
permissionValidation(token: JwtToken<T>, requestType: string): Promise<boolean>;
|
|
19
|
+
bearerTokenValidation(req: XRequest): Promise<JwtToken<T> | number>;
|
|
21
20
|
}
|
|
@@ -34,8 +34,17 @@ export default class CryptoClient extends CoreCryptoClient {
|
|
|
34
34
|
const mod = jwt.default || jwt;
|
|
35
35
|
return mod.decode(token, { json: true });
|
|
36
36
|
}
|
|
37
|
-
jwtGenerateAccessToken(data, expire) {
|
|
38
|
-
|
|
37
|
+
async jwtGenerateAccessToken(data, expire) {
|
|
38
|
+
let sData;
|
|
39
|
+
if (this.authProvider) {
|
|
40
|
+
sData = await this.authProvider.jwtAddData(data);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
sData = data;
|
|
44
|
+
}
|
|
45
|
+
return jwt.sign(sData, this.AesKey, {
|
|
46
|
+
expiresIn: expire ?? this.expiresIn,
|
|
47
|
+
});
|
|
39
48
|
}
|
|
40
49
|
async apiTokenValidation(username, token, requestType) {
|
|
41
50
|
if (this.authProvider) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grandlinex/kernel",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
4
4
|
"description": "GrandLineX is an out-of-the-box server framework on top of ExpressJs.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -46,36 +46,36 @@
|
|
|
46
46
|
},
|
|
47
47
|
"license": "BSD-3-Clause",
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@grandlinex/core": "0.
|
|
50
|
-
"axios": "1.
|
|
49
|
+
"@grandlinex/core": "0.31.0",
|
|
50
|
+
"axios": "1.4.0",
|
|
51
51
|
"body-parser": "1.20.2",
|
|
52
52
|
"express": "4.18.2",
|
|
53
|
-
"jsonwebtoken": "9.0.
|
|
53
|
+
"jsonwebtoken": "9.0.1"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@types/express": "^4.17.17",
|
|
57
|
-
"@types/jest": "^29.5.
|
|
58
|
-
"@types/jsonwebtoken": "^9.0.
|
|
59
|
-
"@types/node": "^
|
|
60
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
61
|
-
"@typescript-eslint/parser": "^5.
|
|
57
|
+
"@types/jest": "^29.5.2",
|
|
58
|
+
"@types/jsonwebtoken": "^9.0.2",
|
|
59
|
+
"@types/node": "^20.4.0",
|
|
60
|
+
"@typescript-eslint/eslint-plugin": "^5.61.0",
|
|
61
|
+
"@typescript-eslint/parser": "^5.61.0",
|
|
62
62
|
"cross-env": "^7.0.3",
|
|
63
|
-
"eslint": "^8.
|
|
63
|
+
"eslint": "^8.44.0",
|
|
64
64
|
"eslint-config-airbnb": "^19.0.4",
|
|
65
65
|
"eslint-config-airbnb-typescript": "^17.0.0",
|
|
66
66
|
"eslint-config-prettier": "^8.8.0",
|
|
67
67
|
"eslint-plugin-import": "^2.27.5",
|
|
68
|
-
"eslint-plugin-jest": "^27.2.
|
|
68
|
+
"eslint-plugin-jest": "^27.2.2",
|
|
69
69
|
"eslint-plugin-jsx-a11y": "^6.7.1",
|
|
70
70
|
"eslint-plugin-prettier": "^4.2.1",
|
|
71
|
-
"jest": "^29.
|
|
72
|
-
"jest-junit": "^
|
|
73
|
-
"prettier": "
|
|
74
|
-
"ts-jest": "^29.1.
|
|
75
|
-
"ts-loader": "^9.4.
|
|
71
|
+
"jest": "^29.6.1",
|
|
72
|
+
"jest-junit": "^16.0.0",
|
|
73
|
+
"prettier": "2.8.8",
|
|
74
|
+
"ts-jest": "^29.1.1",
|
|
75
|
+
"ts-loader": "^9.4.4",
|
|
76
76
|
"ts-node": "^10.9.1",
|
|
77
|
-
"typedoc": "^0.
|
|
78
|
-
"typescript": "^5.
|
|
77
|
+
"typedoc": "^0.24.8",
|
|
78
|
+
"typescript": "^5.1.6"
|
|
79
79
|
},
|
|
80
80
|
"repository": {
|
|
81
81
|
"type": "git",
|