@nestjs-kitchen/authz 2.0.3 → 2.0.4
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/authz.provider.d.ts +1 -3
- package/dist/authz.provider.js +27 -43
- package/dist/constants.d.ts +6 -8
- package/dist/constants.js +13 -46
- package/dist/errors.d.ts +3 -5
- package/dist/errors.js +29 -44
- package/dist/index.d.ts +5 -21
- package/dist/index.js +16 -45
- package/dist/jwt/extract-jwt.d.ts +4 -7
- package/dist/jwt/extract-jwt.js +136 -152
- package/dist/jwt/index.d.ts +2 -16
- package/dist/jwt/index.js +7 -30
- package/dist/jwt/jwt-authz-als.middleware.d.ts +6 -16
- package/dist/jwt/jwt-authz-als.middleware.js +40 -60
- package/dist/jwt/jwt-authz.guard.d.ts +8 -21
- package/dist/jwt/jwt-authz.guard.js +126 -148
- package/dist/jwt/jwt-authz.interface.d.ts +16 -25
- package/dist/jwt/jwt-authz.interface.js +66 -114
- package/dist/jwt/jwt-authz.module.d.ts +19 -31
- package/dist/jwt/jwt-authz.module.js +240 -242
- package/dist/jwt/jwt-authz.service.d.ts +6 -17
- package/dist/jwt/jwt-authz.service.js +113 -144
- package/dist/jwt/jwt-authz.strategy.d.ts +5 -17
- package/dist/jwt/jwt-authz.strategy.js +127 -150
- package/dist/session/index.d.ts +1 -12
- package/dist/session/index.js +5 -27
- package/dist/session/session-authz-als.middleware.d.ts +5 -12
- package/dist/session/session-authz-als.middleware.js +95 -109
- package/dist/session/session-authz.guard.d.ts +6 -15
- package/dist/session/session-authz.guard.js +96 -116
- package/dist/session/session-authz.interface.d.ts +5 -12
- package/dist/session/session-authz.interface.js +18 -40
- package/dist/session/session-authz.module.d.ts +18 -26
- package/dist/session/session-authz.module.js +231 -237
- package/dist/session/session-authz.service.d.ts +5 -13
- package/dist/session/session-authz.service.js +66 -83
- package/dist/session/session-authz.strategy.d.ts +4 -12
- package/dist/session/session-authz.strategy.js +65 -78
- package/dist/user.decorator.d.ts +3 -6
- package/dist/user.decorator.js +25 -33
- package/dist/utils/cookie-parsers.d.ts +3 -6
- package/dist/utils/cookie-parsers.js +62 -63
- package/dist/utils/create-authz-decorator-factory.d.ts +3 -9
- package/dist/utils/create-authz-decorator-factory.js +29 -50
- package/dist/utils/create-set-cookie-fn.d.ts +3 -9
- package/dist/utils/create-set-cookie-fn.js +21 -42
- package/dist/utils/generics.d.ts +4 -6
- package/dist/utils/generics.js +31 -54
- package/dist/utils/get-allow-anonymous.d.ts +2 -9
- package/dist/utils/get-allow-anonymous.js +6 -27
- package/dist/utils/get-als-store.d.ts +2 -5
- package/dist/utils/get-als-store.js +10 -32
- package/dist/utils/get-context-authz-meta-params-list.d.ts +2 -9
- package/dist/utils/get-context-authz-meta-params-list.js +15 -33
- package/dist/utils/get-passport-property.d.ts +1 -3
- package/dist/utils/get-passport-property.js +7 -28
- package/dist/utils/index.d.ts +11 -16
- package/dist/utils/index.js +26 -40
- package/dist/utils/merge-dynamic-module-configs.d.ts +2 -5
- package/dist/utils/merge-dynamic-module-configs.js +13 -38
- package/dist/utils/msgpackrs.d.ts +2 -4
- package/dist/utils/msgpackrs.js +9 -32
- package/dist/utils/types.d.ts +23 -26
- package/dist/utils/types.js +2 -15
- package/package.json +4 -4
|
@@ -1,261 +1,259 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
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;
|
|
8
7
|
};
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
16
10
|
};
|
|
17
|
-
var
|
|
18
|
-
|
|
19
|
-
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
20
|
-
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
21
|
-
if (decorator = decorators[i])
|
|
22
|
-
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
23
|
-
if (kind && result) __defProp(target, key, result);
|
|
24
|
-
return result;
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
25
13
|
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
var import_jwt_authz = require("./jwt-authz.guard");
|
|
40
|
-
var import_jwt_authz2 = require("./jwt-authz.interface");
|
|
41
|
-
var import_jwt_authz3 = require("./jwt-authz.service");
|
|
42
|
-
var import_jwt_authz4 = require("./jwt-authz.strategy");
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.createJwtAuthzModule = void 0;
|
|
16
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const uid_1 = require("uid");
|
|
19
|
+
const constants_1 = require("../constants");
|
|
20
|
+
const errors_1 = require("../errors");
|
|
21
|
+
const utils_1 = require("../utils");
|
|
22
|
+
const jwt_authz_als_middleware_1 = require("./jwt-authz-als.middleware");
|
|
23
|
+
const jwt_authz_guard_1 = require("./jwt-authz.guard");
|
|
24
|
+
const jwt_authz_interface_1 = require("./jwt-authz.interface");
|
|
25
|
+
const jwt_authz_service_1 = require("./jwt-authz.service");
|
|
26
|
+
const jwt_authz_strategy_1 = require("./jwt-authz.strategy");
|
|
43
27
|
const store = {
|
|
44
|
-
|
|
28
|
+
globalInited: 0
|
|
45
29
|
};
|
|
46
|
-
const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, ASYNC_OPTIONS_TYPE, OPTIONS_TYPE } = new
|
|
47
|
-
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
|
|
30
|
+
const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, ASYNC_OPTIONS_TYPE, OPTIONS_TYPE } = new common_1.ConfigurableModuleBuilder({
|
|
31
|
+
moduleName: 'JwtAuthModule'
|
|
32
|
+
})
|
|
33
|
+
.setFactoryMethodName('createJwtAuthzModuleOptions')
|
|
34
|
+
.setExtras({
|
|
35
|
+
authzProvider: undefined,
|
|
51
36
|
global: false
|
|
52
|
-
|
|
53
|
-
(definition, extras) => {
|
|
37
|
+
}, (definition, extras) => {
|
|
54
38
|
const { authzProvider, global } = extras;
|
|
55
39
|
if (!authzProvider) {
|
|
56
|
-
|
|
40
|
+
throw new errors_1.AuthzError(`InternalError: Missing parameter 'authzProvider' in configuration.`);
|
|
57
41
|
}
|
|
58
|
-
const routes = (0,
|
|
59
|
-
const excludes = (0,
|
|
42
|
+
const routes = (0, utils_1.normalizedArray)(extras.routes) ?? [];
|
|
43
|
+
const excludes = (0, utils_1.normalizedArray)(extras.excludes) ?? [];
|
|
60
44
|
if (!global && !routes.length) {
|
|
61
|
-
|
|
45
|
+
throw new errors_1.AuthzError(`InternalError: Missing parameter 'global' or 'routes' in configuration.`);
|
|
62
46
|
}
|
|
63
47
|
if (store.globalInited) {
|
|
64
|
-
|
|
65
|
-
`InternalError: Cannot initialize mutiple global modules. Only one global module is allowed.`
|
|
66
|
-
);
|
|
48
|
+
throw new errors_1.AuthzError(`InternalError: Cannot initialize mutiple global modules. Only one global module is allowed.`);
|
|
67
49
|
}
|
|
68
50
|
if (global) {
|
|
69
|
-
|
|
51
|
+
store.globalInited += 1;
|
|
70
52
|
}
|
|
71
|
-
return (0,
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
53
|
+
return (0, utils_1.mergeDynamicModuleConfigs)(definition, {
|
|
54
|
+
global,
|
|
55
|
+
providers: [
|
|
56
|
+
{
|
|
57
|
+
provide: constants_1.ROUTES_OPTIONS,
|
|
58
|
+
useValue: {
|
|
59
|
+
global,
|
|
60
|
+
excludes,
|
|
61
|
+
routes
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
],
|
|
65
|
+
exports: []
|
|
84
66
|
});
|
|
85
|
-
|
|
86
|
-
|
|
67
|
+
})
|
|
68
|
+
.build();
|
|
69
|
+
/**
|
|
70
|
+
* Creates a JWT module along with its associated guard and service,
|
|
71
|
+
* with types inferred from the provided implementation of `AuthzProviderClass`.
|
|
72
|
+
*
|
|
73
|
+
* @param authzProvider - The implementation class of `AuthzProviderClass`
|
|
74
|
+
* @returns \{AuthzModule, AuthzGuard, AuthzService}
|
|
75
|
+
*/
|
|
87
76
|
const createJwtAuthzModule = (authzProvider) => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
AUTHZ_PROVIDER,
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
);
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
77
|
+
// prevent token overriding
|
|
78
|
+
const id = `${constants_1.PREFIX}${(0, uid_1.uid)()}`;
|
|
79
|
+
// strategy tokens
|
|
80
|
+
const JWT_STRATEGY = `${id}_JWT_STRATEGY`;
|
|
81
|
+
const JWT_REFRESH_STRATEGY = `${id}_REFRESH_STRATEGY`;
|
|
82
|
+
// provider tokens
|
|
83
|
+
const AUTHZ_PROVIDER = `${id}_AUTHZ_PROVIDER`;
|
|
84
|
+
const ALS_PROVIDER = `${id}_ALS_PROVIDER`;
|
|
85
|
+
const JWT_AUTHZ_OPTIONS = `${id}_JWT_AUTHZ_OPTIONS`;
|
|
86
|
+
// meta keys
|
|
87
|
+
const JWT_META_KEY = `${id}_JWT_META_KEY`;
|
|
88
|
+
const JWT_REFRESH_META_KEY = `${id}_REFRESH_META_KEY`;
|
|
89
|
+
// providers
|
|
90
|
+
const JwtAuthzService = (0, jwt_authz_service_1.createJwtAuthzService)([AUTHZ_PROVIDER, JWT_AUTHZ_OPTIONS, ALS_PROVIDER]);
|
|
91
|
+
const JwtAuthzAlsMiddleware = (0, jwt_authz_als_middleware_1.createJwtAuthzAlsMiddleware)([ALS_PROVIDER, JWT_AUTHZ_OPTIONS]);
|
|
92
|
+
const als = new node_async_hooks_1.AsyncLocalStorage();
|
|
93
|
+
// strategy
|
|
94
|
+
const JwtStrategy = (0, jwt_authz_strategy_1.createJwtStrategy)([JWT_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]);
|
|
95
|
+
const RefreshStrategy = (0, jwt_authz_strategy_1.createRefreshStrategy)([JWT_REFRESH_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]);
|
|
96
|
+
// each strategy can be only registered once in passport.
|
|
97
|
+
// no need to provide multiple times as
|
|
98
|
+
// 1. they use the same ALS and authzProvider instance.
|
|
99
|
+
// 2. guard use strategy through passport via strategy name.
|
|
100
|
+
let isStrategyInited = false;
|
|
101
|
+
// guards
|
|
102
|
+
const RefreshAuthzGuard = (0, jwt_authz_guard_1.createJwtRefreshAuthzGuard)([JWT_REFRESH_STRATEGY, JWT_AUTHZ_OPTIONS]);
|
|
103
|
+
const JwtAuthzGuard = (0, jwt_authz_guard_1.createJwtAuthzGuard)([
|
|
104
|
+
JWT_STRATEGY,
|
|
105
|
+
AUTHZ_PROVIDER,
|
|
106
|
+
JWT_AUTHZ_OPTIONS,
|
|
107
|
+
ALS_PROVIDER,
|
|
108
|
+
JWT_META_KEY,
|
|
109
|
+
JWT_REFRESH_META_KEY
|
|
110
|
+
]);
|
|
111
|
+
const Verify = (0, utils_1.createAuthzDecoratorFactory)(JWT_META_KEY);
|
|
112
|
+
const NoVerify = () => {
|
|
113
|
+
return (0, common_1.SetMetadata)(JWT_META_KEY, {
|
|
114
|
+
options: { public: true, override: true }
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
const Refresh = () => {
|
|
118
|
+
return (0, common_1.applyDecorators)(JwtAuthzGuard.NoVerify(), (0, common_1.SetMetadata)(JWT_REFRESH_META_KEY, true), (0, common_1.UseGuards)(RefreshAuthzGuard));
|
|
119
|
+
};
|
|
120
|
+
const Apply = (...rest) => {
|
|
121
|
+
return (0, common_1.applyDecorators)(JwtAuthzGuard.Verify(...rest), (0, common_1.UseGuards)(JwtAuthzGuard));
|
|
122
|
+
};
|
|
123
|
+
JwtAuthzGuard.Verify = Verify;
|
|
124
|
+
JwtAuthzGuard.NoVerify = NoVerify;
|
|
125
|
+
JwtAuthzGuard.Refresh = Refresh;
|
|
126
|
+
JwtAuthzGuard.Apply = Apply;
|
|
127
|
+
const getCommonConfigs = () => {
|
|
128
|
+
const configs = {
|
|
129
|
+
providers: [
|
|
130
|
+
{
|
|
131
|
+
provide: AUTHZ_PROVIDER,
|
|
132
|
+
useClass: authzProvider
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
provide: ALS_PROVIDER,
|
|
136
|
+
useValue: als
|
|
137
|
+
},
|
|
138
|
+
...(!isStrategyInited ? [JwtStrategy, RefreshStrategy] : []),
|
|
139
|
+
JwtAuthzService
|
|
140
|
+
],
|
|
141
|
+
exports: [AUTHZ_PROVIDER, ALS_PROVIDER, JWT_AUTHZ_OPTIONS, JwtAuthzService]
|
|
142
|
+
};
|
|
143
|
+
isStrategyInited = true;
|
|
144
|
+
return configs;
|
|
145
|
+
};
|
|
146
|
+
let JwtAuthzModule = class JwtAuthzModule extends ConfigurableModuleClass {
|
|
147
|
+
/**
|
|
148
|
+
* Configures authz module.
|
|
149
|
+
*/
|
|
150
|
+
static register(options) {
|
|
151
|
+
const jwtAuthzOptions = (0, jwt_authz_interface_1.normalizedJwtAuthzModuleOptions)(options);
|
|
152
|
+
return (0, utils_1.mergeDynamicModuleConfigs)(super.register({ ...options, authzProvider }), getCommonConfigs(), {
|
|
153
|
+
providers: [
|
|
154
|
+
{
|
|
155
|
+
provide: JWT_AUTHZ_OPTIONS,
|
|
156
|
+
useValue: jwtAuthzOptions
|
|
157
|
+
}
|
|
158
|
+
]
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Configures authz module asynchronously.
|
|
163
|
+
*/
|
|
164
|
+
static registerAsync(options) {
|
|
165
|
+
return (0, utils_1.mergeDynamicModuleConfigs)(super.registerAsync({ ...options, authzProvider }), getCommonConfigs(), {
|
|
166
|
+
providers: [
|
|
167
|
+
{
|
|
168
|
+
provide: JWT_AUTHZ_OPTIONS,
|
|
169
|
+
useFactory: (moduleOptions) => {
|
|
170
|
+
const jwtAuthzOptions = (0, jwt_authz_interface_1.normalizedJwtAuthzModuleOptions)(moduleOptions);
|
|
171
|
+
return jwtAuthzOptions;
|
|
172
|
+
},
|
|
173
|
+
inject: [MODULE_OPTIONS_TOKEN]
|
|
174
|
+
}
|
|
175
|
+
]
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
constructor(routesOpt) {
|
|
179
|
+
super();
|
|
180
|
+
this.routesOpt = routesOpt;
|
|
181
|
+
}
|
|
182
|
+
configure(consumer) {
|
|
183
|
+
consumer
|
|
184
|
+
.apply(JwtAuthzAlsMiddleware)
|
|
185
|
+
.exclude(...this.routesOpt.excludes)
|
|
186
|
+
// nestjs v11 will be compatible with splat wildcard.
|
|
187
|
+
.forRoutes(...(this.routesOpt.global ? ['*'] : this.routesOpt.routes));
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
JwtAuthzModule = __decorate([
|
|
191
|
+
(0, common_1.Module)({}),
|
|
192
|
+
__param(0, (0, common_1.Inject)(constants_1.ROUTES_OPTIONS)),
|
|
193
|
+
__metadata("design:paramtypes", [Object])
|
|
194
|
+
], JwtAuthzModule);
|
|
195
|
+
return {
|
|
196
|
+
/**
|
|
197
|
+
* A dynamic module used to configure JWT based authentication and authorization features for the application.
|
|
198
|
+
*
|
|
199
|
+
* This module can be configured using 2 static methods:
|
|
200
|
+
*
|
|
201
|
+
* - `register`
|
|
202
|
+
* - `registerAsync`
|
|
203
|
+
*
|
|
204
|
+
* ### Usage
|
|
205
|
+
*
|
|
206
|
+
* ```typescript
|
|
207
|
+
* @Module({
|
|
208
|
+
* imports: [
|
|
209
|
+
* // Import and configure JWT strategy
|
|
210
|
+
* AuthzModule.register({
|
|
211
|
+
* jwt: {
|
|
212
|
+
* jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
213
|
+
* secret: '1234567890',
|
|
214
|
+
* algorithm: 'HS256'
|
|
215
|
+
* },
|
|
216
|
+
* // Enable refresh token handling
|
|
217
|
+
* refresh: {
|
|
218
|
+
* jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
219
|
+
* secret: '0987654321',
|
|
220
|
+
* algorithm: 'HS256'
|
|
221
|
+
* },
|
|
222
|
+
* // Apply strategy to specific controllers.
|
|
223
|
+
* routes: [BusinessController]
|
|
224
|
+
* })
|
|
225
|
+
* ],
|
|
226
|
+
* controllers: [BusinessController]
|
|
227
|
+
* })
|
|
228
|
+
* export class BusinessModule {}
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
AuthzModule: JwtAuthzModule,
|
|
232
|
+
/**
|
|
233
|
+
* A custom guard that applies authentication to controllers.
|
|
234
|
+
*
|
|
235
|
+
* This guard also provides 4 utility decorators to apply and modify authorization:
|
|
236
|
+
*
|
|
237
|
+
* - `@AuthzGuard.Verify`: Used to verify the user's authorization for specific meta data.
|
|
238
|
+
* - `@AuthzGuard.NoVerify`: Used to `skip` authentication & authorization checks for specific routes.
|
|
239
|
+
* - `@AuthzGuard.Apply`: A simplified version of `@UseGuards(AuthzGuard)` and `@AuthzGuard.Verify`, combining both for convenience.
|
|
240
|
+
* - `@AuthzGuard.Refresh`: Used to ensure that only using refresh token for authentication on specific routes, for refreshing JWT tokens.
|
|
241
|
+
*
|
|
242
|
+
* ### Usage:
|
|
243
|
+
*
|
|
244
|
+
* ```typescript
|
|
245
|
+
* @UseGuards(AuthzGuard)
|
|
246
|
+
* @Controller(// ...)
|
|
247
|
+
* export class BusinessController {
|
|
248
|
+
* // ...
|
|
249
|
+
* }
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
AuthzGuard: JwtAuthzGuard,
|
|
253
|
+
/**
|
|
254
|
+
* A custom servcie to provide methods to handle authentication and authorization.
|
|
255
|
+
*/
|
|
256
|
+
AuthzService: JwtAuthzService
|
|
146
257
|
};
|
|
147
|
-
isStrategyInited = true;
|
|
148
|
-
return configs;
|
|
149
|
-
};
|
|
150
|
-
let JwtAuthzModule = class extends ConfigurableModuleClass {
|
|
151
|
-
constructor(routesOpt) {
|
|
152
|
-
super();
|
|
153
|
-
this.routesOpt = routesOpt;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Configures authz module.
|
|
157
|
-
*/
|
|
158
|
-
static register(options) {
|
|
159
|
-
const jwtAuthzOptions = (0, import_jwt_authz2.normalizedJwtAuthzModuleOptions)(options);
|
|
160
|
-
return (0, import_utils.mergeDynamicModuleConfigs)(super.register({ ...options, authzProvider }), getCommonConfigs(), {
|
|
161
|
-
providers: [
|
|
162
|
-
{
|
|
163
|
-
provide: JWT_AUTHZ_OPTIONS,
|
|
164
|
-
useValue: jwtAuthzOptions
|
|
165
|
-
}
|
|
166
|
-
]
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Configures authz module asynchronously.
|
|
171
|
-
*/
|
|
172
|
-
static registerAsync(options) {
|
|
173
|
-
return (0, import_utils.mergeDynamicModuleConfigs)(super.registerAsync({ ...options, authzProvider }), getCommonConfigs(), {
|
|
174
|
-
providers: [
|
|
175
|
-
{
|
|
176
|
-
provide: JWT_AUTHZ_OPTIONS,
|
|
177
|
-
useFactory: (moduleOptions) => {
|
|
178
|
-
const jwtAuthzOptions = (0, import_jwt_authz2.normalizedJwtAuthzModuleOptions)(moduleOptions);
|
|
179
|
-
return jwtAuthzOptions;
|
|
180
|
-
},
|
|
181
|
-
inject: [MODULE_OPTIONS_TOKEN]
|
|
182
|
-
}
|
|
183
|
-
]
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
configure(consumer) {
|
|
187
|
-
consumer.apply(JwtAuthzAlsMiddleware).exclude(...this.routesOpt.excludes).forRoutes(...this.routesOpt.global ? ["*"] : this.routesOpt.routes);
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
JwtAuthzModule = __decorateClass([
|
|
191
|
-
(0, import_common.Module)({}),
|
|
192
|
-
__decorateParam(0, (0, import_common.Inject)(import_constants.ROUTES_OPTIONS))
|
|
193
|
-
], JwtAuthzModule);
|
|
194
|
-
return {
|
|
195
|
-
/**
|
|
196
|
-
* A dynamic module used to configure JWT based authentication and authorization features for the application.
|
|
197
|
-
*
|
|
198
|
-
* This module can be configured using 2 static methods:
|
|
199
|
-
*
|
|
200
|
-
* - `register`
|
|
201
|
-
* - `registerAsync`
|
|
202
|
-
*
|
|
203
|
-
* ### Usage
|
|
204
|
-
*
|
|
205
|
-
* ```typescript
|
|
206
|
-
* @Module({
|
|
207
|
-
* imports: [
|
|
208
|
-
* // Import and configure JWT strategy
|
|
209
|
-
* AuthzModule.register({
|
|
210
|
-
* jwt: {
|
|
211
|
-
* jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
212
|
-
* secret: '1234567890',
|
|
213
|
-
* algorithm: 'HS256'
|
|
214
|
-
* },
|
|
215
|
-
* // Enable refresh token handling
|
|
216
|
-
* refresh: {
|
|
217
|
-
* jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
218
|
-
* secret: '0987654321',
|
|
219
|
-
* algorithm: 'HS256'
|
|
220
|
-
* },
|
|
221
|
-
* // Apply strategy to specific controllers.
|
|
222
|
-
* routes: [BusinessController]
|
|
223
|
-
* })
|
|
224
|
-
* ],
|
|
225
|
-
* controllers: [BusinessController]
|
|
226
|
-
* })
|
|
227
|
-
* export class BusinessModule {}
|
|
228
|
-
* ```
|
|
229
|
-
*/
|
|
230
|
-
AuthzModule: JwtAuthzModule,
|
|
231
|
-
/**
|
|
232
|
-
* A custom guard that applies authentication to controllers.
|
|
233
|
-
*
|
|
234
|
-
* This guard also provides 4 utility decorators to apply and modify authorization:
|
|
235
|
-
*
|
|
236
|
-
* - `@AuthzGuard.Verify`: Used to verify the user's authorization for specific meta data.
|
|
237
|
-
* - `@AuthzGuard.NoVerify`: Used to `skip` authentication & authorization checks for specific routes.
|
|
238
|
-
* - `@AuthzGuard.Apply`: A simplified version of `@UseGuards(AuthzGuard)` and `@AuthzGuard.Verify`, combining both for convenience.
|
|
239
|
-
* - `@AuthzGuard.Refresh`: Used to ensure that only using refresh token for authentication on specific routes, for refreshing JWT tokens.
|
|
240
|
-
*
|
|
241
|
-
* ### Usage:
|
|
242
|
-
*
|
|
243
|
-
* ```typescript
|
|
244
|
-
* @UseGuards(AuthzGuard)
|
|
245
|
-
* @Controller(// ...)
|
|
246
|
-
* export class BusinessController {
|
|
247
|
-
* // ...
|
|
248
|
-
* }
|
|
249
|
-
* ```
|
|
250
|
-
*/
|
|
251
|
-
AuthzGuard: JwtAuthzGuard,
|
|
252
|
-
/**
|
|
253
|
-
* A custom servcie to provide methods to handle authentication and authorization.
|
|
254
|
-
*/
|
|
255
|
-
AuthzService: JwtAuthzService
|
|
256
|
-
};
|
|
257
258
|
};
|
|
258
|
-
|
|
259
|
-
0 && (module.exports = {
|
|
260
|
-
createJwtAuthzModule
|
|
261
|
-
});
|
|
259
|
+
exports.createJwtAuthzModule = createJwtAuthzModule;
|
|
@@ -1,18 +1,9 @@
|
|
|
1
|
-
import * as _nestjs_common from '@nestjs/common';
|
|
2
|
-
import { AuthzProviderClass } from '../authz.provider.js';
|
|
3
|
-
import { CookieOptionsWithSecret, DeepReadonly } from '../utils/types.js';
|
|
4
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import '
|
|
8
|
-
import '
|
|
9
|
-
import
|
|
10
|
-
import 'crypto';
|
|
11
|
-
import 'jsonwebtoken';
|
|
12
|
-
import './extract-jwt.js';
|
|
13
|
-
import 'cookie';
|
|
14
|
-
|
|
15
|
-
declare const createJwtAuthzService: <P = unknown, U = unknown>([AUTHZ_PROVIDER, JWT_AUTHZ_OPTIONS, ALS_PROVIDER]: [any, any, any]) => _nestjs_common.Type<Omit<{
|
|
2
|
+
import { AuthzProviderClass } from '../authz.provider';
|
|
3
|
+
import { type DeepReadonly } from '../utils';
|
|
4
|
+
import type { JwtAlsType } from './jwt-authz-als.middleware';
|
|
5
|
+
import type { JwtAuthzOptions } from './jwt-authz.interface';
|
|
6
|
+
export declare const createJwtAuthzService: <P = unknown, U = unknown>([AUTHZ_PROVIDER, JWT_AUTHZ_OPTIONS, ALS_PROVIDER]: [any, any, any]) => import("@nestjs/common").Type<Omit<{
|
|
16
7
|
readonly authzProvider: AuthzProviderClass<P, U>;
|
|
17
8
|
readonly jwtAuthzOptions: JwtAuthzOptions;
|
|
18
9
|
readonly als: AsyncLocalStorage<JwtAlsType<U>>;
|
|
@@ -44,11 +35,9 @@ declare const createJwtAuthzService: <P = unknown, U = unknown>([AUTHZ_PROVIDER,
|
|
|
44
35
|
/**
|
|
45
36
|
* Sets a secure HTTP cookie with the given name, value, and optional cookie options.
|
|
46
37
|
*/
|
|
47
|
-
setCookie(name: string, value: string, options?: CookieOptionsWithSecret | undefined): void;
|
|
38
|
+
setCookie(name: string, value: string, options?: import("../utils").CookieOptionsWithSecret | undefined): void;
|
|
48
39
|
/**
|
|
49
40
|
* Retrieves the current user associated with the request, if available.
|
|
50
41
|
*/
|
|
51
42
|
getUser(): DeepReadonly<U> | undefined;
|
|
52
43
|
}, "als" | "jwtAuthzOptions" | "authzProvider">>;
|
|
53
|
-
|
|
54
|
-
export { createJwtAuthzService };
|