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 | var __param = (this && this.__param) || function (paramIndex, decorator) {
|
12 | return function (target, key) { decorator(target, key, paramIndex); }
|
13 | };
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | exports.JwtService = void 0;
|
16 | const common_1 = require("@nestjs/common");
|
17 | const jwt = require("jsonwebtoken");
|
18 | const interfaces_1 = require("./interfaces");
|
19 | const jwt_constants_1 = require("./jwt.constants");
|
20 | let JwtService = class JwtService {
|
21 | constructor(options = {}) {
|
22 | this.options = options;
|
23 | this.logger = new common_1.Logger('JwtService');
|
24 | }
|
25 | sign(payload, options) {
|
26 | const signOptions = this.mergeJwtOptions(Object.assign({}, options), 'signOptions');
|
27 | const secret = this.getSecretKey(payload, options, 'privateKey', interfaces_1.JwtSecretRequestType.SIGN);
|
28 | return jwt.sign(payload, secret, signOptions);
|
29 | }
|
30 | signAsync(payload, options) {
|
31 | const signOptions = this.mergeJwtOptions(Object.assign({}, options), 'signOptions');
|
32 | const secret = this.getSecretKey(payload, options, 'privateKey', interfaces_1.JwtSecretRequestType.SIGN);
|
33 | return new Promise((resolve, reject) => jwt.sign(payload, secret, signOptions, (err, encoded) => err ? reject(err) : resolve(encoded)));
|
34 | }
|
35 | verify(token, options) {
|
36 | const verifyOptions = this.mergeJwtOptions(Object.assign({}, options), 'verifyOptions');
|
37 | const secret = this.getSecretKey(token, options, 'publicKey', interfaces_1.JwtSecretRequestType.VERIFY);
|
38 | return jwt.verify(token, secret, verifyOptions);
|
39 | }
|
40 | verifyAsync(token, options) {
|
41 | const verifyOptions = this.mergeJwtOptions(Object.assign({}, options), 'verifyOptions');
|
42 | const secret = this.getSecretKey(token, options, 'publicKey', interfaces_1.JwtSecretRequestType.VERIFY);
|
43 | return new Promise((resolve, reject) => jwt.verify(token, secret, verifyOptions, (err, decoded) => err ? reject(err) : resolve(decoded)));
|
44 | }
|
45 | decode(token, options) {
|
46 | return jwt.decode(token, options);
|
47 | }
|
48 | mergeJwtOptions(options, key) {
|
49 | delete options.secret;
|
50 | if (key === 'signOptions') {
|
51 | delete options.privateKey;
|
52 | }
|
53 | else {
|
54 | delete options.publicKey;
|
55 | }
|
56 | return options
|
57 | ? Object.assign(Object.assign({}, (this.options[key] || {})), options) : this.options[key];
|
58 | }
|
59 | getSecretKey(token, options, key, secretRequestType) {
|
60 | var _a, _b;
|
61 | let secret = this.options.secretOrKeyProvider
|
62 | ? this.options.secretOrKeyProvider(secretRequestType, token, options)
|
63 | : (options === null || options === void 0 ? void 0 : options.secret) ||
|
64 | this.options.secret ||
|
65 | (key === 'privateKey'
|
66 | ? ((_a = options) === null || _a === void 0 ? void 0 : _a.privateKey) || this.options.privateKey
|
67 | : ((_b = options) === null || _b === void 0 ? void 0 : _b.publicKey) ||
|
68 | this.options.publicKey) ||
|
69 | this.options[key];
|
70 | if (this.options.secretOrPrivateKey) {
|
71 | this.logger.warn(`"secretOrPrivateKey" has been deprecated, please use the new explicit "secret" or use "secretOrKeyProvider" or "privateKey"/"publicKey" exclusively.`);
|
72 | secret = this.options.secretOrPrivateKey;
|
73 | }
|
74 | return secret;
|
75 | }
|
76 | };
|
77 | JwtService = __decorate([
|
78 | (0, common_1.Injectable)(),
|
79 | __param(0, (0, common_1.Optional)()),
|
80 | __param(0, (0, common_1.Inject)(jwt_constants_1.JWT_MODULE_OPTIONS)),
|
81 | __metadata("design:paramtypes", [Object])
|
82 | ], JwtService);
|
83 | exports.JwtService = JwtService;
|