@midwayjs/jwt 2.13.3

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/README.md ADDED
@@ -0,0 +1,36 @@
1
+ ## 开始
2
+
3
+ 安装 `npm i @midwayjs/jwt `
4
+
5
+ 可配合 @midwayjs/passport 使用
6
+
7
+ config.{env}.ts
8
+
9
+ ```ts
10
+ config.jwt = {
11
+ secret: 'dev123456',
12
+ expiresIn: '10d', // https://github.com/vercel/ms
13
+ };
14
+ ```
15
+
16
+ ```ts
17
+ import { JWTService } from '@midwayjs/jwt';
18
+
19
+ @Provide()
20
+ class Demo {
21
+ @Inject()
22
+ jwt: JwtService;
23
+ }
24
+ ```
25
+
26
+ ## API
27
+
28
+ **请不要在 payload 存放任何敏感信息**
29
+
30
+ - public async sign(payload: JwtPayload, options?: SignOptions, secret?: Secret): Promise<string | void>
31
+ - public signSync(payload: JwtPayload, options?: SignOptions, secret?: Secret): string | void
32
+
33
+ - verifySync(token: string,options?: VerifyOptions & { complete: true },secret?: Secret): Jwt | string | JwtPayload
34
+ - public async verify(token: string,options?: VerifyOptions & { complete: true },secret?: Secret | GetPublicKeyOrSecret): Promise<JwtType | undefined | JwtPayload>
35
+
36
+ - decodeSync(token: string,options?: DecodeOptions & { complete: true } & { json: true }): Jwt | null | JwtPayload | string
@@ -0,0 +1,5 @@
1
+ import { MidwayContainer } from '@midwayjs/core';
2
+ export declare class JwtConfiguration {
3
+ onReady(container: MidwayContainer): Promise<void>;
4
+ }
5
+ //# sourceMappingURL=configuration.d.ts.map
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.JwtConfiguration = void 0;
10
+ const decorator_1 = require("@midwayjs/decorator");
11
+ const jwt_1 = require("./jwt");
12
+ let JwtConfiguration = class JwtConfiguration {
13
+ async onReady(container) {
14
+ await container.getAsync(jwt_1.JwtService);
15
+ }
16
+ };
17
+ JwtConfiguration = __decorate([
18
+ (0, decorator_1.Configuration)({
19
+ namespace: 'jwt',
20
+ importConfigs: [
21
+ {
22
+ default: {
23
+ jwt: {},
24
+ },
25
+ },
26
+ ],
27
+ })
28
+ ], JwtConfiguration);
29
+ exports.JwtConfiguration = JwtConfiguration;
30
+ //# sourceMappingURL=configuration.js.map
@@ -0,0 +1,3 @@
1
+ export { JwtConfiguration as Configuration } from './configuration';
2
+ export * from './jwt';
3
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.Configuration = void 0;
14
+ var configuration_1 = require("./configuration");
15
+ Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.JwtConfiguration; } });
16
+ __exportStar(require("./jwt"), exports);
17
+ //# sourceMappingURL=index.js.map
package/dist/jwt.d.ts ADDED
@@ -0,0 +1,85 @@
1
+ /// <reference types="node" />
2
+ import type { DecodeOptions, GetPublicKeyOrSecret, Jwt, Secret, SignOptions, VerifyOptions } from 'jsonwebtoken';
3
+ declare type JwtPayload = string | Buffer | Record<string, any>;
4
+ /**
5
+ *
6
+ * @see{@link https://github.com/auth0/node-jsonwebtoken}
7
+ */
8
+ export declare class JwtService {
9
+ private jwtConfig;
10
+ /**
11
+ * Synchronously sign the given payload into a JSON Web Token string
12
+ * payload - Payload to sign, could be an literal, buffer or string
13
+ * secretOrPrivateKey - Either the secret for HMAC algorithms, or the PEM encoded private key for RSA and ECDSA.
14
+ * [options] - Options for the signature
15
+ * returns - The JSON Web Token string
16
+ */
17
+ signSync(payload: JwtPayload, options?: SignOptions): string;
18
+ signSync(payload: JwtPayload, secretOrPrivateKey: Secret, options?: SignOptions): string;
19
+ /**
20
+ * Asynchronous sign the given payload into a JSON Web Token string
21
+ * payload - Payload to sign, could be an literal, buffer or string
22
+ * secretOrPrivateKey - Either the secret for HMAC algorithms, or the PEM encoded private key for RSA and ECDSA.
23
+ * [options] - Options for the signature
24
+ * returns - The JSON Web Token string
25
+ */
26
+ sign(payload: JwtPayload, options?: SignOptions): Promise<string>;
27
+ sign(payload: JwtPayload, secretOrPrivateKey: Secret, options?: SignOptions): Promise<string>;
28
+ /**
29
+ * Synchronously verify given token using a secret or a public key to get a decoded token
30
+ * token - JWT string to verify
31
+ * secretOrPublicKey - Either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA.
32
+ * [options] - Options for the verification
33
+ * returns - The decoded token.
34
+ */
35
+ verifySync(token: string, options: VerifyOptions & {
36
+ complete: true;
37
+ }): Jwt | string;
38
+ verifySync(token: string, options: VerifyOptions): JwtPayload | string;
39
+ verifySync(token: string, secretOrPublicKey: Secret, options?: VerifyOptions & {
40
+ complete: true;
41
+ }): Jwt | string;
42
+ verifySync(token: string, secretOrPublicKey: Secret, options?: VerifyOptions): JwtPayload | string;
43
+ /**
44
+ * Asynchronous verify given token using a secret or a public key to get a decoded token
45
+ * token - JWT string to verify
46
+ * secretOrPublicKey - Either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA.
47
+ * [options] - Options for the verification
48
+ * returns - The decoded token.
49
+ */
50
+ verify(token: string, options?: VerifyOptions & {
51
+ complete: true;
52
+ }): Promise<Jwt | string>;
53
+ verify(token: string, options?: VerifyOptions): Promise<JwtPayload | string>;
54
+ verify(token: string, secretOrPublicKey: Secret | GetPublicKeyOrSecret, options?: VerifyOptions & {
55
+ complete: true;
56
+ }): Promise<Jwt | string>;
57
+ verify(token: string, secretOrPublicKey: Secret | GetPublicKeyOrSecret, options?: VerifyOptions): Promise<JwtPayload | string>;
58
+ /**
59
+ * Returns the decoded payload without verifying if the signature is valid.
60
+ * token - JWT string to decode
61
+ * [options] - Options for decoding
62
+ * returns - The decoded Token
63
+ */
64
+ decode(token: string, options: DecodeOptions & {
65
+ complete: true;
66
+ }): null | Jwt;
67
+ decode(token: string, options: DecodeOptions & {
68
+ json: true;
69
+ }): null | JwtPayload;
70
+ decode(token: string, options?: DecodeOptions): null | JwtPayload | string;
71
+ /**
72
+ * alias decode method
73
+ * @param token
74
+ * @param options
75
+ */
76
+ decodeSync(token: string, options: DecodeOptions & {
77
+ complete: true;
78
+ }): null | Jwt;
79
+ decodeSync(token: string, options: DecodeOptions & {
80
+ json: true;
81
+ }): null | JwtPayload;
82
+ decodeSync(token: string, options?: DecodeOptions): null | JwtPayload | string;
83
+ }
84
+ export {};
85
+ //# sourceMappingURL=jwt.d.ts.map
package/dist/jwt.js ADDED
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.JwtService = void 0;
13
+ const decorator_1 = require("@midwayjs/decorator");
14
+ const jwt = require("jsonwebtoken");
15
+ /**
16
+ *
17
+ * @see{@link https://github.com/auth0/node-jsonwebtoken}
18
+ */
19
+ let JwtService = class JwtService {
20
+ signSync(payload, secretOrPrivateKey, options) {
21
+ var _a, _b;
22
+ if (!options) {
23
+ options = secretOrPrivateKey;
24
+ secretOrPrivateKey = (_a = this.jwtConfig) === null || _a === void 0 ? void 0 : _a.secret;
25
+ }
26
+ if (!secretOrPrivateKey) {
27
+ throw new Error('[midway-jwt]: jwt secret should be set');
28
+ }
29
+ options = options !== null && options !== void 0 ? options : {};
30
+ options.expiresIn = (_b = options.expiresIn) !== null && _b !== void 0 ? _b : this.jwtConfig.expiresIn;
31
+ return jwt.sign(payload, secretOrPrivateKey, options);
32
+ }
33
+ async sign(payload, secretOrPrivateKey, options) {
34
+ var _a, _b;
35
+ if (!options) {
36
+ options = secretOrPrivateKey;
37
+ secretOrPrivateKey = (_a = this.jwtConfig) === null || _a === void 0 ? void 0 : _a.secret;
38
+ }
39
+ if (!secretOrPrivateKey) {
40
+ throw new Error('[midway-jwt]: provide the jwt secret please');
41
+ }
42
+ options = options !== null && options !== void 0 ? options : {};
43
+ options.expiresIn = (_b = options.expiresIn) !== null && _b !== void 0 ? _b : this.jwtConfig.expiresIn;
44
+ return new Promise((resolve, reject) => {
45
+ jwt.sign(payload, secretOrPrivateKey, options, (err, encoded) => {
46
+ if (err) {
47
+ reject(err);
48
+ }
49
+ else {
50
+ resolve(encoded);
51
+ }
52
+ });
53
+ });
54
+ }
55
+ verifySync(token, secretOrPublicKey, options) {
56
+ var _a;
57
+ if (!options) {
58
+ options = secretOrPublicKey;
59
+ secretOrPublicKey = (_a = this.jwtConfig) === null || _a === void 0 ? void 0 : _a.secret;
60
+ }
61
+ if (!secretOrPublicKey) {
62
+ throw new Error('[midway-jwt]: provide the jwt secret please');
63
+ }
64
+ return jwt.verify(token, secretOrPublicKey, options);
65
+ }
66
+ async verify(token, secretOrPublicKey, options) {
67
+ var _a;
68
+ if (!options) {
69
+ options = secretOrPublicKey;
70
+ secretOrPublicKey = (_a = this.jwtConfig) === null || _a === void 0 ? void 0 : _a.secret;
71
+ }
72
+ if (!secretOrPublicKey) {
73
+ throw new Error('[midway-jwt]: provide the jwt secret please');
74
+ }
75
+ return new Promise((resolve, reject) => {
76
+ jwt.verify(token, secretOrPublicKey, options, (err, encoded) => {
77
+ if (err) {
78
+ reject(err);
79
+ }
80
+ else {
81
+ resolve(encoded);
82
+ }
83
+ });
84
+ });
85
+ }
86
+ decode(token, options) {
87
+ return jwt.decode(token, options);
88
+ }
89
+ decodeSync(token, options) {
90
+ return this.decode(token, options);
91
+ }
92
+ };
93
+ __decorate([
94
+ (0, decorator_1.Config)('jwt'),
95
+ __metadata("design:type", Object)
96
+ ], JwtService.prototype, "jwtConfig", void 0);
97
+ JwtService = __decorate([
98
+ (0, decorator_1.Provide)(),
99
+ (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
100
+ ], JwtService);
101
+ exports.JwtService = JwtService;
102
+ //# sourceMappingURL=jwt.js.map
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@midwayjs/jwt",
3
+ "description": "midway jwt component",
4
+ "version": "2.13.3",
5
+ "main": "dist/index.js",
6
+ "typings": "dist/index.d.ts",
7
+ "files": [
8
+ "dist/**/*.js",
9
+ "dist/**/*.d.ts"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "test": "node --require=ts-node/register ../../node_modules/jest/bin/jest.js",
14
+ "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --coverage --forceExit"
15
+ },
16
+ "keywords": [
17
+ "midway",
18
+ "jsonwebtoken",
19
+ "jwt"
20
+ ],
21
+ "author": "Nawbc",
22
+ "license": "MIT",
23
+ "devDependencies": {
24
+ "@midwayjs/core": "^2.13.4",
25
+ "@midwayjs/decorator": "^2.13.2",
26
+ "@midwayjs/express": "^2.13.4",
27
+ "@midwayjs/koa": "^2.13.4",
28
+ "@midwayjs/mock": "^2.13.4",
29
+ "@midwayjs/web": "^2.13.4",
30
+ "@types/jsonwebtoken": "^8.5.5",
31
+ "passport-jwt": "^4.0.0"
32
+ },
33
+ "dependencies": {
34
+ "jsonwebtoken": "^8.5.1"
35
+ }
36
+ }