@celljs/jwt 3.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Cellbang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # Cell - JWT Component
package/cell.yml ADDED
@@ -0,0 +1,3 @@
1
+ # cell:
2
+ # jwt:
3
+ # secret: your secret
@@ -0,0 +1,8 @@
1
+ import { CustomError } from '@celljs/core';
2
+ export declare class PublicKeyNotFoundError extends CustomError {
3
+ constructor();
4
+ }
5
+ export declare class PrivateKeyNotFoundError extends CustomError {
6
+ constructor();
7
+ }
8
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/node/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,qBAAa,sBAAuB,SAAQ,WAAW;;CAItD;AAED,qBAAa,uBAAwB,SAAQ,WAAW;;CAIvD"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PrivateKeyNotFoundError = exports.PublicKeyNotFoundError = void 0;
4
+ const core_1 = require("@celljs/core");
5
+ class PublicKeyNotFoundError extends core_1.CustomError {
6
+ constructor() {
7
+ super('Public key not found');
8
+ }
9
+ }
10
+ exports.PublicKeyNotFoundError = PublicKeyNotFoundError;
11
+ class PrivateKeyNotFoundError extends core_1.CustomError {
12
+ constructor() {
13
+ super('Private key not found');
14
+ }
15
+ }
16
+ exports.PrivateKeyNotFoundError = PrivateKeyNotFoundError;
17
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/node/errors.ts"],"names":[],"mappings":";;;AAAA,uCAA2C;AAE3C,MAAa,sBAAuB,SAAQ,kBAAW;IACnD;QACI,KAAK,CAAC,sBAAsB,CAAC,CAAC;IAClC,CAAC;CACJ;AAJD,wDAIC;AAED,MAAa,uBAAwB,SAAQ,kBAAW;IACpD;QACI,KAAK,CAAC,uBAAuB,CAAC,CAAC;IACnC,CAAC;CACJ;AAJD,0DAIC"}
@@ -0,0 +1,5 @@
1
+ export * from './jwt-protocol';
2
+ export * from './jwt-service';
3
+ export * from './errors';
4
+ export * from './jwt-secret-service';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,cAAc,sBAAsB,CAAC"}
@@ -0,0 +1,21 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./jwt-protocol"), exports);
18
+ __exportStar(require("./jwt-service"), exports);
19
+ __exportStar(require("./errors"), exports);
20
+ __exportStar(require("./jwt-secret-service"), exports);
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B;AAC/B,gDAA8B;AAC9B,2CAAyB;AACzB,uDAAqC"}
@@ -0,0 +1,32 @@
1
+ /// <reference types="node" />
2
+ import * as jwt from 'jsonwebtoken';
3
+ export declare const JwtService: unique symbol;
4
+ export declare const JwtSecretService: unique symbol;
5
+ export interface JwtOptions {
6
+ signOptions?: jwt.SignOptions;
7
+ secret?: string | Buffer;
8
+ publicKey?: string | Buffer;
9
+ privateKey?: jwt.Secret;
10
+ verifyOptions?: jwt.VerifyOptions;
11
+ }
12
+ export interface SignOptions extends jwt.SignOptions {
13
+ }
14
+ export interface VerifyOptions extends jwt.VerifyOptions {
15
+ }
16
+ export interface DecodeOptions extends jwt.DecodeOptions {
17
+ }
18
+ export interface Jwt extends jwt.Jwt {
19
+ }
20
+ export interface JwtPayload extends jwt.JwtPayload {
21
+ }
22
+ export declare type Secret = jwt.Secret;
23
+ export interface JwtService {
24
+ sign(payload: string | Buffer | object, options?: SignOptions, privateKey?: Secret): Promise<string>;
25
+ verify<T extends object = any>(token: string, options?: VerifyOptions, publicKey?: string | Buffer): Promise<T>;
26
+ decode(token: string, options?: DecodeOptions): Promise<JwtPayload | string | undefined>;
27
+ }
28
+ export interface JwtSecretService {
29
+ getPublicKey(): Promise<string | Buffer>;
30
+ getPrivateKey(): Promise<Secret>;
31
+ }
32
+ //# sourceMappingURL=jwt-protocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt-protocol.d.ts","sourceRoot":"","sources":["../../src/node/jwt-protocol.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,GAAG,MAAM,cAAc,CAAC;AAEpC,eAAO,MAAM,UAAU,eAAuB,CAAC;AAC/C,eAAO,MAAM,gBAAgB,eAA6B,CAAC;AAE3D,MAAM,WAAW,UAAU;IACvB,WAAW,CAAC,EAAE,GAAG,CAAC,WAAW,CAAC;IAC9B,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC5B,UAAU,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC;IACxB,aAAa,CAAC,EAAE,GAAG,CAAC,aAAa,CAAC;CACrC;AAED,MAAM,WAAW,WAAY,SAAQ,GAAG,CAAC,WAAW;CAAG;AAEvD,MAAM,WAAW,aAAc,SAAQ,GAAG,CAAC,aAAa;CAAG;AAE3D,MAAM,WAAW,aAAc,SAAQ,GAAG,CAAC,aAAa;CAAG;AAE3D,MAAM,WAAW,GAAI,SAAQ,GAAG,CAAC,GAAG;CAAG;AAEvC,MAAM,WAAW,UAAW,SAAQ,GAAG,CAAC,UAAU;CAAG;AAErD,oBAAY,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;AAEhC,MAAM,WAAW,UAAU;IACvB,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACrG,MAAM,CAAC,CAAC,SAAS,MAAM,GAAG,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAChH,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,UAAU,GAAG,MAAM,GAAG,SAAS,CAAC,CAAC;CAC5F;AAED,MAAM,WAAW,gBAAgB;IAC7B,YAAY,IAAI,OAAO,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC;IACzC,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;CACpC"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JwtSecretService = exports.JwtService = void 0;
4
+ exports.JwtService = Symbol('JwtService');
5
+ exports.JwtSecretService = Symbol('JwtSecretService');
6
+ //# sourceMappingURL=jwt-protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt-protocol.js","sourceRoot":"","sources":["../../src/node/jwt-protocol.ts"],"names":[],"mappings":";;;AAEa,QAAA,UAAU,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AAClC,QAAA,gBAAgB,GAAG,MAAM,CAAC,kBAAkB,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ /// <reference types="node" />
2
+ import { JwtOptions, JwtSecretService, Secret } from './jwt-protocol';
3
+ export declare class jwtSecretService implements JwtSecretService {
4
+ protected readonly jwtOptions?: JwtOptions;
5
+ getPublicKey(): Promise<string | Buffer>;
6
+ getPrivateKey(): Promise<Secret>;
7
+ }
8
+ //# sourceMappingURL=jwt-secret-service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt-secret-service.d.ts","sourceRoot":"","sources":["../../src/node/jwt-secret-service.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAEtE,qBACa,gBAAiB,YAAW,gBAAgB;IAGrD,SAAS,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC;IAErC,YAAY,IAAI,OAAO,CAAC,MAAM,GAAG,MAAM,CAAC;IAQxC,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;CAOzC"}
@@ -0,0 +1,42 @@
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.jwtSecretService = void 0;
13
+ const core_1 = require("@celljs/core");
14
+ const errors_1 = require("./errors");
15
+ const jwt_protocol_1 = require("./jwt-protocol");
16
+ let jwtSecretService = class jwtSecretService {
17
+ async getPublicKey() {
18
+ var _a, _b, _c;
19
+ const secret = (_b = (_a = this.jwtOptions) === null || _a === void 0 ? void 0 : _a.publicKey) !== null && _b !== void 0 ? _b : (_c = this.jwtOptions) === null || _c === void 0 ? void 0 : _c.secret;
20
+ if (secret) {
21
+ return secret;
22
+ }
23
+ throw new errors_1.PublicKeyNotFoundError();
24
+ }
25
+ async getPrivateKey() {
26
+ var _a, _b, _c;
27
+ const secret = (_b = (_a = this.jwtOptions) === null || _a === void 0 ? void 0 : _a.privateKey) !== null && _b !== void 0 ? _b : (_c = this.jwtOptions) === null || _c === void 0 ? void 0 : _c.secret;
28
+ if (secret) {
29
+ return secret;
30
+ }
31
+ throw new errors_1.PrivateKeyNotFoundError();
32
+ }
33
+ };
34
+ __decorate([
35
+ (0, core_1.Value)('cell.jwt'),
36
+ __metadata("design:type", Object)
37
+ ], jwtSecretService.prototype, "jwtOptions", void 0);
38
+ jwtSecretService = __decorate([
39
+ (0, core_1.Component)(jwt_protocol_1.JwtSecretService)
40
+ ], jwtSecretService);
41
+ exports.jwtSecretService = jwtSecretService;
42
+ //# sourceMappingURL=jwt-secret-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt-secret-service.js","sourceRoot":"","sources":["../../src/node/jwt-secret-service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,uCAAgD;AAChD,qCAA2E;AAC3E,iDAAsE;AAG/D,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IAKzB,KAAK,CAAC,YAAY;;QACd,MAAM,MAAM,GAAG,MAAA,MAAA,IAAI,CAAC,UAAU,0CAAE,SAAS,mCAAI,MAAA,IAAI,CAAC,UAAU,0CAAE,MAAM,CAAC;QACrE,IAAI,MAAM,EAAE;YACR,OAAO,MAAM,CAAC;SACjB;QACD,MAAM,IAAI,+BAAsB,EAAE,CAAC;IACvC,CAAC;IAED,KAAK,CAAC,aAAa;;QACf,MAAM,MAAM,GAAG,MAAA,MAAA,IAAI,CAAC,UAAU,0CAAE,UAAU,mCAAI,MAAA,IAAI,CAAC,UAAU,0CAAE,MAAM,CAAC;QACtE,IAAI,MAAM,EAAE;YACR,OAAO,MAAM,CAAC;SACjB;QACD,MAAM,IAAI,gCAAuB,EAAE,CAAC;IACxC,CAAC;CACJ,CAAA;AAlBG;IAAC,IAAA,YAAK,EAAC,UAAU,CAAC;;oDACyB;AAHlC,gBAAgB;IAD5B,IAAA,gBAAS,EAAC,+BAAgB,CAAC;GACf,gBAAgB,CAoB5B;AApBY,4CAAgB"}
@@ -0,0 +1,10 @@
1
+ /// <reference types="node" />
2
+ import { DecodeOptions, JwtOptions, JwtPayload, JwtSecretService, JwtService, Secret, SignOptions, VerifyOptions } from './jwt-protocol';
3
+ export declare class JwtServiceImpl implements JwtService {
4
+ protected readonly jwtOptions?: JwtOptions;
5
+ protected readonly jwtSecretService: JwtSecretService;
6
+ sign(payload: string | object | Buffer, options?: SignOptions, privateKey?: Secret): Promise<string>;
7
+ verify<T extends object = any>(token: string, options?: VerifyOptions, publicKey?: string | Buffer): Promise<T>;
8
+ decode(token: string, options?: DecodeOptions): Promise<JwtPayload | string | undefined>;
9
+ }
10
+ //# sourceMappingURL=jwt-service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt-service.d.ts","sourceRoot":"","sources":["../../src/node/jwt-service.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,UAAU,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAGzI,qBACa,cAAe,YAAW,UAAU;IAG7C,SAAS,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC;IAG3C,SAAS,CAAC,QAAQ,CAAC,gBAAgB,EAAE,gBAAgB,CAAC;IAEhD,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAcpG,MAAM,CAAC,CAAC,SAAS,MAAM,GAAG,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC;IAc/G,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,UAAU,GAAG,MAAM,GAAG,SAAS,CAAC;CAQjG"}
@@ -0,0 +1,67 @@
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.JwtServiceImpl = void 0;
13
+ const core_1 = require("@celljs/core");
14
+ const jwt_protocol_1 = require("./jwt-protocol");
15
+ const jwt = require("jsonwebtoken");
16
+ let JwtServiceImpl = class JwtServiceImpl {
17
+ async sign(payload, options, privateKey) {
18
+ var _a;
19
+ const mergedOptions = Object.assign(Object.assign({}, (_a = this.jwtOptions) === null || _a === void 0 ? void 0 : _a.signOptions), options);
20
+ const secret = privateKey !== null && privateKey !== void 0 ? privateKey : await this.jwtSecretService.getPrivateKey();
21
+ return new Promise((resolve, reject) => {
22
+ jwt.sign(payload, secret, mergedOptions, (err, token) => {
23
+ if (err) {
24
+ reject(err);
25
+ }
26
+ else {
27
+ resolve(token || '');
28
+ }
29
+ });
30
+ });
31
+ }
32
+ async verify(token, options, publicKey) {
33
+ var _a;
34
+ const mergedOptions = Object.assign(Object.assign({}, (_a = this.jwtOptions) === null || _a === void 0 ? void 0 : _a.verifyOptions), options);
35
+ const secret = publicKey !== null && publicKey !== void 0 ? publicKey : await this.jwtSecretService.getPublicKey();
36
+ return new Promise((resolve, reject) => {
37
+ jwt.verify(token, secret, mergedOptions, (err, decoded) => {
38
+ if (err) {
39
+ reject(err);
40
+ }
41
+ else {
42
+ resolve(decoded);
43
+ }
44
+ });
45
+ });
46
+ }
47
+ async decode(token, options) {
48
+ const decoded = jwt.decode(token, options);
49
+ if (!decoded) {
50
+ return undefined;
51
+ }
52
+ return decoded;
53
+ }
54
+ };
55
+ __decorate([
56
+ (0, core_1.Value)('cell.jwt'),
57
+ __metadata("design:type", Object)
58
+ ], JwtServiceImpl.prototype, "jwtOptions", void 0);
59
+ __decorate([
60
+ (0, core_1.Autowired)(jwt_protocol_1.JwtSecretService),
61
+ __metadata("design:type", Object)
62
+ ], JwtServiceImpl.prototype, "jwtSecretService", void 0);
63
+ JwtServiceImpl = __decorate([
64
+ (0, core_1.Component)(jwt_protocol_1.JwtService)
65
+ ], JwtServiceImpl);
66
+ exports.JwtServiceImpl = JwtServiceImpl;
67
+ //# sourceMappingURL=jwt-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt-service.js","sourceRoot":"","sources":["../../src/node/jwt-service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,uCAA2D;AAC3D,iDAAyI;AACzI,oCAAoC;AAG7B,IAAM,cAAc,GAApB,MAAM,cAAc;IAQvB,KAAK,CAAC,IAAI,CAAC,OAAiC,EAAE,OAAqB,EAAE,UAAmB;;QACpF,MAAM,aAAa,mCAAQ,MAAA,IAAI,CAAC,UAAU,0CAAE,WAAW,GAAK,OAAO,CAAE,CAAC;QACtE,MAAM,MAAM,GAAG,UAAU,aAAV,UAAU,cAAV,UAAU,GAAI,MAAM,IAAI,CAAC,gBAAgB,CAAC,aAAa,EAAE,CAAC;QACzE,OAAO,IAAI,OAAO,CAAS,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC3C,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE;gBACpD,IAAI,GAAG,EAAE;oBACL,MAAM,CAAC,GAAG,CAAC,CAAC;iBACf;qBAAM;oBACH,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;iBACxB;YACL,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;IACP,CAAC;IAED,KAAK,CAAC,MAAM,CAAyB,KAAa,EAAE,OAAuB,EAAE,SAA2B;;QACpG,MAAM,aAAa,mCAAQ,MAAA,IAAI,CAAC,UAAU,0CAAE,aAAa,GAAK,OAAO,CAAE,CAAC;QACxE,MAAM,MAAM,GAAG,SAAS,aAAT,SAAS,cAAT,SAAS,GAAI,MAAM,IAAI,CAAC,gBAAgB,CAAC,YAAY,EAAE,CAAC;QACvE,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACtC,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,EAAE;gBACtD,IAAI,GAAG,EAAE;oBACL,MAAM,CAAC,GAAG,CAAC,CAAC;iBACf;qBAAM;oBACH,OAAO,CAAC,OAAY,CAAC,CAAC;iBACzB;YACL,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;IACP,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,KAAa,EAAE,OAAuB;QAC/C,MAAM,OAAO,GAAG,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAC3C,IAAI,CAAC,OAAO,EAAE;YACV,OAAO,SAAS,CAAC;SACpB;QACD,OAAO,OAAO,CAAC;IACnB,CAAC;CAEJ,CAAA;AA1CG;IAAC,IAAA,YAAK,EAAC,UAAU,CAAC;;kDACyB;AAE3C;IAAC,IAAA,gBAAS,EAAC,+BAAgB,CAAC;;wDAC0B;AAN7C,cAAc;IAD1B,IAAA,gBAAS,EAAC,yBAAU,CAAC;GACT,cAAc,CA4C1B;AA5CY,wCAAc"}
@@ -0,0 +1,4 @@
1
+ import './index';
2
+ declare const _default: import("inversify/lib/interfaces/interfaces").interfaces.ContainerModule;
3
+ export default _default;
4
+ //# sourceMappingURL=module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../src/node/module.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,CAAC;;AAEjB,wBAA0B"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const core_1 = require("@celljs/core");
4
+ require("./index");
5
+ exports.default = (0, core_1.autoBind)();
6
+ //# sourceMappingURL=module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.js","sourceRoot":"","sources":["../../src/node/module.ts"],"names":[],"mappings":";;AAAA,uCAAwC;AACxC,mBAAiB;AAEjB,kBAAe,IAAA,eAAQ,GAAE,CAAC"}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=package.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package.spec.d.ts","sourceRoot":"","sources":["../src/package.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ describe('jwt package', () => {
2
+ it('support code coverage statistics', () => { });
3
+ });
4
+ //# sourceMappingURL=package.spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package.spec.js","sourceRoot":"","sources":["../src/package.spec.ts"],"names":[],"mappings":"AAAA,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IAEzB,EAAE,CAAC,kCAAkC,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;AACrD,CAAC,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@celljs/jwt",
3
+ "version": "3.0.0",
4
+ "description": "",
5
+ "main": "lib/node/index.js",
6
+ "typings": "lib/node/index.d.ts",
7
+ "dependencies": {
8
+ "@celljs/core": "3.0.0",
9
+ "jsonwebtoken": "^9.0.0"
10
+ },
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "keywords": [
15
+ "cell-component"
16
+ ],
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/cellbang/cell.git"
21
+ },
22
+ "bugs": {
23
+ "url": "https://github.com/cellbang/cell/issues"
24
+ },
25
+ "homepage": "https://github.com/cellbang/cell",
26
+ "files": [
27
+ "lib",
28
+ "src",
29
+ "cell.yml"
30
+ ],
31
+ "scripts": {
32
+ "lint": "cell-component lint",
33
+ "build": "cell-component build",
34
+ "watch": "cell-component watch",
35
+ "clean": "cell-component clean",
36
+ "test": "cell-component test:js"
37
+ },
38
+ "devDependencies": {
39
+ "@celljs/component": "3.0.0",
40
+ "@types/jsonwebtoken": "^8.5.8"
41
+ },
42
+ "gitHead": "c0ef8d269e33a795406bcbb240cac4b845c6c24c"
43
+ }
@@ -0,0 +1,13 @@
1
+ import { CustomError } from '@celljs/core';
2
+
3
+ export class PublicKeyNotFoundError extends CustomError {
4
+ constructor() {
5
+ super('Public key not found');
6
+ }
7
+ }
8
+
9
+ export class PrivateKeyNotFoundError extends CustomError {
10
+ constructor() {
11
+ super('Private key not found');
12
+ }
13
+ }
@@ -0,0 +1,4 @@
1
+ export * from './jwt-protocol';
2
+ export * from './jwt-service';
3
+ export * from './errors';
4
+ export * from './jwt-secret-service';
@@ -0,0 +1,35 @@
1
+ import * as jwt from 'jsonwebtoken';
2
+
3
+ export const JwtService = Symbol('JwtService');
4
+ export const JwtSecretService = Symbol('JwtSecretService');
5
+
6
+ export interface JwtOptions {
7
+ signOptions?: jwt.SignOptions;
8
+ secret?: string | Buffer;
9
+ publicKey?: string | Buffer;
10
+ privateKey?: jwt.Secret;
11
+ verifyOptions?: jwt.VerifyOptions;
12
+ }
13
+
14
+ export interface SignOptions extends jwt.SignOptions {}
15
+
16
+ export interface VerifyOptions extends jwt.VerifyOptions {}
17
+
18
+ export interface DecodeOptions extends jwt.DecodeOptions {}
19
+
20
+ export interface Jwt extends jwt.Jwt {}
21
+
22
+ export interface JwtPayload extends jwt.JwtPayload {}
23
+
24
+ export type Secret = jwt.Secret;
25
+
26
+ export interface JwtService {
27
+ sign(payload: string | Buffer | object, options?: SignOptions, privateKey?: Secret): Promise<string>;
28
+ verify<T extends object = any>(token: string, options?: VerifyOptions, publicKey?: string | Buffer): Promise<T>;
29
+ decode(token: string, options?: DecodeOptions): Promise<JwtPayload | string | undefined>;
30
+ }
31
+
32
+ export interface JwtSecretService {
33
+ getPublicKey(): Promise<string | Buffer>;
34
+ getPrivateKey(): Promise<Secret>;
35
+ }
@@ -0,0 +1,26 @@
1
+ import { Component, Value } from '@celljs/core';
2
+ import { PrivateKeyNotFoundError, PublicKeyNotFoundError } from './errors';
3
+ import { JwtOptions, JwtSecretService, Secret } from './jwt-protocol';
4
+
5
+ @Component(JwtSecretService)
6
+ export class jwtSecretService implements JwtSecretService {
7
+
8
+ @Value('cell.jwt')
9
+ protected readonly jwtOptions?: JwtOptions;
10
+
11
+ async getPublicKey(): Promise<string | Buffer> {
12
+ const secret = this.jwtOptions?.publicKey ?? this.jwtOptions?.secret;
13
+ if (secret) {
14
+ return secret;
15
+ }
16
+ throw new PublicKeyNotFoundError();
17
+ }
18
+
19
+ async getPrivateKey(): Promise<Secret> {
20
+ const secret = this.jwtOptions?.privateKey ?? this.jwtOptions?.secret;
21
+ if (secret) {
22
+ return secret;
23
+ }
24
+ throw new PrivateKeyNotFoundError();
25
+ }
26
+ }
@@ -0,0 +1,50 @@
1
+ import { Autowired, Component, Value } from '@celljs/core';
2
+ import { DecodeOptions, JwtOptions, JwtPayload, JwtSecretService, JwtService, Secret, SignOptions, VerifyOptions } from './jwt-protocol';
3
+ import * as jwt from 'jsonwebtoken';
4
+
5
+ @Component(JwtService)
6
+ export class JwtServiceImpl implements JwtService {
7
+
8
+ @Value('cell.jwt')
9
+ protected readonly jwtOptions?: JwtOptions;
10
+
11
+ @Autowired(JwtSecretService)
12
+ protected readonly jwtSecretService: JwtSecretService;
13
+
14
+ async sign(payload: string | object | Buffer, options?: SignOptions, privateKey?: Secret): Promise<string> {
15
+ const mergedOptions = { ...this.jwtOptions?.signOptions, ...options };
16
+ const secret = privateKey ?? await this.jwtSecretService.getPrivateKey();
17
+ return new Promise<string>((resolve, reject) => {
18
+ jwt.sign(payload, secret, mergedOptions, (err, token) => {
19
+ if (err) {
20
+ reject(err);
21
+ } else {
22
+ resolve(token || '');
23
+ }
24
+ });
25
+ });
26
+ }
27
+
28
+ async verify<T extends object = any>(token: string, options?: VerifyOptions, publicKey?: string | Buffer): Promise<T> {
29
+ const mergedOptions = { ...this.jwtOptions?.verifyOptions, ...options };
30
+ const secret = publicKey ?? await this.jwtSecretService.getPublicKey();
31
+ return new Promise<T>((resolve, reject) => {
32
+ jwt.verify(token, secret, mergedOptions, (err, decoded) => {
33
+ if (err) {
34
+ reject(err);
35
+ } else {
36
+ resolve(decoded as T);
37
+ }
38
+ });
39
+ });
40
+ }
41
+
42
+ async decode(token: string, options?: DecodeOptions): Promise<JwtPayload | string | undefined> {
43
+ const decoded = jwt.decode(token, options);
44
+ if (!decoded) {
45
+ return undefined;
46
+ }
47
+ return decoded;
48
+ }
49
+
50
+ }
@@ -0,0 +1,4 @@
1
+ import { autoBind } from '@celljs/core';
2
+ import './index';
3
+
4
+ export default autoBind();
@@ -0,0 +1,4 @@
1
+ describe('jwt package', () => {
2
+
3
+ it('support code coverage statistics', () => {});
4
+ });