@nestjs-kitchen/authz 2.0.3 → 2.0.5
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 +3 -4
- 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,21 +1,14 @@
|
|
|
1
|
-
import * as _nestjs_core from '@nestjs/core';
|
|
2
|
-
import { SessionAuthzOptions, SessionOptions, SessionAuthzModuleOptions } from './session-authz.interface.js';
|
|
3
|
-
import { AuthzProviderClass } from '../authz.provider.js';
|
|
4
|
-
import { AbstractConstructor, RoutesOptions, AuthzModuleBaseOptions, AuthzModuleRoutesOptions, AuthzDecoParams, MethodParameters, ApplyDecorators, CookieOptionsWithSecret, DeepReadonly } from '../utils/types.js';
|
|
5
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
8
|
-
import { AuthzError } from '../errors
|
|
9
|
-
import {
|
|
10
|
-
import '
|
|
11
|
-
import '
|
|
12
|
-
import
|
|
13
|
-
|
|
14
|
-
declare const ASYNC_OPTIONS_TYPE: _nestjs_common.ConfigurableModuleAsyncOptions<SessionAuthzModuleOptions, "createSessionAuthzModuleOptions"> & 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 AuthzModuleRoutesOptions, type RoutesOptions } from '../utils';
|
|
6
|
+
import { type SessionAlsType } from './session-authz-als.middleware';
|
|
7
|
+
import { type SessionAuthzModuleOptions, type SessionAuthzOptions } from './session-authz.interface';
|
|
8
|
+
declare const ASYNC_OPTIONS_TYPE: import("@nestjs/common").ConfigurableModuleAsyncOptions<SessionAuthzModuleOptions, "createSessionAuthzModuleOptions"> & Partial<{
|
|
15
9
|
authzProvider?: Type<AuthzProviderClass<unknown, unknown>>;
|
|
16
|
-
} & AuthzModuleRoutesOptions
|
|
17
|
-
|
|
18
|
-
session: SessionOptions & {
|
|
10
|
+
} & AuthzModuleRoutesOptions>, OPTIONS_TYPE: Partial<import("../utils").AuthzModuleBaseOptions> & {
|
|
11
|
+
session: import("./session-authz.interface").SessionOptions & {
|
|
19
12
|
keepSessionInfo?: boolean;
|
|
20
13
|
};
|
|
21
14
|
} & Partial<{
|
|
@@ -28,7 +21,7 @@ declare const OPTIONS_TYPE: Partial<AuthzModuleBaseOptions> & {
|
|
|
28
21
|
* @param authzProvider - The implementation class of `AuthzProviderClass`
|
|
29
22
|
* @returns \{AuthzModule, AuthzGuard, AuthzService}
|
|
30
23
|
*/
|
|
31
|
-
declare const cereateSessionAuthzModule: <P, U, T extends AuthzProviderClass<P, U>>(authzProvider: AbstractConstructor<T, P, U>) => {
|
|
24
|
+
export declare const cereateSessionAuthzModule: <P, U, T extends AuthzProviderClass<P, U>>(authzProvider: AbstractConstructor<T, P, U>) => {
|
|
32
25
|
/**
|
|
33
26
|
* A dynamic module used to configure session based authentication and authorization features for the application.
|
|
34
27
|
*
|
|
@@ -97,7 +90,7 @@ declare const cereateSessionAuthzModule: <P, U, T extends AuthzProviderClass<P,
|
|
|
97
90
|
* ```
|
|
98
91
|
*/
|
|
99
92
|
AuthzGuard: Type<Omit<{
|
|
100
|
-
readonly reflector:
|
|
93
|
+
readonly reflector: import("@nestjs/core").Reflector;
|
|
101
94
|
readonly authzProvider: AuthzProviderClass<unknown, unknown>;
|
|
102
95
|
readonly sessionAuthzOptions: SessionAuthzOptions;
|
|
103
96
|
readonly als: AsyncLocalStorage<SessionAlsType<unknown, unknown>>;
|
|
@@ -106,11 +99,11 @@ declare const cereateSessionAuthzModule: <P, U, T extends AuthzProviderClass<P,
|
|
|
106
99
|
session: boolean;
|
|
107
100
|
};
|
|
108
101
|
handleRequest<T_1>(_err: unknown, user: T_1, info?: AuthzError): T_1;
|
|
109
|
-
canActivate(context:
|
|
102
|
+
canActivate(context: import("@nestjs/common").ExecutionContext): Promise<boolean>;
|
|
110
103
|
logIn<TRequest extends {
|
|
111
104
|
logIn: Function;
|
|
112
105
|
} = any>(request: TRequest): Promise<void>;
|
|
113
|
-
getRequest(context:
|
|
106
|
+
getRequest(context: import("@nestjs/common").ExecutionContext): any;
|
|
114
107
|
}, "als" | "reflector" | "authzProvider" | "sessionAuthzOptions">> & {
|
|
115
108
|
/**
|
|
116
109
|
* Verifies the user's authorization for specific meta data.
|
|
@@ -129,7 +122,7 @@ declare const cereateSessionAuthzModule: <P, U, T extends AuthzProviderClass<P,
|
|
|
129
122
|
* }
|
|
130
123
|
* ```
|
|
131
124
|
*/
|
|
132
|
-
Verify: (...args: AuthzDecoParams<MethodParameters<T, "authorize">[1]>) => ApplyDecorators;
|
|
125
|
+
Verify: (...args: import("../utils").AuthzDecoParams<import("../utils").MethodParameters<T, "authorize">[1]>) => import("../utils").ApplyDecorators;
|
|
133
126
|
/**
|
|
134
127
|
* Skips authentication & authorization checks for specific routes.
|
|
135
128
|
*
|
|
@@ -164,7 +157,7 @@ declare const cereateSessionAuthzModule: <P, U, T extends AuthzProviderClass<P,
|
|
|
164
157
|
* }
|
|
165
158
|
* ```
|
|
166
159
|
*/
|
|
167
|
-
Apply: (...rest: Parameters<(...args: AuthzDecoParams<MethodParameters<T, "authorize">[1]>) => ApplyDecorators>) => <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
160
|
+
Apply: (...rest: Parameters<(...args: import("../utils").AuthzDecoParams<import("../utils").MethodParameters<T, "authorize">[1]>) => import("../utils").ApplyDecorators>) => <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
168
161
|
};
|
|
169
162
|
/**
|
|
170
163
|
* A custom servcie to provide methods to handle authentication and authorization.
|
|
@@ -174,9 +167,8 @@ declare const cereateSessionAuthzModule: <P, U, T extends AuthzProviderClass<P,
|
|
|
174
167
|
readonly als: AsyncLocalStorage<SessionAlsType<P, U>>;
|
|
175
168
|
logIn(user: U): Promise<void>;
|
|
176
169
|
logOut(): Promise<void>;
|
|
177
|
-
setCookie(name: string, value: string, options?: CookieOptionsWithSecret | undefined): void;
|
|
178
|
-
getUser(): DeepReadonly<U> | undefined;
|
|
170
|
+
setCookie(name: string, value: string, options?: import("../utils").CookieOptionsWithSecret | undefined): void;
|
|
171
|
+
getUser(): import("../utils").DeepReadonly<U> | undefined;
|
|
179
172
|
}, "als" | "authzProvider">>;
|
|
180
173
|
};
|
|
181
|
-
|
|
182
|
-
export { cereateSessionAuthzModule };
|
|
174
|
+
export {};
|
|
@@ -1,256 +1,250 @@
|
|
|
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
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var __decorateClass = (decorators, target, key, kind) => {
|
|
29
|
-
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
30
|
-
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
31
|
-
if (decorator = decorators[i])
|
|
32
|
-
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
33
|
-
if (kind && result) __defProp(target, key, result);
|
|
34
|
-
return result;
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
35
13
|
};
|
|
36
|
-
var
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
var import_session_authz4 = require("./session-authz.strategy");
|
|
14
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
15
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.cereateSessionAuthzModule = void 0;
|
|
19
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
20
|
+
const common_1 = require("@nestjs/common");
|
|
21
|
+
const express_session_1 = __importDefault(require("express-session"));
|
|
22
|
+
const uid_1 = require("uid");
|
|
23
|
+
const constants_1 = require("../constants");
|
|
24
|
+
const errors_1 = require("../errors");
|
|
25
|
+
const utils_1 = require("../utils");
|
|
26
|
+
const session_authz_als_middleware_1 = require("./session-authz-als.middleware");
|
|
27
|
+
const session_authz_guard_1 = require("./session-authz.guard");
|
|
28
|
+
const session_authz_interface_1 = require("./session-authz.interface");
|
|
29
|
+
const session_authz_service_1 = require("./session-authz.service");
|
|
30
|
+
const session_authz_strategy_1 = require("./session-authz.strategy");
|
|
54
31
|
const store = {
|
|
55
|
-
|
|
32
|
+
globalInited: 0
|
|
56
33
|
};
|
|
57
|
-
const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, ASYNC_OPTIONS_TYPE, OPTIONS_TYPE } = new
|
|
58
|
-
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
|
|
34
|
+
const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, ASYNC_OPTIONS_TYPE, OPTIONS_TYPE } = new common_1.ConfigurableModuleBuilder({
|
|
35
|
+
moduleName: 'SessionAuthModule'
|
|
36
|
+
})
|
|
37
|
+
.setFactoryMethodName('createSessionAuthzModuleOptions')
|
|
38
|
+
.setExtras({
|
|
39
|
+
authzProvider: undefined,
|
|
62
40
|
global: false
|
|
63
|
-
|
|
64
|
-
(definition, extras) => {
|
|
41
|
+
}, (definition, extras) => {
|
|
65
42
|
const { authzProvider, global } = extras;
|
|
66
43
|
if (!authzProvider) {
|
|
67
|
-
|
|
44
|
+
throw new errors_1.AuthzError(`InternalError: Missing parameter 'authzProvider' in configuration.`);
|
|
68
45
|
}
|
|
69
|
-
const routes = (0,
|
|
70
|
-
const excludes = (0,
|
|
46
|
+
const routes = (0, utils_1.normalizedArray)(extras.routes) ?? [];
|
|
47
|
+
const excludes = (0, utils_1.normalizedArray)(extras.excludes) ?? [];
|
|
71
48
|
if (!global && !routes.length) {
|
|
72
|
-
|
|
49
|
+
throw new errors_1.AuthzError(`InternalError: Missing parameter 'global' or 'routes' in configuration.`);
|
|
73
50
|
}
|
|
74
51
|
if (store.globalInited) {
|
|
75
|
-
|
|
76
|
-
`InternalError: Cannot initialize mutiple global modules. Only one global module is allowed.`
|
|
77
|
-
);
|
|
52
|
+
throw new errors_1.AuthzError(`InternalError: Cannot initialize mutiple global modules. Only one global module is allowed.`);
|
|
78
53
|
}
|
|
79
54
|
if (global) {
|
|
80
|
-
|
|
55
|
+
store.globalInited += 1;
|
|
81
56
|
}
|
|
82
|
-
return (0,
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
57
|
+
return (0, utils_1.mergeDynamicModuleConfigs)(definition, {
|
|
58
|
+
global,
|
|
59
|
+
providers: [
|
|
60
|
+
{
|
|
61
|
+
provide: constants_1.ROUTES_OPTIONS,
|
|
62
|
+
useValue: {
|
|
63
|
+
global,
|
|
64
|
+
excludes,
|
|
65
|
+
routes
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
],
|
|
69
|
+
exports: []
|
|
95
70
|
});
|
|
96
|
-
|
|
97
|
-
|
|
71
|
+
})
|
|
72
|
+
.build();
|
|
73
|
+
/**
|
|
74
|
+
* Creates a session module along with its associated guard and service,
|
|
75
|
+
* with types inferred from the provided implementation of `AuthzProviderClass`.
|
|
76
|
+
*
|
|
77
|
+
* @param authzProvider - The implementation class of `AuthzProviderClass`
|
|
78
|
+
* @returns \{AuthzModule, AuthzGuard, AuthzService}
|
|
79
|
+
*/
|
|
98
80
|
const cereateSessionAuthzModule = (authzProvider) => {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
81
|
+
const id = `${constants_1.PREFIX}${(0, uid_1.uid)()}`;
|
|
82
|
+
// strategy tokens
|
|
83
|
+
const SESSION_STRATEGY = `${id}_SESSION_STRATEGY`;
|
|
84
|
+
// provider tokens
|
|
85
|
+
const AUTHZ_PROVIDER = `${id}_AUTHZ_PROVIDER`;
|
|
86
|
+
const ALS_PROVIDER = `${id}_ALS_PROVIDER`;
|
|
87
|
+
const SESSION_AUTHZ_OPTIONS = `${id}_SESSION_AUTHZ_OPTIONS`;
|
|
88
|
+
// meta keys
|
|
89
|
+
const SESSION_META_KEY = `${id}_SESSION_META_KEY`;
|
|
90
|
+
// strategies
|
|
91
|
+
const SessionAuthzStrategy = (0, session_authz_strategy_1.createSessionAuthzStrategy)([SESSION_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]);
|
|
92
|
+
// providers
|
|
93
|
+
const SessionAuthzService = (0, session_authz_service_1.createSessionAuthzService)([AUTHZ_PROVIDER, ALS_PROVIDER]);
|
|
94
|
+
const SessionAuthzAlsMiddleware = (0, session_authz_als_middleware_1.createSessionAuthzAlsMiddleware)([ALS_PROVIDER, SESSION_AUTHZ_OPTIONS]);
|
|
95
|
+
const als = new node_async_hooks_1.AsyncLocalStorage();
|
|
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 SessionAuthzGuard = (0, session_authz_guard_1.createSessionAuthzGuard)([
|
|
103
|
+
SESSION_STRATEGY,
|
|
104
|
+
AUTHZ_PROVIDER,
|
|
105
|
+
SESSION_AUTHZ_OPTIONS,
|
|
106
|
+
ALS_PROVIDER,
|
|
107
|
+
SESSION_META_KEY
|
|
108
|
+
]);
|
|
109
|
+
const Verify = (0, utils_1.createAuthzDecoratorFactory)(SESSION_META_KEY);
|
|
110
|
+
const NoVerify = () => {
|
|
111
|
+
return (0, common_1.SetMetadata)(SESSION_META_KEY, {
|
|
112
|
+
options: { public: true, override: true }
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
const Apply = (...rest) => {
|
|
116
|
+
return (0, common_1.applyDecorators)(SessionAuthzGuard.Verify(...rest), (0, common_1.UseGuards)(SessionAuthzGuard));
|
|
117
|
+
};
|
|
118
|
+
SessionAuthzGuard.Verify = Verify;
|
|
119
|
+
SessionAuthzGuard.NoVerify = NoVerify;
|
|
120
|
+
SessionAuthzGuard.Apply = Apply;
|
|
121
|
+
const getCommonConfigs = () => {
|
|
122
|
+
const configs = {
|
|
123
|
+
providers: [
|
|
124
|
+
{
|
|
125
|
+
provide: AUTHZ_PROVIDER,
|
|
126
|
+
useClass: authzProvider
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
provide: ALS_PROVIDER,
|
|
130
|
+
useValue: als
|
|
131
|
+
},
|
|
132
|
+
...(!isStrategyInited ? [SessionAuthzStrategy] : []),
|
|
133
|
+
SessionAuthzService
|
|
134
|
+
],
|
|
135
|
+
exports: [AUTHZ_PROVIDER, ALS_PROVIDER, SESSION_AUTHZ_OPTIONS, SessionAuthzService]
|
|
136
|
+
};
|
|
137
|
+
isStrategyInited = true;
|
|
138
|
+
return configs;
|
|
139
|
+
};
|
|
140
|
+
let SessionAuthzModule = class SessionAuthzModule extends ConfigurableModuleClass {
|
|
141
|
+
/**
|
|
142
|
+
* Configures authz module.
|
|
143
|
+
*
|
|
144
|
+
* Note: DO NOT register the same routes in multiple session authz modules, or import the same session authz module in the same module multiple times, express-session middleware will not work properly.
|
|
145
|
+
*/
|
|
146
|
+
static register(options) {
|
|
147
|
+
const sessionAuthzOptions = (0, session_authz_interface_1.normalizedSessionAuthzModuleOptions)(options);
|
|
148
|
+
return (0, utils_1.mergeDynamicModuleConfigs)(super.register({ ...options, authzProvider }), getCommonConfigs(), {
|
|
149
|
+
providers: [
|
|
150
|
+
{
|
|
151
|
+
provide: SESSION_AUTHZ_OPTIONS,
|
|
152
|
+
useValue: sessionAuthzOptions
|
|
153
|
+
}
|
|
154
|
+
]
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Configures authz module asynchronously.
|
|
159
|
+
*
|
|
160
|
+
* Note: DO NOT register the same routes in multiple session authz modules, express-session middleware will not work properly.
|
|
161
|
+
*/
|
|
162
|
+
static registerAsync(options) {
|
|
163
|
+
return (0, utils_1.mergeDynamicModuleConfigs)(super.registerAsync({ ...options, authzProvider }), getCommonConfigs(), {
|
|
164
|
+
providers: [
|
|
165
|
+
{
|
|
166
|
+
provide: SESSION_AUTHZ_OPTIONS,
|
|
167
|
+
useFactory: (moduleOptions) => {
|
|
168
|
+
return (0, session_authz_interface_1.normalizedSessionAuthzModuleOptions)(moduleOptions);
|
|
169
|
+
},
|
|
170
|
+
inject: [MODULE_OPTIONS_TOKEN]
|
|
171
|
+
}
|
|
172
|
+
]
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
constructor(routesOpt, sessionAuthzOptions) {
|
|
176
|
+
super();
|
|
177
|
+
this.routesOpt = routesOpt;
|
|
178
|
+
this.sessionAuthzOptions = sessionAuthzOptions;
|
|
179
|
+
}
|
|
180
|
+
configure(consumer) {
|
|
181
|
+
consumer
|
|
182
|
+
.apply((0, express_session_1.default)(this.sessionAuthzOptions.session), SessionAuthzAlsMiddleware)
|
|
183
|
+
.exclude(...this.routesOpt.excludes)
|
|
184
|
+
// nestjs v11 will be compatible with splat wildcard.
|
|
185
|
+
.forRoutes(...(this.routesOpt.global ? ['*'] : this.routesOpt.routes));
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
SessionAuthzModule = __decorate([
|
|
189
|
+
(0, common_1.Module)({}),
|
|
190
|
+
__param(0, (0, common_1.Inject)(constants_1.ROUTES_OPTIONS)),
|
|
191
|
+
__param(1, (0, common_1.Inject)(SESSION_AUTHZ_OPTIONS)),
|
|
192
|
+
__metadata("design:paramtypes", [Object, Object])
|
|
193
|
+
], SessionAuthzModule);
|
|
194
|
+
return {
|
|
195
|
+
/**
|
|
196
|
+
* A dynamic module used to configure session 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 session strategy
|
|
209
|
+
* AuthzModule.register({
|
|
210
|
+
* session: {
|
|
211
|
+
* name: 'custom-session-id-name',
|
|
212
|
+
* secret: '1234567890'
|
|
213
|
+
* },
|
|
214
|
+
* // Define routes that use AuthzGuard
|
|
215
|
+
* routes: [BusinessController]
|
|
216
|
+
* })
|
|
217
|
+
* ],
|
|
218
|
+
* controllers: [BusinessController]
|
|
219
|
+
* })
|
|
220
|
+
* export class BusinessModule {}
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
AuthzModule: SessionAuthzModule,
|
|
224
|
+
/**
|
|
225
|
+
* A custom guard that applies authentication to controllers.
|
|
226
|
+
*
|
|
227
|
+
* This guard also provides 3 utility decorators to apply and modify authorization:
|
|
228
|
+
*
|
|
229
|
+
* - `@AuthzGuard.Verify`: Used to verify the user's authorization for specific meta data.
|
|
230
|
+
* - `@AuthzGuard.NoVerify`: Used to `skip` authentication & authorization checks for specific routes.
|
|
231
|
+
* - `@AuthzGuard.Apply`: A simplified version of `@UseGuards(AuthzGuard)` and `@AuthzGuard.Verify`, combining both for convenience.
|
|
232
|
+
*
|
|
233
|
+
* ### Usage:
|
|
234
|
+
*
|
|
235
|
+
* ```typescript
|
|
236
|
+
* @UseGuards(AuthzGuard)
|
|
237
|
+
* @Controller(// ...)
|
|
238
|
+
* export class BusinessController {
|
|
239
|
+
* // ...
|
|
240
|
+
* }
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
AuthzGuard: SessionAuthzGuard,
|
|
244
|
+
/**
|
|
245
|
+
* A custom servcie to provide methods to handle authentication and authorization.
|
|
246
|
+
*/
|
|
247
|
+
AuthzService: SessionAuthzService
|
|
144
248
|
};
|
|
145
|
-
isStrategyInited = true;
|
|
146
|
-
return configs;
|
|
147
|
-
};
|
|
148
|
-
let SessionAuthzModule = class extends ConfigurableModuleClass {
|
|
149
|
-
constructor(routesOpt, sessionAuthzOptions) {
|
|
150
|
-
super();
|
|
151
|
-
this.routesOpt = routesOpt;
|
|
152
|
-
this.sessionAuthzOptions = sessionAuthzOptions;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Configures authz module.
|
|
156
|
-
*
|
|
157
|
-
* Note: DO NOT register the same routes in multiple session authz modules, or import the same session authz module in the same module multiple times, express-session middleware will not work properly.
|
|
158
|
-
*/
|
|
159
|
-
static register(options) {
|
|
160
|
-
const sessionAuthzOptions = (0, import_session_authz2.normalizedSessionAuthzModuleOptions)(options);
|
|
161
|
-
return (0, import_utils.mergeDynamicModuleConfigs)(super.register({ ...options, authzProvider }), getCommonConfigs(), {
|
|
162
|
-
providers: [
|
|
163
|
-
{
|
|
164
|
-
provide: SESSION_AUTHZ_OPTIONS,
|
|
165
|
-
useValue: sessionAuthzOptions
|
|
166
|
-
}
|
|
167
|
-
]
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Configures authz module asynchronously.
|
|
172
|
-
*
|
|
173
|
-
* Note: DO NOT register the same routes in multiple session authz modules, express-session middleware will not work properly.
|
|
174
|
-
*/
|
|
175
|
-
static registerAsync(options) {
|
|
176
|
-
return (0, import_utils.mergeDynamicModuleConfigs)(super.registerAsync({ ...options, authzProvider }), getCommonConfigs(), {
|
|
177
|
-
providers: [
|
|
178
|
-
{
|
|
179
|
-
provide: SESSION_AUTHZ_OPTIONS,
|
|
180
|
-
useFactory: (moduleOptions) => {
|
|
181
|
-
return (0, import_session_authz2.normalizedSessionAuthzModuleOptions)(moduleOptions);
|
|
182
|
-
},
|
|
183
|
-
inject: [MODULE_OPTIONS_TOKEN]
|
|
184
|
-
}
|
|
185
|
-
]
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
configure(consumer) {
|
|
189
|
-
consumer.apply((0, import_express_session.default)(this.sessionAuthzOptions.session), SessionAuthzAlsMiddleware).exclude(...this.routesOpt.excludes).forRoutes(...this.routesOpt.global ? ["*"] : this.routesOpt.routes);
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
SessionAuthzModule = __decorateClass([
|
|
193
|
-
(0, import_common.Module)({}),
|
|
194
|
-
__decorateParam(0, (0, import_common.Inject)(import_constants.ROUTES_OPTIONS)),
|
|
195
|
-
__decorateParam(1, (0, import_common.Inject)(SESSION_AUTHZ_OPTIONS))
|
|
196
|
-
], SessionAuthzModule);
|
|
197
|
-
return {
|
|
198
|
-
/**
|
|
199
|
-
* A dynamic module used to configure session based authentication and authorization features for the application.
|
|
200
|
-
*
|
|
201
|
-
* This module can be configured using 2 static methods:
|
|
202
|
-
*
|
|
203
|
-
* - `register`
|
|
204
|
-
* - `registerAsync`
|
|
205
|
-
*
|
|
206
|
-
* ### Usage
|
|
207
|
-
*
|
|
208
|
-
* ```typescript
|
|
209
|
-
* @Module({
|
|
210
|
-
* imports: [
|
|
211
|
-
* // Import and configure session strategy
|
|
212
|
-
* AuthzModule.register({
|
|
213
|
-
* session: {
|
|
214
|
-
* name: 'custom-session-id-name',
|
|
215
|
-
* secret: '1234567890'
|
|
216
|
-
* },
|
|
217
|
-
* // Define routes that use AuthzGuard
|
|
218
|
-
* routes: [BusinessController]
|
|
219
|
-
* })
|
|
220
|
-
* ],
|
|
221
|
-
* controllers: [BusinessController]
|
|
222
|
-
* })
|
|
223
|
-
* export class BusinessModule {}
|
|
224
|
-
* ```
|
|
225
|
-
*/
|
|
226
|
-
AuthzModule: SessionAuthzModule,
|
|
227
|
-
/**
|
|
228
|
-
* A custom guard that applies authentication to controllers.
|
|
229
|
-
*
|
|
230
|
-
* This guard also provides 3 utility decorators to apply and modify authorization:
|
|
231
|
-
*
|
|
232
|
-
* - `@AuthzGuard.Verify`: Used to verify the user's authorization for specific meta data.
|
|
233
|
-
* - `@AuthzGuard.NoVerify`: Used to `skip` authentication & authorization checks for specific routes.
|
|
234
|
-
* - `@AuthzGuard.Apply`: A simplified version of `@UseGuards(AuthzGuard)` and `@AuthzGuard.Verify`, combining both for convenience.
|
|
235
|
-
*
|
|
236
|
-
* ### Usage:
|
|
237
|
-
*
|
|
238
|
-
* ```typescript
|
|
239
|
-
* @UseGuards(AuthzGuard)
|
|
240
|
-
* @Controller(// ...)
|
|
241
|
-
* export class BusinessController {
|
|
242
|
-
* // ...
|
|
243
|
-
* }
|
|
244
|
-
* ```
|
|
245
|
-
*/
|
|
246
|
-
AuthzGuard: SessionAuthzGuard,
|
|
247
|
-
/**
|
|
248
|
-
* A custom servcie to provide methods to handle authentication and authorization.
|
|
249
|
-
*/
|
|
250
|
-
AuthzService: SessionAuthzService
|
|
251
|
-
};
|
|
252
249
|
};
|
|
253
|
-
|
|
254
|
-
0 && (module.exports = {
|
|
255
|
-
cereateSessionAuthzModule
|
|
256
|
-
});
|
|
250
|
+
exports.cereateSessionAuthzModule = cereateSessionAuthzModule;
|
|
@@ -1,14 +1,8 @@
|
|
|
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 'express-session';
|
|
10
|
-
|
|
11
|
-
declare const createSessionAuthzService: <P = unknown, U = unknown>([AUTHZ_PROVIDER, ALS_PROVIDER]: [any, any]) => _nestjs_common.Type<Omit<{
|
|
2
|
+
import { AuthzProviderClass } from '../authz.provider';
|
|
3
|
+
import { type DeepReadonly } from '../utils';
|
|
4
|
+
import type { SessionAlsType } from './session-authz-als.middleware';
|
|
5
|
+
export declare const createSessionAuthzService: <P = unknown, U = unknown>([AUTHZ_PROVIDER, ALS_PROVIDER]: [any, any]) => import("@nestjs/common").Type<Omit<{
|
|
12
6
|
readonly authzProvider: AuthzProviderClass<P, U>;
|
|
13
7
|
readonly als: AsyncLocalStorage<SessionAlsType<P, U>>;
|
|
14
8
|
/**
|
|
@@ -24,11 +18,9 @@ declare const createSessionAuthzService: <P = unknown, U = unknown>([AUTHZ_PROVI
|
|
|
24
18
|
/**
|
|
25
19
|
* Sets a secure HTTP cookie with the given name, value, and optional cookie options.
|
|
26
20
|
*/
|
|
27
|
-
setCookie(name: string, value: string, options?: CookieOptionsWithSecret | undefined): void;
|
|
21
|
+
setCookie(name: string, value: string, options?: import("../utils").CookieOptionsWithSecret | undefined): void;
|
|
28
22
|
/**
|
|
29
23
|
* Retrieves the current user associated with the request, if available.
|
|
30
24
|
*/
|
|
31
25
|
getUser(): DeepReadonly<U> | undefined;
|
|
32
26
|
}, "als" | "authzProvider">>;
|
|
33
|
-
|
|
34
|
-
export { createSessionAuthzService };
|