@grandlinex/kernel 0.31.0 → 0.31.1

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 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.1] - 2023-07-07
5
+ ### Added
6
+ - @/token pass the token body to jwt generator function
7
+ ### Fixed
8
+ - Deal with code smells
9
+
4
10
  ## [v0.31.0] - 2023-07-06
5
11
  ### Added
6
12
  - JTW Token now can be extended with custom payload
@@ -27,7 +27,7 @@ class GetTokenAction extends index_js_1.BaseApiAction {
27
27
  const jwt = await cc.jwtGenerateAccessToken({
28
28
  userid: valid.userId,
29
29
  username,
30
- });
30
+ }, req.body);
31
31
  ex.done();
32
32
  res.status(200).send({ token: jwt });
33
33
  }
@@ -13,11 +13,11 @@ export interface IAuthProvider<T extends JwtExtend> {
13
13
  authorizeToken(userid: string, token: string, requestType: string): Promise<AuthResult>;
14
14
  validateAccess(token: JwtToken<T>, requestType: string): Promise<boolean>;
15
15
  bearerTokenValidation(req: XRequest): Promise<JwtToken<T> | number>;
16
- jwtAddData(token: JwtToken<T>): Promise<JwtToken<T>>;
16
+ jwtAddData(token: JwtToken<T>, extend?: Record<string, any>): Promise<JwtToken<T>>;
17
17
  }
18
18
  export default abstract class BaseAuthProvider<T extends JwtExtend = JwtExtend> implements IAuthProvider<T> {
19
19
  abstract authorizeToken(username: string, token: string, requestType: string): Promise<AuthResult>;
20
20
  abstract validateAccess(token: JwtToken<T>, requestType: string): Promise<boolean>;
21
21
  abstract bearerTokenValidation(req: XRequest): Promise<JwtToken<T> | number>;
22
- jwtAddData(token: JwtToken<T>): Promise<JwtToken<T>>;
22
+ jwtAddData(token: JwtToken<T>, extend?: Record<string, any>): Promise<JwtToken<T>>;
23
23
  }
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class BaseAuthProvider {
4
- async jwtAddData(token) {
4
+ async jwtAddData(token, extend) {
5
5
  return token;
6
6
  }
7
7
  }
@@ -18,7 +18,7 @@ class ServerTiming {
18
18
  return content;
19
19
  }
20
20
  completeElement(e) {
21
- const cur = this.map.get(e.chanel) || [];
21
+ const cur = this.map.get(e.chanel) ?? [];
22
22
  cur.push(e);
23
23
  this.map.set(e.chanel, cur);
24
24
  }
@@ -1,6 +1,6 @@
1
1
  import express from 'express';
2
2
  export type XRequest = express.Request & {
3
- rawBody?: string | null | undefined;
3
+ rawBody?: string | null;
4
4
  };
5
5
  export type XResponse = express.Response;
6
6
  export type XNextFc = express.NextFunction;
@@ -8,7 +8,7 @@ export interface ICClient<T extends JwtExtend = JwtExtend> extends ICoreCClient
8
8
  setAuthProvider(provider: IAuthProvider<T>): boolean;
9
9
  jwtVerifyAccessToken(token: string): Promise<JwtToken<T> | number>;
10
10
  jwtDecodeAccessToken(token: string): jwt.JwtPayload | null;
11
- jwtGenerateAccessToken(data: JwtToken<T>, expire?: string | number): Promise<string>;
11
+ jwtGenerateAccessToken(data: JwtToken<T>, extend?: Record<string, any>, expire?: string | number): Promise<string>;
12
12
  apiTokenValidation(username: string, token: string, requestType: string): Promise<{
13
13
  valid: boolean;
14
14
  userId: string | null;
@@ -10,7 +10,7 @@ export default class CryptoClient<T extends JwtExtend = JwtExtend> extends CoreC
10
10
  setAuthProvider(provider: IAuthProvider<T>): boolean;
11
11
  jwtVerifyAccessToken(token: string): Promise<JwtToken<T> | number>;
12
12
  jwtDecodeAccessToken(token: string): JwtToken<T> | null;
13
- jwtGenerateAccessToken(data: JwtToken<T>, expire?: string | number): Promise<string>;
13
+ jwtGenerateAccessToken(data: JwtToken<T>, extend?: Record<string, any>, expire?: string | number): Promise<string>;
14
14
  apiTokenValidation(username: string, token: string, requestType: string): Promise<{
15
15
  valid: boolean;
16
16
  userId: string | null;
@@ -59,10 +59,10 @@ class CryptoClient extends core_1.CoreCryptoClient {
59
59
  const mod = jwt.default || jwt;
60
60
  return mod.decode(token, { json: true });
61
61
  }
62
- async jwtGenerateAccessToken(data, expire) {
62
+ async jwtGenerateAccessToken(data, extend, expire) {
63
63
  let sData;
64
64
  if (this.authProvider) {
65
- sData = await this.authProvider.jwtAddData(data);
65
+ sData = await this.authProvider.jwtAddData(data, extend);
66
66
  }
67
67
  else {
68
68
  sData = data;
@@ -103,7 +103,7 @@ class CryptoClient extends core_1.CoreCryptoClient {
103
103
  return this.authProvider.bearerTokenValidation(req);
104
104
  }
105
105
  const authHeader = req.headers.authorization;
106
- const token = authHeader && authHeader.split(' ')[1];
106
+ const token = authHeader?.split(' ')?.[1];
107
107
  if (!token) {
108
108
  return 401;
109
109
  }
@@ -25,7 +25,7 @@ export default class GetTokenAction extends BaseApiAction {
25
25
  const jwt = await cc.jwtGenerateAccessToken({
26
26
  userid: valid.userId,
27
27
  username,
28
- });
28
+ }, req.body);
29
29
  ex.done();
30
30
  res.status(200).send({ token: jwt });
31
31
  }
@@ -13,11 +13,11 @@ export interface IAuthProvider<T extends JwtExtend> {
13
13
  authorizeToken(userid: string, token: string, requestType: string): Promise<AuthResult>;
14
14
  validateAccess(token: JwtToken<T>, requestType: string): Promise<boolean>;
15
15
  bearerTokenValidation(req: XRequest): Promise<JwtToken<T> | number>;
16
- jwtAddData(token: JwtToken<T>): Promise<JwtToken<T>>;
16
+ jwtAddData(token: JwtToken<T>, extend?: Record<string, any>): Promise<JwtToken<T>>;
17
17
  }
18
18
  export default abstract class BaseAuthProvider<T extends JwtExtend = JwtExtend> implements IAuthProvider<T> {
19
19
  abstract authorizeToken(username: string, token: string, requestType: string): Promise<AuthResult>;
20
20
  abstract validateAccess(token: JwtToken<T>, requestType: string): Promise<boolean>;
21
21
  abstract bearerTokenValidation(req: XRequest): Promise<JwtToken<T> | number>;
22
- jwtAddData(token: JwtToken<T>): Promise<JwtToken<T>>;
22
+ jwtAddData(token: JwtToken<T>, extend?: Record<string, any>): Promise<JwtToken<T>>;
23
23
  }
@@ -1,5 +1,5 @@
1
1
  export default class BaseAuthProvider {
2
- async jwtAddData(token) {
2
+ async jwtAddData(token, extend) {
3
3
  return token;
4
4
  }
5
5
  }
@@ -13,7 +13,7 @@ export default class ServerTiming {
13
13
  return content;
14
14
  }
15
15
  completeElement(e) {
16
- const cur = this.map.get(e.chanel) || [];
16
+ const cur = this.map.get(e.chanel) ?? [];
17
17
  cur.push(e);
18
18
  this.map.set(e.chanel, cur);
19
19
  }
@@ -1,6 +1,6 @@
1
1
  import express from 'express';
2
2
  export type XRequest = express.Request & {
3
- rawBody?: string | null | undefined;
3
+ rawBody?: string | null;
4
4
  };
5
5
  export type XResponse = express.Response;
6
6
  export type XNextFc = express.NextFunction;
@@ -8,7 +8,7 @@ export interface ICClient<T extends JwtExtend = JwtExtend> extends ICoreCClient
8
8
  setAuthProvider(provider: IAuthProvider<T>): boolean;
9
9
  jwtVerifyAccessToken(token: string): Promise<JwtToken<T> | number>;
10
10
  jwtDecodeAccessToken(token: string): jwt.JwtPayload | null;
11
- jwtGenerateAccessToken(data: JwtToken<T>, expire?: string | number): Promise<string>;
11
+ jwtGenerateAccessToken(data: JwtToken<T>, extend?: Record<string, any>, expire?: string | number): Promise<string>;
12
12
  apiTokenValidation(username: string, token: string, requestType: string): Promise<{
13
13
  valid: boolean;
14
14
  userId: string | null;
@@ -10,7 +10,7 @@ export default class CryptoClient<T extends JwtExtend = JwtExtend> extends CoreC
10
10
  setAuthProvider(provider: IAuthProvider<T>): boolean;
11
11
  jwtVerifyAccessToken(token: string): Promise<JwtToken<T> | number>;
12
12
  jwtDecodeAccessToken(token: string): JwtToken<T> | null;
13
- jwtGenerateAccessToken(data: JwtToken<T>, expire?: string | number): Promise<string>;
13
+ jwtGenerateAccessToken(data: JwtToken<T>, extend?: Record<string, any>, expire?: string | number): Promise<string>;
14
14
  apiTokenValidation(username: string, token: string, requestType: string): Promise<{
15
15
  valid: boolean;
16
16
  userId: string | null;
@@ -34,10 +34,10 @@ export default class CryptoClient extends CoreCryptoClient {
34
34
  const mod = jwt.default || jwt;
35
35
  return mod.decode(token, { json: true });
36
36
  }
37
- async jwtGenerateAccessToken(data, expire) {
37
+ async jwtGenerateAccessToken(data, extend, expire) {
38
38
  let sData;
39
39
  if (this.authProvider) {
40
- sData = await this.authProvider.jwtAddData(data);
40
+ sData = await this.authProvider.jwtAddData(data, extend);
41
41
  }
42
42
  else {
43
43
  sData = data;
@@ -78,7 +78,7 @@ export default class CryptoClient extends CoreCryptoClient {
78
78
  return this.authProvider.bearerTokenValidation(req);
79
79
  }
80
80
  const authHeader = req.headers.authorization;
81
- const token = authHeader && authHeader.split(' ')[1];
81
+ const token = authHeader?.split(' ')?.[1];
82
82
  if (!token) {
83
83
  return 401;
84
84
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grandlinex/kernel",
3
- "version": "0.31.0",
3
+ "version": "0.31.1",
4
4
  "description": "GrandLineX is an out-of-the-box server framework on top of ExpressJs.",
5
5
  "type": "module",
6
6
  "exports": {