@citec-spbu/jwt 1.1.0 → 1.1.2
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 +1 -2
- package/dist/index.js +1 -2
- package/dist/interfaces/index.d.ts +1 -0
- package/dist/interfaces/index.js +1 -0
- package/dist/interfaces/jwt-options.interface.d.ts +22 -0
- package/dist/interfaces/jwt-options.interface.js +2 -0
- package/dist/jwt.constants.d.ts +1 -0
- package/dist/jwt.constants.js +4 -0
- package/dist/jwt.module.d.ts +8 -0
- package/dist/jwt.module.js +65 -0
- package/dist/jwt.providers.d.ts +2 -0
- package/dist/jwt.providers.js +7 -0
- package/dist/jwt.service.d.ts +13 -0
- package/dist/jwt.service.js +123 -0
- package/package.json +3 -5
- package/dist/consumer.module.d.ts +0 -6
- package/dist/consumer.module.js +0 -29
- package/dist/issuer.module.d.ts +0 -6
- package/dist/issuer.module.js +0 -30
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -14,6 +14,5 @@ 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
|
-
__exportStar(require("./issuer.module"), exports);
|
|
18
|
-
__exportStar(require("./consumer.module"), exports);
|
|
19
17
|
__exportStar(require("./interfaces"), exports);
|
|
18
|
+
__exportStar(require("./jwt.constants"), exports);
|
package/dist/interfaces/index.js
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ModuleMetadata, Provider, Type } from "@nestjs/common";
|
|
2
|
+
import * as jwt from "jsonwebtoken";
|
|
3
|
+
export interface JwtModuleOptions {
|
|
4
|
+
publicKey?: jwt.Secret;
|
|
5
|
+
privateKey?: jwt.Secret;
|
|
6
|
+
}
|
|
7
|
+
export interface JwtOptionsFactory {
|
|
8
|
+
createJwtOptions(): Promise<JwtModuleOptions> | JwtModuleOptions;
|
|
9
|
+
}
|
|
10
|
+
export interface JwtModuleAsyncOptions extends Pick<ModuleMetadata, "imports"> {
|
|
11
|
+
useExisting?: Type<JwtOptionsFactory>;
|
|
12
|
+
useClass?: Type<JwtOptionsFactory>;
|
|
13
|
+
useFactory?: (...args: any[]) => Promise<JwtModuleOptions> | JwtModuleOptions;
|
|
14
|
+
inject?: any[];
|
|
15
|
+
extraProviders?: Provider[];
|
|
16
|
+
}
|
|
17
|
+
export interface JwtSignOptions extends jwt.SignOptions {
|
|
18
|
+
privateKey?: jwt.Secret;
|
|
19
|
+
}
|
|
20
|
+
export interface JwtVerifyOptions extends jwt.VerifyOptions {
|
|
21
|
+
publicKey?: jwt.Secret;
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const JWT_MODULE_OPTIONS = "JWT_MODULE_OPTIONS";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { DynamicModule } from "@nestjs/common";
|
|
2
|
+
import { JwtModuleAsyncOptions, JwtModuleOptions } from "./interfaces";
|
|
3
|
+
export declare class JwtModule {
|
|
4
|
+
static register(options: JwtModuleOptions): DynamicModule;
|
|
5
|
+
static registerAsync(options: JwtModuleAsyncOptions): DynamicModule;
|
|
6
|
+
private static createAsyncProviders;
|
|
7
|
+
private static createAsyncOptionsProvider;
|
|
8
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
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 JwtModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.JwtModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const jwt_constants_1 = require("./jwt.constants");
|
|
13
|
+
const jwt_providers_1 = require("./jwt.providers");
|
|
14
|
+
const jwt_service_1 = require("./jwt.service");
|
|
15
|
+
let JwtModule = JwtModule_1 = class JwtModule {
|
|
16
|
+
static register(options) {
|
|
17
|
+
return {
|
|
18
|
+
module: JwtModule_1,
|
|
19
|
+
providers: (0, jwt_providers_1.createJwtProvider)(options)
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
static registerAsync(options) {
|
|
23
|
+
return {
|
|
24
|
+
module: JwtModule_1,
|
|
25
|
+
imports: options.imports || [],
|
|
26
|
+
providers: [
|
|
27
|
+
...this.createAsyncProviders(options),
|
|
28
|
+
...(options.extraProviders ?? [])
|
|
29
|
+
]
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
static createAsyncProviders(options) {
|
|
33
|
+
if (options.useExisting || options.useFactory) {
|
|
34
|
+
return [this.createAsyncOptionsProvider(options)];
|
|
35
|
+
}
|
|
36
|
+
return [
|
|
37
|
+
this.createAsyncOptionsProvider(options),
|
|
38
|
+
{
|
|
39
|
+
provide: options.useClass,
|
|
40
|
+
useClass: options.useClass
|
|
41
|
+
}
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
static createAsyncOptionsProvider(options) {
|
|
45
|
+
if (options.useFactory) {
|
|
46
|
+
return {
|
|
47
|
+
provide: jwt_constants_1.JWT_MODULE_OPTIONS,
|
|
48
|
+
useFactory: options.useFactory,
|
|
49
|
+
inject: options.inject || []
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
provide: jwt_constants_1.JWT_MODULE_OPTIONS,
|
|
54
|
+
useFactory: async (optionsFactory) => await optionsFactory.createJwtOptions(),
|
|
55
|
+
inject: [options.useExisting || options.useClass]
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
exports.JwtModule = JwtModule;
|
|
60
|
+
exports.JwtModule = JwtModule = JwtModule_1 = __decorate([
|
|
61
|
+
(0, common_1.Module)({
|
|
62
|
+
providers: [jwt_service_1.JwtService],
|
|
63
|
+
exports: [jwt_service_1.JwtService]
|
|
64
|
+
})
|
|
65
|
+
], JwtModule);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createJwtProvider = createJwtProvider;
|
|
4
|
+
const jwt_constants_1 = require("./jwt.constants");
|
|
5
|
+
function createJwtProvider(options) {
|
|
6
|
+
return [{ provide: jwt_constants_1.JWT_MODULE_OPTIONS, useValue: options || {} }];
|
|
7
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as jwt from "jsonwebtoken";
|
|
2
|
+
import { JwtModuleOptions, JwtSignOptions, JwtVerifyOptions, TokenPayload } from "./interfaces";
|
|
3
|
+
export declare class JwtService {
|
|
4
|
+
private readonly options;
|
|
5
|
+
constructor(options?: JwtModuleOptions);
|
|
6
|
+
sign(payload: TokenPayload, options?: JwtSignOptions): string;
|
|
7
|
+
signAsync(payload: TokenPayload, options?: JwtSignOptions): Promise<string>;
|
|
8
|
+
verify(token: string, options?: JwtVerifyOptions): TokenPayload;
|
|
9
|
+
verifyAsync(token: string, options?: JwtVerifyOptions): Promise<TokenPayload>;
|
|
10
|
+
decode(token: string, options?: jwt.DecodeOptions): TokenPayload;
|
|
11
|
+
private mergeJwtOptions;
|
|
12
|
+
private getSecretKey;
|
|
13
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
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;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
42
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
43
|
+
};
|
|
44
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
45
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.JwtService = void 0;
|
|
49
|
+
const common_1 = require("@nestjs/common");
|
|
50
|
+
const jwt = __importStar(require("jsonwebtoken"));
|
|
51
|
+
const jwt_constants_1 = require("./jwt.constants");
|
|
52
|
+
const SIGN_OPTIONS = {
|
|
53
|
+
algorithm: "RS256",
|
|
54
|
+
issuer: "auth-service",
|
|
55
|
+
audience: "gateway-service",
|
|
56
|
+
keyid: "auth.v1"
|
|
57
|
+
};
|
|
58
|
+
const VERIFY_OPTIONS = {
|
|
59
|
+
algorithms: ["RS256"],
|
|
60
|
+
issuer: "auth-service",
|
|
61
|
+
audience: "gateway-service"
|
|
62
|
+
};
|
|
63
|
+
let JwtService = class JwtService {
|
|
64
|
+
options;
|
|
65
|
+
constructor(options = {}) {
|
|
66
|
+
this.options = options;
|
|
67
|
+
}
|
|
68
|
+
sign(payload, options) {
|
|
69
|
+
const signOptions = this.mergeJwtOptions({ ...options }, "signOptions");
|
|
70
|
+
const privateKey = this.getSecretKey(options, "privateKey");
|
|
71
|
+
if (!privateKey) {
|
|
72
|
+
throw new Error('"privateKey" must be provided for "sign" function');
|
|
73
|
+
}
|
|
74
|
+
return jwt.sign(payload, privateKey, signOptions);
|
|
75
|
+
}
|
|
76
|
+
signAsync(payload, options) {
|
|
77
|
+
const signOptions = this.mergeJwtOptions({ ...options }, "signOptions");
|
|
78
|
+
const privateKey = this.getSecretKey(options, "privateKey");
|
|
79
|
+
if (!privateKey) {
|
|
80
|
+
throw new Error('"privateKey" must be provided for "signAsync" function');
|
|
81
|
+
}
|
|
82
|
+
return new Promise((resolve, reject) => jwt.sign(payload, privateKey, signOptions, (err, token) => err ? reject(err) : resolve(token)));
|
|
83
|
+
}
|
|
84
|
+
verify(token, options) {
|
|
85
|
+
const verifyOptions = this.mergeJwtOptions({ ...options }, "verifyOptions");
|
|
86
|
+
const publicKey = this.getSecretKey(options, "publicKey");
|
|
87
|
+
if (!publicKey) {
|
|
88
|
+
throw new Error('"publicKey" must be provided for "verify" function');
|
|
89
|
+
}
|
|
90
|
+
return jwt.verify(token, publicKey, verifyOptions);
|
|
91
|
+
}
|
|
92
|
+
verifyAsync(token, options) {
|
|
93
|
+
const verifyOptions = this.mergeJwtOptions({ ...options }, "verifyOptions");
|
|
94
|
+
const publicKey = this.getSecretKey(options, "publicKey");
|
|
95
|
+
if (!publicKey) {
|
|
96
|
+
throw new Error('"publicKey" must be provided for "verifyAsync" function');
|
|
97
|
+
}
|
|
98
|
+
return new Promise((resolve, reject) => jwt.verify(token, publicKey, verifyOptions, (err, decoded) => err ? reject(err) : resolve(decoded)));
|
|
99
|
+
}
|
|
100
|
+
decode(token, options) {
|
|
101
|
+
return jwt.decode(token, options);
|
|
102
|
+
}
|
|
103
|
+
mergeJwtOptions(options, key) {
|
|
104
|
+
if (key === "signOptions") {
|
|
105
|
+
return { ...SIGN_OPTIONS, ...options };
|
|
106
|
+
}
|
|
107
|
+
return { ...VERIFY_OPTIONS, ...options };
|
|
108
|
+
}
|
|
109
|
+
getSecretKey(options, key) {
|
|
110
|
+
const secret = (key === "privateKey"
|
|
111
|
+
? options?.privateKey || this.options.privateKey
|
|
112
|
+
: options?.publicKey || this.options.publicKey) ||
|
|
113
|
+
this.options[key];
|
|
114
|
+
return secret;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
exports.JwtService = JwtService;
|
|
118
|
+
exports.JwtService = JwtService = __decorate([
|
|
119
|
+
(0, common_1.Injectable)(),
|
|
120
|
+
__param(0, (0, common_1.Optional)()),
|
|
121
|
+
__param(0, (0, common_1.Inject)(jwt_constants_1.JWT_MODULE_OPTIONS)),
|
|
122
|
+
__metadata("design:paramtypes", [Object])
|
|
123
|
+
], JwtService);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@citec-spbu/jwt",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Lightweight auth lib",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -16,15 +16,13 @@
|
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@citec-spbu/core": "^1.0.3",
|
|
19
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
19
20
|
"@types/node": "^25.0.8",
|
|
20
21
|
"prettier": "^3.7.4",
|
|
21
22
|
"typescript": "^5.9.3"
|
|
22
23
|
},
|
|
23
24
|
"dependencies": {
|
|
24
25
|
"@nestjs/common": "^11.1.12",
|
|
25
|
-
"
|
|
26
|
-
"@nestjs/jwt": "^11.0.2",
|
|
27
|
-
"reflect-metadata": "^0.2.2",
|
|
28
|
-
"rxjs": "^7.8.2"
|
|
26
|
+
"jsonwebtoken": "^9.0.3"
|
|
29
27
|
}
|
|
30
28
|
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { DynamicModule } from "@nestjs/common";
|
|
2
|
-
export interface ConsumerModuleOptions {
|
|
3
|
-
publicKey: string;
|
|
4
|
-
}
|
|
5
|
-
export declare function createConsumerModule(options: ConsumerModuleOptions): DynamicModule;
|
|
6
|
-
export declare function createConsumerModuleAsync(useFactory: (...args: any[]) => Promise<ConsumerModuleOptions> | ConsumerModuleOptions, inject?: any[]): DynamicModule;
|
package/dist/consumer.module.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createConsumerModule = createConsumerModule;
|
|
4
|
-
exports.createConsumerModuleAsync = createConsumerModuleAsync;
|
|
5
|
-
const jwt_1 = require("@nestjs/jwt");
|
|
6
|
-
const VERIFY_OPTIONS = {
|
|
7
|
-
algorithms: ["RS256"],
|
|
8
|
-
issuer: "auth-service",
|
|
9
|
-
audience: "gateway-service"
|
|
10
|
-
};
|
|
11
|
-
function createConsumerModule(options) {
|
|
12
|
-
const jwtOptions = {
|
|
13
|
-
publicKey: options.publicKey,
|
|
14
|
-
verifyOptions: VERIFY_OPTIONS
|
|
15
|
-
};
|
|
16
|
-
return jwt_1.JwtModule.register(jwtOptions);
|
|
17
|
-
}
|
|
18
|
-
function createConsumerModuleAsync(useFactory, inject) {
|
|
19
|
-
return jwt_1.JwtModule.registerAsync({
|
|
20
|
-
useFactory: async (...args) => {
|
|
21
|
-
const options = await useFactory(...args);
|
|
22
|
-
return {
|
|
23
|
-
publicKey: options.publicKey,
|
|
24
|
-
verifyOptions: VERIFY_OPTIONS
|
|
25
|
-
};
|
|
26
|
-
},
|
|
27
|
-
inject: inject || []
|
|
28
|
-
});
|
|
29
|
-
}
|
package/dist/issuer.module.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { DynamicModule } from "@nestjs/common";
|
|
2
|
-
export interface IssuerModuleOptions {
|
|
3
|
-
privateKey: string;
|
|
4
|
-
}
|
|
5
|
-
export declare function createIssuerModule(options: IssuerModuleOptions): DynamicModule;
|
|
6
|
-
export declare function createIssuerModuleAsync(useFactory: (...args: any[]) => Promise<IssuerModuleOptions> | IssuerModuleOptions, inject?: any[]): DynamicModule;
|
package/dist/issuer.module.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createIssuerModule = createIssuerModule;
|
|
4
|
-
exports.createIssuerModuleAsync = createIssuerModuleAsync;
|
|
5
|
-
const jwt_1 = require("@nestjs/jwt");
|
|
6
|
-
const SIGN_OPTIONS = {
|
|
7
|
-
algorithm: "RS256",
|
|
8
|
-
issuer: "auth-service",
|
|
9
|
-
audience: "gateway-service",
|
|
10
|
-
keyid: "auth.v1"
|
|
11
|
-
};
|
|
12
|
-
function createIssuerModule(options) {
|
|
13
|
-
const jwtOptions = {
|
|
14
|
-
privateKey: options.privateKey,
|
|
15
|
-
signOptions: SIGN_OPTIONS
|
|
16
|
-
};
|
|
17
|
-
return jwt_1.JwtModule.register(jwtOptions);
|
|
18
|
-
}
|
|
19
|
-
function createIssuerModuleAsync(useFactory, inject) {
|
|
20
|
-
return jwt_1.JwtModule.registerAsync({
|
|
21
|
-
useFactory: async (...args) => {
|
|
22
|
-
const options = await useFactory(...args);
|
|
23
|
-
return {
|
|
24
|
-
privateKey: options.privateKey,
|
|
25
|
-
signOptions: SIGN_OPTIONS
|
|
26
|
-
};
|
|
27
|
-
},
|
|
28
|
-
inject: inject || []
|
|
29
|
-
});
|
|
30
|
-
}
|