@midwayjs/jwt 3.12.10 → 3.13.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/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ export * from './interface';
1
2
  export { JwtConfiguration as Configuration } from './configuration';
2
3
  export * from './jwt';
4
+ export * as Jwt from 'jsonwebtoken';
3
5
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -14,8 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.Configuration = void 0;
17
+ exports.Jwt = exports.Configuration = void 0;
18
+ __exportStar(require("./interface"), exports);
18
19
  var configuration_1 = require("./configuration");
19
20
  Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.JwtConfiguration; } });
20
21
  __exportStar(require("./jwt"), exports);
22
+ exports.Jwt = require("jsonwebtoken");
21
23
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,11 @@
1
+ import { DecodeOptions, SignOptions, VerifyOptions } from 'jsonwebtoken';
2
+ export type JwtUserConfig = (SignOptions & {
3
+ secret?: string;
4
+ }) | JwtConfig;
5
+ export type JwtConfig = {
6
+ sign?: SignOptions;
7
+ verify?: VerifyOptions;
8
+ decode?: DecodeOptions;
9
+ secret?: string;
10
+ };
11
+ //# sourceMappingURL=interface.d.ts.map
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=interface.js.map
package/dist/jwt.d.ts CHANGED
@@ -80,6 +80,10 @@ export declare class JwtService {
80
80
  json: true;
81
81
  }): null | JwtPayload;
82
82
  decodeSync(token: string, options?: DecodeOptions): null | JwtPayload | string;
83
+ getSignOptions(options?: SignOptions): SignOptions | import("./interface").JwtConfig;
84
+ getVerifyOptions(options?: VerifyOptions): VerifyOptions;
85
+ getDecodeOptions(options?: DecodeOptions): DecodeOptions;
86
+ isSecret(secret: any): secret is Secret;
83
87
  }
84
88
  export {};
85
89
  //# sourceMappingURL=jwt.d.ts.map
package/dist/jwt.js CHANGED
@@ -11,6 +11,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.JwtService = void 0;
13
13
  const core_1 = require("@midwayjs/core");
14
+ const crypto_1 = require("crypto");
14
15
  const jwt = require("jsonwebtoken");
15
16
  /**
16
17
  *
@@ -18,29 +19,27 @@ const jwt = require("jsonwebtoken");
18
19
  */
19
20
  let JwtService = class JwtService {
20
21
  signSync(payload, secretOrPrivateKey, options) {
21
- var _a, _b;
22
- if (!options) {
22
+ var _a;
23
+ if (!this.isSecret(secretOrPrivateKey)) {
23
24
  options = secretOrPrivateKey;
24
25
  secretOrPrivateKey = (_a = this.jwtConfig) === null || _a === void 0 ? void 0 : _a.secret;
25
26
  }
26
27
  if (!secretOrPrivateKey) {
27
- throw new Error('[midway:jwt] jwt secret should be set');
28
+ throw new core_1.MidwayCommonError('[midway:jwt] jwt secret should be set');
28
29
  }
29
- options = options !== null && options !== void 0 ? options : {};
30
- options.expiresIn = (_b = options.expiresIn) !== null && _b !== void 0 ? _b : this.jwtConfig.expiresIn;
30
+ options = this.getSignOptions(options);
31
31
  return jwt.sign(payload, secretOrPrivateKey, options);
32
32
  }
33
33
  async sign(payload, secretOrPrivateKey, options) {
34
- var _a, _b;
35
- if (!options) {
34
+ var _a;
35
+ if (!this.isSecret(secretOrPrivateKey)) {
36
36
  options = secretOrPrivateKey;
37
37
  secretOrPrivateKey = (_a = this.jwtConfig) === null || _a === void 0 ? void 0 : _a.secret;
38
38
  }
39
39
  if (!secretOrPrivateKey) {
40
- throw new Error('[midway:jwt] provide the jwt secret please');
40
+ throw new core_1.MidwayCommonError('[midway:jwt] provide the jwt secret please');
41
41
  }
42
- options = options !== null && options !== void 0 ? options : {};
43
- options.expiresIn = (_b = options.expiresIn) !== null && _b !== void 0 ? _b : this.jwtConfig.expiresIn;
42
+ options = this.getSignOptions(options);
44
43
  return new Promise((resolve, reject) => {
45
44
  jwt.sign(payload, secretOrPrivateKey, options, (err, encoded) => {
46
45
  if (err) {
@@ -54,24 +53,26 @@ let JwtService = class JwtService {
54
53
  }
55
54
  verifySync(token, secretOrPublicKey, options) {
56
55
  var _a;
57
- if (!options) {
56
+ if (!this.isSecret(secretOrPublicKey)) {
58
57
  options = secretOrPublicKey;
59
58
  secretOrPublicKey = (_a = this.jwtConfig) === null || _a === void 0 ? void 0 : _a.secret;
60
59
  }
61
60
  if (!secretOrPublicKey) {
62
- throw new Error('[midway:jwt] provide the jwt secret please');
61
+ throw new core_1.MidwayCommonError('[midway:jwt] provide the jwt secret please');
63
62
  }
63
+ options = this.getVerifyOptions(options);
64
64
  return jwt.verify(token, secretOrPublicKey, options);
65
65
  }
66
66
  async verify(token, secretOrPublicKey, options) {
67
67
  var _a;
68
- if (!options) {
68
+ if (!this.isSecret(secretOrPublicKey)) {
69
69
  options = secretOrPublicKey;
70
70
  secretOrPublicKey = (_a = this.jwtConfig) === null || _a === void 0 ? void 0 : _a.secret;
71
71
  }
72
72
  if (!secretOrPublicKey) {
73
- throw new Error('[midway:jwt] provide the jwt secret please');
73
+ throw new core_1.MidwayCommonError('[midway:jwt] provide the jwt secret please');
74
74
  }
75
+ options = this.getVerifyOptions(options);
75
76
  return new Promise((resolve, reject) => {
76
77
  jwt.verify(token, secretOrPublicKey, options, (err, encoded) => {
77
78
  if (err) {
@@ -84,10 +85,45 @@ let JwtService = class JwtService {
84
85
  });
85
86
  }
86
87
  decode(token, options) {
87
- return jwt.decode(token, options);
88
+ return jwt.decode(token, this.getDecodeOptions(options));
88
89
  }
89
90
  decodeSync(token, options) {
90
- return this.decode(token, options);
91
+ return this.decode(token, this.getDecodeOptions(options));
92
+ }
93
+ getSignOptions(options) {
94
+ let signOptions = 'sign' in this.jwtConfig ? this.jwtConfig.sign : this.jwtConfig;
95
+ signOptions = Object.assign({}, signOptions, options);
96
+ // delete possible invalid options from jwtConfig
97
+ for (const keyToDelete of ['sign', 'verify', 'decode', 'secret']) {
98
+ delete signOptions[keyToDelete];
99
+ }
100
+ return signOptions;
101
+ }
102
+ getVerifyOptions(options) {
103
+ let verifyOptions = 'verify' in this.jwtConfig ? this.jwtConfig.verify : {};
104
+ verifyOptions = Object.assign({}, verifyOptions, options);
105
+ delete verifyOptions['secret'];
106
+ return verifyOptions;
107
+ }
108
+ getDecodeOptions(options) {
109
+ let decodeOptions = 'decode' in this.jwtConfig ? this.jwtConfig.decode : {};
110
+ decodeOptions = Object.assign({}, decodeOptions, options);
111
+ delete decodeOptions['secret'];
112
+ return decodeOptions;
113
+ }
114
+ isSecret(secret) {
115
+ if (typeof secret === 'string')
116
+ return true;
117
+ if (Buffer.isBuffer(secret))
118
+ return true;
119
+ if (secret instanceof crypto_1.KeyObject)
120
+ return true;
121
+ if (secret &&
122
+ typeof secret === 'object' &&
123
+ 'key' in secret &&
124
+ 'passphrase' in secret)
125
+ return true;
126
+ return false;
91
127
  }
92
128
  };
93
129
  __decorate([
package/index.d.ts CHANGED
@@ -1,11 +1,8 @@
1
- import { SignOptions } from 'jsonwebtoken';
2
-
1
+ import { JwtUserConfig } from './dist/index';
3
2
  export * from './dist/index';
4
3
 
5
4
  declare module '@midwayjs/core/dist/interface' {
6
5
  interface MidwayConfig {
7
- jwt?: SignOptions & {
8
- secret?: string;
9
- };
6
+ jwt?: JwtUserConfig;
10
7
  }
11
8
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@midwayjs/jwt",
3
3
  "description": "midway jwt component",
4
- "version": "3.12.10",
4
+ "version": "3.13.1",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
7
7
  "files": [
@@ -19,18 +19,21 @@
19
19
  "jsonwebtoken",
20
20
  "jwt"
21
21
  ],
22
+ "engines": {
23
+ "node": ">=12"
24
+ },
22
25
  "author": "Nawbc",
23
26
  "license": "MIT",
24
27
  "devDependencies": {
25
- "@midwayjs/core": "^3.12.3",
26
- "@midwayjs/express": "^3.12.10",
27
- "@midwayjs/koa": "^3.12.10",
28
- "@midwayjs/mock": "^3.12.10",
29
- "@midwayjs/web": "^3.12.10"
28
+ "@midwayjs/core": "^3.13.0",
29
+ "@midwayjs/express": "^3.13.0",
30
+ "@midwayjs/koa": "^3.13.0",
31
+ "@midwayjs/mock": "^3.13.0",
32
+ "@midwayjs/web": "^3.13.1"
30
33
  },
31
34
  "dependencies": {
32
- "@types/jsonwebtoken": "9.0.4",
35
+ "@types/jsonwebtoken": "9.0.5",
33
36
  "jsonwebtoken": "9.0.2"
34
37
  },
35
- "gitHead": "8d92c54424843d437fd5f77f6ff93f00c055117b"
38
+ "gitHead": "bc7378899b34cc2846a7429777d052b0ed7cdb24"
36
39
  }