@nestjs-kitchen/authz 2.0.2 → 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 +29 -48
- package/dist/constants.d.ts +6 -8
- package/dist/constants.js +13 -46
- package/dist/errors.d.ts +3 -5
- package/dist/errors.js +35 -60
- 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 +138 -158
- 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 +44 -81
- package/dist/jwt/jwt-authz.guard.d.ts +8 -21
- package/dist/jwt/jwt-authz.guard.js +130 -175
- package/dist/jwt/jwt-authz.interface.d.ts +16 -25
- package/dist/jwt/jwt-authz.interface.js +69 -92
- package/dist/jwt/jwt-authz.module.d.ts +19 -31
- package/dist/jwt/jwt-authz.module.js +250 -299
- package/dist/jwt/jwt-authz.service.d.ts +6 -17
- package/dist/jwt/jwt-authz.service.js +116 -162
- package/dist/jwt/jwt-authz.strategy.d.ts +5 -17
- package/dist/jwt/jwt-authz.strategy.js +132 -192
- 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 +98 -130
- package/dist/session/session-authz.guard.d.ts +6 -15
- package/dist/session/session-authz.guard.js +98 -135
- package/dist/session/session-authz.interface.d.ts +5 -12
- package/dist/session/session-authz.interface.js +19 -42
- package/dist/session/session-authz.module.d.ts +18 -26
- package/dist/session/session-authz.module.js +241 -290
- package/dist/session/session-authz.service.d.ts +5 -13
- package/dist/session/session-authz.service.js +69 -105
- package/dist/session/session-authz.strategy.d.ts +4 -12
- package/dist/session/session-authz.strategy.js +68 -105
- package/dist/user.decorator.d.ts +3 -6
- package/dist/user.decorator.js +26 -35
- package/dist/utils/cookie-parsers.d.ts +3 -6
- package/dist/utils/cookie-parsers.js +66 -74
- package/dist/utils/create-authz-decorator-factory.d.ts +3 -9
- package/dist/utils/create-authz-decorator-factory.js +32 -54
- package/dist/utils/create-set-cookie-fn.d.ts +3 -9
- package/dist/utils/create-set-cookie-fn.js +23 -45
- package/dist/utils/generics.d.ts +4 -6
- package/dist/utils/generics.js +37 -63
- package/dist/utils/get-allow-anonymous.d.ts +2 -9
- package/dist/utils/get-allow-anonymous.js +7 -29
- package/dist/utils/get-als-store.d.ts +2 -5
- package/dist/utils/get-als-store.js +11 -34
- package/dist/utils/get-context-authz-meta-params-list.d.ts +2 -9
- package/dist/utils/get-context-authz-meta-params-list.js +16 -35
- package/dist/utils/get-passport-property.d.ts +1 -3
- package/dist/utils/get-passport-property.js +8 -30
- 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 +14 -49
- package/dist/utils/msgpackrs.d.ts +2 -4
- package/dist/utils/msgpackrs.js +11 -35
- package/dist/utils/types.d.ts +23 -26
- package/dist/utils/types.js +2 -15
- package/package.json +7 -7
|
@@ -1,26 +1,15 @@
|
|
|
1
|
-
import './extract-jwt.js';
|
|
2
|
-
import * as _nestjs_core from '@nestjs/core';
|
|
3
|
-
import { JwtOptions, JwtAuthzModuleOptions, JwtAuthzOptions } from './jwt-authz.interface.js';
|
|
4
|
-
import { AuthzProviderClass } from '../authz.provider.js';
|
|
5
|
-
import { AbstractConstructor, RoutesOptions, AuthzModuleBaseOptions, AuthzModuleRoutesOptions, AuthzDecoParams, MethodParameters, ApplyDecorators, CookieOptionsWithSecret, DeepReadonly } from '../utils/types.js';
|
|
6
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
7
|
-
import
|
|
8
|
-
import {
|
|
9
|
-
import { AuthzError } from '../errors
|
|
10
|
-
import {
|
|
11
|
-
import '
|
|
12
|
-
import '
|
|
13
|
-
import
|
|
14
|
-
import '@nestjs/common/interfaces';
|
|
15
|
-
import 'express';
|
|
16
|
-
import '../constants.js';
|
|
17
|
-
|
|
18
|
-
declare const ASYNC_OPTIONS_TYPE: _nestjs_common.ConfigurableModuleAsyncOptions<JwtAuthzModuleOptions, "createJwtAuthzModuleOptions"> & Partial<{
|
|
2
|
+
import { DynamicModule, MiddlewareConsumer, type Type } from '@nestjs/common';
|
|
3
|
+
import { AuthzProviderClass } from '../authz.provider';
|
|
4
|
+
import { AuthzError } from '../errors';
|
|
5
|
+
import { type AbstractConstructor, type ApplyDecorators, type AuthzModuleRoutesOptions, type RoutesOptions } from '../utils';
|
|
6
|
+
import { type JwtAlsType } from './jwt-authz-als.middleware';
|
|
7
|
+
import { type JwtAuthzModuleOptions, type JwtAuthzOptions } from './jwt-authz.interface';
|
|
8
|
+
declare const ASYNC_OPTIONS_TYPE: import("@nestjs/common").ConfigurableModuleAsyncOptions<JwtAuthzModuleOptions, "createJwtAuthzModuleOptions"> & Partial<{
|
|
19
9
|
authzProvider?: Type<AuthzProviderClass<unknown, unknown>>;
|
|
20
|
-
} & AuthzModuleRoutesOptions
|
|
21
|
-
|
|
22
|
-
jwt
|
|
23
|
-
refresh?: JwtOptions;
|
|
10
|
+
} & AuthzModuleRoutesOptions>, OPTIONS_TYPE: Partial<import("../utils").AuthzModuleBaseOptions> & {
|
|
11
|
+
jwt: import("./jwt-authz.interface").JwtOptions;
|
|
12
|
+
refresh?: import("./jwt-authz.interface").JwtOptions;
|
|
24
13
|
} & Partial<{
|
|
25
14
|
authzProvider?: Type<AuthzProviderClass<unknown, unknown>>;
|
|
26
15
|
} & AuthzModuleRoutesOptions>;
|
|
@@ -31,7 +20,7 @@ declare const OPTIONS_TYPE: Partial<AuthzModuleBaseOptions> & {
|
|
|
31
20
|
* @param authzProvider - The implementation class of `AuthzProviderClass`
|
|
32
21
|
* @returns \{AuthzModule, AuthzGuard, AuthzService}
|
|
33
22
|
*/
|
|
34
|
-
declare const createJwtAuthzModule: <P, U, T extends AuthzProviderClass<P, U>>(authzProvider: AbstractConstructor<T, P, U>) => {
|
|
23
|
+
export declare const createJwtAuthzModule: <P, U, T extends AuthzProviderClass<P, U>>(authzProvider: AbstractConstructor<T, P, U>) => {
|
|
35
24
|
/**
|
|
36
25
|
* A dynamic module used to configure JWT based authentication and authorization features for the application.
|
|
37
26
|
*
|
|
@@ -103,7 +92,7 @@ declare const createJwtAuthzModule: <P, U, T extends AuthzProviderClass<P, U>>(a
|
|
|
103
92
|
* ```
|
|
104
93
|
*/
|
|
105
94
|
AuthzGuard: Type<Omit<{
|
|
106
|
-
readonly reflector:
|
|
95
|
+
readonly reflector: import("@nestjs/core").Reflector;
|
|
107
96
|
readonly authzProvider: AuthzProviderClass<unknown, unknown>;
|
|
108
97
|
readonly jwtAuthzOptions: JwtAuthzOptions;
|
|
109
98
|
readonly als: AsyncLocalStorage<JwtAlsType<unknown>>;
|
|
@@ -112,11 +101,11 @@ declare const createJwtAuthzModule: <P, U, T extends AuthzProviderClass<P, U>>(a
|
|
|
112
101
|
session: boolean;
|
|
113
102
|
};
|
|
114
103
|
handleRequest<T_1>(_err: unknown, user: T_1, info?: AuthzError): T_1;
|
|
115
|
-
canActivate(context:
|
|
104
|
+
canActivate(context: import("@nestjs/common").ExecutionContext): Promise<boolean>;
|
|
116
105
|
logIn<TRequest extends {
|
|
117
106
|
logIn: Function;
|
|
118
107
|
} = any>(request: TRequest): Promise<void>;
|
|
119
|
-
getRequest(context:
|
|
108
|
+
getRequest(context: import("@nestjs/common").ExecutionContext): any;
|
|
120
109
|
}, "als" | "jwtAuthzOptions" | "reflector" | "authzProvider">> & {
|
|
121
110
|
/**
|
|
122
111
|
* Verifies the user's authorization for specific meta data.
|
|
@@ -135,7 +124,7 @@ declare const createJwtAuthzModule: <P, U, T extends AuthzProviderClass<P, U>>(a
|
|
|
135
124
|
* }
|
|
136
125
|
* ```
|
|
137
126
|
*/
|
|
138
|
-
Verify: (...args: AuthzDecoParams<MethodParameters<T, "authorize">[1]>) => ApplyDecorators;
|
|
127
|
+
Verify: (...args: import("../utils").AuthzDecoParams<import("../utils").MethodParameters<T, "authorize">[1]>) => ApplyDecorators;
|
|
139
128
|
/**
|
|
140
129
|
* Skips authentication & authorization checks for specific routes.
|
|
141
130
|
*
|
|
@@ -188,7 +177,7 @@ declare const createJwtAuthzModule: <P, U, T extends AuthzProviderClass<P, U>>(a
|
|
|
188
177
|
* }
|
|
189
178
|
* ```
|
|
190
179
|
*/
|
|
191
|
-
Apply: (...rest: Parameters<(...args: AuthzDecoParams<MethodParameters<T, "authorize">[1]>) => ApplyDecorators>) => <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
180
|
+
Apply: (...rest: Parameters<(...args: import("../utils").AuthzDecoParams<import("../utils").MethodParameters<T, "authorize">[1]>) => ApplyDecorators>) => <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
192
181
|
};
|
|
193
182
|
/**
|
|
194
183
|
* A custom servcie to provide methods to handle authentication and authorization.
|
|
@@ -207,9 +196,8 @@ declare const createJwtAuthzModule: <P, U, T extends AuthzProviderClass<P, U>>(a
|
|
|
207
196
|
refresh(user?: U | undefined): Promise<{
|
|
208
197
|
token: string;
|
|
209
198
|
} | undefined>;
|
|
210
|
-
setCookie(name: string, value: string, options?: CookieOptionsWithSecret | undefined): void;
|
|
211
|
-
getUser(): DeepReadonly<U> | undefined;
|
|
199
|
+
setCookie(name: string, value: string, options?: import("../utils").CookieOptionsWithSecret | undefined): void;
|
|
200
|
+
getUser(): import("../utils").DeepReadonly<U> | undefined;
|
|
212
201
|
}, "als" | "jwtAuthzOptions" | "authzProvider">>;
|
|
213
202
|
};
|
|
214
|
-
|
|
215
|
-
export { createJwtAuthzModule };
|
|
203
|
+
export {};
|
|
@@ -1,308 +1,259 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__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;
|
|
10
7
|
};
|
|
11
|
-
var
|
|
12
|
-
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
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);
|
|
18
10
|
};
|
|
19
|
-
var
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
var import_jwt_authz3 = require("./jwt-authz.service");
|
|
36
|
-
var import_jwt_authz4 = require("./jwt-authz.strategy");
|
|
37
|
-
function _ts_decorate(decorators, target, key, desc) {
|
|
38
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
39
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
40
|
-
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;
|
|
41
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
42
|
-
}
|
|
43
|
-
__name(_ts_decorate, "_ts_decorate");
|
|
44
|
-
function _ts_metadata(k, v) {
|
|
45
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
46
|
-
}
|
|
47
|
-
__name(_ts_metadata, "_ts_metadata");
|
|
48
|
-
function _ts_param(paramIndex, decorator) {
|
|
49
|
-
return function(target, key) {
|
|
50
|
-
decorator(target, key, paramIndex);
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
__name(_ts_param, "_ts_param");
|
|
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.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");
|
|
54
27
|
const store = {
|
|
55
|
-
|
|
28
|
+
globalInited: 0
|
|
56
29
|
};
|
|
57
|
-
const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, ASYNC_OPTIONS_TYPE, OPTIONS_TYPE } = new
|
|
58
|
-
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
|
|
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,
|
|
36
|
+
global: false
|
|
62
37
|
}, (definition, extras) => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
const routes = (0, import_utils.normalizedArray)(extras.routes) ?? [];
|
|
68
|
-
const excludes = (0, import_utils.normalizedArray)(extras.excludes) ?? [];
|
|
69
|
-
if (!global && !routes.length) {
|
|
70
|
-
throw new import_errors.AuthzError(`InternalError: Missing parameter 'global' or 'routes' in configuration.`);
|
|
71
|
-
}
|
|
72
|
-
if (store.globalInited) {
|
|
73
|
-
throw new import_errors.AuthzError(`InternalError: Cannot initialize mutiple global modules. Only one global module is allowed.`);
|
|
74
|
-
}
|
|
75
|
-
if (global) {
|
|
76
|
-
store.globalInited += 1;
|
|
77
|
-
}
|
|
78
|
-
return (0, import_utils.mergeDynamicModuleConfigs)(definition, {
|
|
79
|
-
global,
|
|
80
|
-
providers: [
|
|
81
|
-
{
|
|
82
|
-
provide: import_constants.ROUTES_OPTIONS,
|
|
83
|
-
useValue: {
|
|
84
|
-
global,
|
|
85
|
-
excludes,
|
|
86
|
-
routes
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
],
|
|
90
|
-
exports: []
|
|
91
|
-
});
|
|
92
|
-
}).build();
|
|
93
|
-
const createJwtAuthzModule = /* @__PURE__ */ __name((authzProvider) => {
|
|
94
|
-
var _a;
|
|
95
|
-
const id = `${import_constants.PREFIX}${(0, import_uid.uid)()}`;
|
|
96
|
-
const JWT_STRATEGY = `${id}_JWT_STRATEGY`;
|
|
97
|
-
const JWT_REFRESH_STRATEGY = `${id}_REFRESH_STRATEGY`;
|
|
98
|
-
const AUTHZ_PROVIDER = `${id}_AUTHZ_PROVIDER`;
|
|
99
|
-
const ALS_PROVIDER = `${id}_ALS_PROVIDER`;
|
|
100
|
-
const JWT_AUTHZ_OPTIONS = `${id}_JWT_AUTHZ_OPTIONS`;
|
|
101
|
-
const JWT_META_KEY = `${id}_JWT_META_KEY`;
|
|
102
|
-
const JWT_REFRESH_META_KEY = `${id}_REFRESH_META_KEY`;
|
|
103
|
-
const JwtAuthzService = (0, import_jwt_authz3.createJwtAuthzService)([
|
|
104
|
-
AUTHZ_PROVIDER,
|
|
105
|
-
JWT_AUTHZ_OPTIONS,
|
|
106
|
-
ALS_PROVIDER
|
|
107
|
-
]);
|
|
108
|
-
const JwtAuthzAlsMiddleware = (0, import_jwt_authz_als.createJwtAuthzAlsMiddleware)([
|
|
109
|
-
ALS_PROVIDER,
|
|
110
|
-
JWT_AUTHZ_OPTIONS
|
|
111
|
-
]);
|
|
112
|
-
const als = new import_node_async_hooks.AsyncLocalStorage();
|
|
113
|
-
const JwtStrategy = (0, import_jwt_authz4.createJwtStrategy)([
|
|
114
|
-
JWT_STRATEGY,
|
|
115
|
-
AUTHZ_PROVIDER,
|
|
116
|
-
ALS_PROVIDER
|
|
117
|
-
]);
|
|
118
|
-
const RefreshStrategy = (0, import_jwt_authz4.createRefreshStrategy)([
|
|
119
|
-
JWT_REFRESH_STRATEGY,
|
|
120
|
-
AUTHZ_PROVIDER,
|
|
121
|
-
ALS_PROVIDER
|
|
122
|
-
]);
|
|
123
|
-
let isStrategyInited = false;
|
|
124
|
-
const RefreshAuthzGuard = (0, import_jwt_authz.createJwtRefreshAuthzGuard)([
|
|
125
|
-
JWT_REFRESH_STRATEGY,
|
|
126
|
-
JWT_AUTHZ_OPTIONS
|
|
127
|
-
]);
|
|
128
|
-
const JwtAuthzGuard = (0, import_jwt_authz.createJwtAuthzGuard)([
|
|
129
|
-
JWT_STRATEGY,
|
|
130
|
-
AUTHZ_PROVIDER,
|
|
131
|
-
JWT_AUTHZ_OPTIONS,
|
|
132
|
-
ALS_PROVIDER,
|
|
133
|
-
JWT_META_KEY,
|
|
134
|
-
JWT_REFRESH_META_KEY
|
|
135
|
-
]);
|
|
136
|
-
const Verify = (0, import_utils.createAuthzDecoratorFactory)(JWT_META_KEY);
|
|
137
|
-
const NoVerify = /* @__PURE__ */ __name(() => {
|
|
138
|
-
return (0, import_common.SetMetadata)(JWT_META_KEY, {
|
|
139
|
-
options: {
|
|
140
|
-
public: true,
|
|
141
|
-
override: true
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
}, "NoVerify");
|
|
145
|
-
const Refresh = /* @__PURE__ */ __name(() => {
|
|
146
|
-
return (0, import_common.applyDecorators)(JwtAuthzGuard.NoVerify(), (0, import_common.SetMetadata)(JWT_REFRESH_META_KEY, true), (0, import_common.UseGuards)(RefreshAuthzGuard));
|
|
147
|
-
}, "Refresh");
|
|
148
|
-
const Apply = /* @__PURE__ */ __name((...rest) => {
|
|
149
|
-
return (0, import_common.applyDecorators)(JwtAuthzGuard.Verify(...rest), (0, import_common.UseGuards)(JwtAuthzGuard));
|
|
150
|
-
}, "Apply");
|
|
151
|
-
JwtAuthzGuard.Verify = Verify;
|
|
152
|
-
JwtAuthzGuard.NoVerify = NoVerify;
|
|
153
|
-
JwtAuthzGuard.Refresh = Refresh;
|
|
154
|
-
JwtAuthzGuard.Apply = Apply;
|
|
155
|
-
const getCommonConfigs = /* @__PURE__ */ __name(() => {
|
|
156
|
-
const configs = {
|
|
157
|
-
providers: [
|
|
158
|
-
{
|
|
159
|
-
provide: AUTHZ_PROVIDER,
|
|
160
|
-
useClass: authzProvider
|
|
161
|
-
},
|
|
162
|
-
{
|
|
163
|
-
provide: ALS_PROVIDER,
|
|
164
|
-
useValue: als
|
|
165
|
-
},
|
|
166
|
-
...!isStrategyInited ? [
|
|
167
|
-
JwtStrategy,
|
|
168
|
-
RefreshStrategy
|
|
169
|
-
] : [],
|
|
170
|
-
JwtAuthzService
|
|
171
|
-
],
|
|
172
|
-
exports: [
|
|
173
|
-
AUTHZ_PROVIDER,
|
|
174
|
-
ALS_PROVIDER,
|
|
175
|
-
JWT_AUTHZ_OPTIONS,
|
|
176
|
-
JwtAuthzService
|
|
177
|
-
]
|
|
178
|
-
};
|
|
179
|
-
isStrategyInited = true;
|
|
180
|
-
return configs;
|
|
181
|
-
}, "getCommonConfigs");
|
|
182
|
-
let JwtAuthzModule = (_a = class extends ConfigurableModuleClass {
|
|
183
|
-
constructor(routesOpt) {
|
|
184
|
-
super();
|
|
185
|
-
__publicField(this, "routesOpt");
|
|
186
|
-
this.routesOpt = routesOpt;
|
|
38
|
+
const { authzProvider, global } = extras;
|
|
39
|
+
if (!authzProvider) {
|
|
40
|
+
throw new errors_1.AuthzError(`InternalError: Missing parameter 'authzProvider' in configuration.`);
|
|
187
41
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const jwtAuthzOptions = (0, import_jwt_authz2.normalizedJwtAuthzModuleOptions)(options);
|
|
193
|
-
return (0, import_utils.mergeDynamicModuleConfigs)(super.register({
|
|
194
|
-
...options,
|
|
195
|
-
authzProvider
|
|
196
|
-
}), getCommonConfigs(), {
|
|
197
|
-
providers: [
|
|
198
|
-
{
|
|
199
|
-
provide: JWT_AUTHZ_OPTIONS,
|
|
200
|
-
useValue: jwtAuthzOptions
|
|
201
|
-
}
|
|
202
|
-
]
|
|
203
|
-
});
|
|
42
|
+
const routes = (0, utils_1.normalizedArray)(extras.routes) ?? [];
|
|
43
|
+
const excludes = (0, utils_1.normalizedArray)(extras.excludes) ?? [];
|
|
44
|
+
if (!global && !routes.length) {
|
|
45
|
+
throw new errors_1.AuthzError(`InternalError: Missing parameter 'global' or 'routes' in configuration.`);
|
|
204
46
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
*/
|
|
208
|
-
static registerAsync(options) {
|
|
209
|
-
return (0, import_utils.mergeDynamicModuleConfigs)(super.registerAsync({
|
|
210
|
-
...options,
|
|
211
|
-
authzProvider
|
|
212
|
-
}), getCommonConfigs(), {
|
|
213
|
-
providers: [
|
|
214
|
-
{
|
|
215
|
-
provide: JWT_AUTHZ_OPTIONS,
|
|
216
|
-
useFactory: /* @__PURE__ */ __name((moduleOptions) => {
|
|
217
|
-
const jwtAuthzOptions = (0, import_jwt_authz2.normalizedJwtAuthzModuleOptions)(moduleOptions);
|
|
218
|
-
return jwtAuthzOptions;
|
|
219
|
-
}, "useFactory"),
|
|
220
|
-
inject: [
|
|
221
|
-
MODULE_OPTIONS_TOKEN
|
|
222
|
-
]
|
|
223
|
-
}
|
|
224
|
-
]
|
|
225
|
-
});
|
|
47
|
+
if (store.globalInited) {
|
|
48
|
+
throw new errors_1.AuthzError(`InternalError: Cannot initialize mutiple global modules. Only one global module is allowed.`);
|
|
226
49
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
"*"
|
|
230
|
-
] : this.routesOpt.routes);
|
|
50
|
+
if (global) {
|
|
51
|
+
store.globalInited += 1;
|
|
231
52
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
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: []
|
|
66
|
+
});
|
|
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
|
+
*/
|
|
76
|
+
const createJwtAuthzModule = (authzProvider) => {
|
|
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
|
|
257
|
+
};
|
|
258
|
+
};
|
|
259
|
+
exports.createJwtAuthzModule = createJwtAuthzModule;
|