@nestjs-kitchen/authz 1.0.0
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/README.md +0 -0
- package/dist/authz.provider.d.ts +7 -0
- package/dist/authz.provider.js +34 -0
- package/dist/constants.d.ts +11 -0
- package/dist/constants.js +46 -0
- package/dist/errors.d.ts +12 -0
- package/dist/errors.js +60 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +43 -0
- package/dist/jwt/extract-jwt.d.ts +71 -0
- package/dist/jwt/extract-jwt.js +162 -0
- package/dist/jwt/index.d.ts +16 -0
- package/dist/jwt/index.js +30 -0
- package/dist/jwt/jwt-authz-als.middleware.d.ts +28 -0
- package/dist/jwt/jwt-authz-als.middleware.js +83 -0
- package/dist/jwt/jwt-authz.guard.d.ts +60 -0
- package/dist/jwt/jwt-authz.guard.js +182 -0
- package/dist/jwt/jwt-authz.interface.d.ts +58 -0
- package/dist/jwt/jwt-authz.interface.js +94 -0
- package/dist/jwt/jwt-authz.module.d.ts +80 -0
- package/dist/jwt/jwt-authz.module.js +244 -0
- package/dist/jwt/jwt-authz.service.d.ts +33 -0
- package/dist/jwt/jwt-authz.service.js +144 -0
- package/dist/jwt/jwt-authz.strategy.d.ts +40 -0
- package/dist/jwt/jwt-authz.strategy.js +194 -0
- package/dist/session/index.d.ts +12 -0
- package/dist/session/index.js +27 -0
- package/dist/session/session-authz-als.middleware.d.ts +25 -0
- package/dist/session/session-authz-als.middleware.js +134 -0
- package/dist/session/session-authz.guard.d.ts +43 -0
- package/dist/session/session-authz.guard.js +142 -0
- package/dist/session/session-authz.interface.d.ts +23 -0
- package/dist/session/session-authz.interface.js +43 -0
- package/dist/session/session-authz.module.d.ts +71 -0
- package/dist/session/session-authz.module.js +245 -0
- package/dist/session/session-authz.service.d.ts +20 -0
- package/dist/session/session-authz.service.js +93 -0
- package/dist/session/session-authz.strategy.d.ts +24 -0
- package/dist/session/session-authz.strategy.js +107 -0
- package/dist/user.decorator.d.ts +6 -0
- package/dist/user.decorator.js +36 -0
- package/dist/utils/cookie-parsers.d.ts +12 -0
- package/dist/utils/cookie-parsers.js +77 -0
- package/dist/utils/create-authz-decorator-factory.d.ts +9 -0
- package/dist/utils/create-authz-decorator-factory.js +56 -0
- package/dist/utils/create-set-cookie-fn.d.ts +9 -0
- package/dist/utils/create-set-cookie-fn.js +46 -0
- package/dist/utils/generics.d.ts +6 -0
- package/dist/utils/generics.js +66 -0
- package/dist/utils/get-allow-anonymous.d.ts +11 -0
- package/dist/utils/get-allow-anonymous.js +30 -0
- package/dist/utils/get-als-store.d.ts +5 -0
- package/dist/utils/get-als-store.js +35 -0
- package/dist/utils/get-context-authz-meta-params-list.d.ts +12 -0
- package/dist/utils/get-context-authz-meta-params-list.js +36 -0
- package/dist/utils/get-passport-property.d.ts +3 -0
- package/dist/utils/get-passport-property.js +31 -0
- package/dist/utils/index.d.ts +16 -0
- package/dist/utils/index.js +41 -0
- package/dist/utils/merge-dynamic-module-configs.d.ts +5 -0
- package/dist/utils/merge-dynamic-module-configs.js +50 -0
- package/dist/utils/msgpackrs.d.ts +4 -0
- package/dist/utils/msgpackrs.js +37 -0
- package/dist/utils/types.d.ts +56 -0
- package/dist/utils/types.js +15 -0
- package/package.json +68 -0
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
31
|
+
var session_authz_module_exports = {};
|
|
32
|
+
__export(session_authz_module_exports, {
|
|
33
|
+
cereateSessionAuthzModule: () => cereateSessionAuthzModule
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(session_authz_module_exports);
|
|
36
|
+
var import_node_async_hooks = require("node:async_hooks");
|
|
37
|
+
var import_common = require("@nestjs/common");
|
|
38
|
+
var import_express_session = __toESM(require("express-session"));
|
|
39
|
+
var import_uid = require("uid");
|
|
40
|
+
var import_constants = require("../constants");
|
|
41
|
+
var import_errors = require("../errors");
|
|
42
|
+
var import_utils = require("../utils");
|
|
43
|
+
var import_session_authz_als = require("./session-authz-als.middleware");
|
|
44
|
+
var import_session_authz = require("./session-authz.guard");
|
|
45
|
+
var import_session_authz2 = require("./session-authz.interface");
|
|
46
|
+
var import_session_authz3 = require("./session-authz.service");
|
|
47
|
+
var import_session_authz4 = require("./session-authz.strategy");
|
|
48
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
49
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
50
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
51
|
+
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;
|
|
52
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
53
|
+
}
|
|
54
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
55
|
+
function _ts_metadata(k, v) {
|
|
56
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
57
|
+
}
|
|
58
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
59
|
+
function _ts_param(paramIndex, decorator) {
|
|
60
|
+
return function(target, key) {
|
|
61
|
+
decorator(target, key, paramIndex);
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
__name(_ts_param, "_ts_param");
|
|
65
|
+
const store = {
|
|
66
|
+
globalInited: 0
|
|
67
|
+
};
|
|
68
|
+
const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, ASYNC_OPTIONS_TYPE, OPTIONS_TYPE } = new import_common.ConfigurableModuleBuilder({
|
|
69
|
+
moduleName: "SessionAuthModule"
|
|
70
|
+
}).setFactoryMethodName("createSessionAuthzModuleOptions").setExtras({
|
|
71
|
+
authzProvider: void 0,
|
|
72
|
+
global: false
|
|
73
|
+
}, (definition, extras) => {
|
|
74
|
+
const { authzProvider, global } = extras;
|
|
75
|
+
if (!authzProvider) {
|
|
76
|
+
throw new import_errors.AuthzError(`InternalError: Missing parameter 'authzProvider' in configuration.`);
|
|
77
|
+
}
|
|
78
|
+
const routes = (0, import_utils.normalizedArray)(extras.routes) ?? [];
|
|
79
|
+
const excludes = (0, import_utils.normalizedArray)(extras.excludes) ?? [];
|
|
80
|
+
if (!global && !routes.length) {
|
|
81
|
+
throw new import_errors.AuthzError(`InternalError: Missing parameter 'global' or 'routes' in configuration.`);
|
|
82
|
+
}
|
|
83
|
+
if (store.globalInited) {
|
|
84
|
+
throw new import_errors.AuthzError(`InternalError: Cannot initialize mutiple global modules. Only one global module is allowed.`);
|
|
85
|
+
}
|
|
86
|
+
if (global) {
|
|
87
|
+
store.globalInited += 1;
|
|
88
|
+
}
|
|
89
|
+
return (0, import_utils.mergeDynamicModuleConfigs)(definition, {
|
|
90
|
+
global,
|
|
91
|
+
providers: [
|
|
92
|
+
{
|
|
93
|
+
provide: import_constants.ROUTES_OPTIONS,
|
|
94
|
+
useValue: {
|
|
95
|
+
global,
|
|
96
|
+
excludes,
|
|
97
|
+
routes
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
],
|
|
101
|
+
exports: []
|
|
102
|
+
});
|
|
103
|
+
}).build();
|
|
104
|
+
const cereateSessionAuthzModule = /* @__PURE__ */ __name((authzProvider) => {
|
|
105
|
+
var _a;
|
|
106
|
+
const id = `${import_constants.PREFIX}${(0, import_uid.uid)()}`;
|
|
107
|
+
const SESSION_STRATEGY = `${id}_SESSION_STRATEGY`;
|
|
108
|
+
const AUTHZ_PROVIDER = `${id}_AUTHZ_PROVIDER`;
|
|
109
|
+
const ALS_PROVIDER = `${id}_ALS_PROVIDER`;
|
|
110
|
+
const SESSION_AUTHZ_OPTIONS = `${id}_SESSION_AUTHZ_OPTIONS`;
|
|
111
|
+
const SESSION_META_KEY = `${id}_SESSION_META_KEY`;
|
|
112
|
+
const SessionAuthzStrategy = (0, import_session_authz4.createSessionAuthzStrategy)([
|
|
113
|
+
SESSION_STRATEGY,
|
|
114
|
+
AUTHZ_PROVIDER,
|
|
115
|
+
ALS_PROVIDER
|
|
116
|
+
]);
|
|
117
|
+
const SessionAuthzService = (0, import_session_authz3.createSessionAuthzService)([
|
|
118
|
+
AUTHZ_PROVIDER,
|
|
119
|
+
ALS_PROVIDER
|
|
120
|
+
]);
|
|
121
|
+
const SessionAuthzAlsMiddleware = (0, import_session_authz_als.createSessionAuthzAlsMiddleware)([
|
|
122
|
+
ALS_PROVIDER,
|
|
123
|
+
SESSION_AUTHZ_OPTIONS
|
|
124
|
+
]);
|
|
125
|
+
const als = new import_node_async_hooks.AsyncLocalStorage();
|
|
126
|
+
let isStrategyInited = false;
|
|
127
|
+
const SessionAuthzGuard = (0, import_session_authz.createSessionAuthzGuard)([
|
|
128
|
+
SESSION_STRATEGY,
|
|
129
|
+
AUTHZ_PROVIDER,
|
|
130
|
+
SESSION_AUTHZ_OPTIONS,
|
|
131
|
+
ALS_PROVIDER,
|
|
132
|
+
SESSION_META_KEY
|
|
133
|
+
]);
|
|
134
|
+
const Verify = (0, import_utils.createAuthzDecoratorFactory)(SESSION_META_KEY);
|
|
135
|
+
const NoVerify = /* @__PURE__ */ __name(() => {
|
|
136
|
+
return (0, import_common.SetMetadata)(SESSION_META_KEY, {
|
|
137
|
+
options: {
|
|
138
|
+
public: true,
|
|
139
|
+
override: true
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}, "NoVerify");
|
|
143
|
+
const Apply = /* @__PURE__ */ __name((...rest) => {
|
|
144
|
+
return (0, import_common.applyDecorators)(SessionAuthzGuard.Verify(...rest), (0, import_common.UseGuards)(SessionAuthzGuard));
|
|
145
|
+
}, "Apply");
|
|
146
|
+
SessionAuthzGuard.Verify = Verify;
|
|
147
|
+
SessionAuthzGuard.NoVerify = NoVerify;
|
|
148
|
+
SessionAuthzGuard.Apply = Apply;
|
|
149
|
+
const getCommonConfigs = /* @__PURE__ */ __name(() => {
|
|
150
|
+
const configs = {
|
|
151
|
+
providers: [
|
|
152
|
+
{
|
|
153
|
+
provide: AUTHZ_PROVIDER,
|
|
154
|
+
useClass: authzProvider
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
provide: ALS_PROVIDER,
|
|
158
|
+
useValue: als
|
|
159
|
+
},
|
|
160
|
+
...!isStrategyInited ? [
|
|
161
|
+
SessionAuthzStrategy
|
|
162
|
+
] : [],
|
|
163
|
+
SessionAuthzService
|
|
164
|
+
],
|
|
165
|
+
exports: [
|
|
166
|
+
AUTHZ_PROVIDER,
|
|
167
|
+
ALS_PROVIDER,
|
|
168
|
+
SESSION_AUTHZ_OPTIONS,
|
|
169
|
+
SessionAuthzService
|
|
170
|
+
]
|
|
171
|
+
};
|
|
172
|
+
isStrategyInited = true;
|
|
173
|
+
return configs;
|
|
174
|
+
}, "getCommonConfigs");
|
|
175
|
+
let SessionAuthzModule = (_a = class extends ConfigurableModuleClass {
|
|
176
|
+
constructor(routesOpt, sessionAuthzOptions) {
|
|
177
|
+
super();
|
|
178
|
+
__publicField(this, "routesOpt");
|
|
179
|
+
__publicField(this, "sessionAuthzOptions");
|
|
180
|
+
this.routesOpt = routesOpt, this.sessionAuthzOptions = sessionAuthzOptions;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Note: DO NOT register the same route 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.
|
|
184
|
+
*/
|
|
185
|
+
static register(options) {
|
|
186
|
+
const sessionAuthzOptions = (0, import_session_authz2.normalizedSessionAuthzModuleOptions)(options);
|
|
187
|
+
return (0, import_utils.mergeDynamicModuleConfigs)(super.register({
|
|
188
|
+
...options,
|
|
189
|
+
authzProvider
|
|
190
|
+
}), getCommonConfigs(), {
|
|
191
|
+
providers: [
|
|
192
|
+
{
|
|
193
|
+
provide: SESSION_AUTHZ_OPTIONS,
|
|
194
|
+
useValue: sessionAuthzOptions
|
|
195
|
+
}
|
|
196
|
+
]
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Note: DO NOT register the same route in multiple session authz modules, express-session middleware will not work properly.
|
|
201
|
+
*/
|
|
202
|
+
static registerAsync(options) {
|
|
203
|
+
return (0, import_utils.mergeDynamicModuleConfigs)(super.registerAsync({
|
|
204
|
+
...options,
|
|
205
|
+
authzProvider
|
|
206
|
+
}), getCommonConfigs(), {
|
|
207
|
+
providers: [
|
|
208
|
+
{
|
|
209
|
+
provide: SESSION_AUTHZ_OPTIONS,
|
|
210
|
+
useFactory: /* @__PURE__ */ __name((moduleOptions) => {
|
|
211
|
+
return (0, import_session_authz2.normalizedSessionAuthzModuleOptions)(moduleOptions);
|
|
212
|
+
}, "useFactory"),
|
|
213
|
+
inject: [
|
|
214
|
+
MODULE_OPTIONS_TOKEN
|
|
215
|
+
]
|
|
216
|
+
}
|
|
217
|
+
]
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
configure(consumer) {
|
|
221
|
+
consumer.apply((0, import_express_session.default)(this.sessionAuthzOptions.session), SessionAuthzAlsMiddleware).exclude(...this.routesOpt.excludes).forRoutes(...this.routesOpt.global ? [
|
|
222
|
+
"*"
|
|
223
|
+
] : this.routesOpt.routes);
|
|
224
|
+
}
|
|
225
|
+
}, __name(_a, "SessionAuthzModule"), _a);
|
|
226
|
+
SessionAuthzModule = _ts_decorate([
|
|
227
|
+
(0, import_common.Module)({}),
|
|
228
|
+
_ts_param(0, (0, import_common.Inject)(import_constants.ROUTES_OPTIONS)),
|
|
229
|
+
_ts_param(1, (0, import_common.Inject)(SESSION_AUTHZ_OPTIONS)),
|
|
230
|
+
_ts_metadata("design:type", Function),
|
|
231
|
+
_ts_metadata("design:paramtypes", [
|
|
232
|
+
typeof RoutesOptions === "undefined" ? Object : RoutesOptions,
|
|
233
|
+
typeof SessionAuthzOptions === "undefined" ? Object : SessionAuthzOptions
|
|
234
|
+
])
|
|
235
|
+
], SessionAuthzModule);
|
|
236
|
+
return {
|
|
237
|
+
AuthzModule: SessionAuthzModule,
|
|
238
|
+
AuthzGuard: SessionAuthzGuard,
|
|
239
|
+
AuthzService: SessionAuthzService
|
|
240
|
+
};
|
|
241
|
+
}, "cereateSessionAuthzModule");
|
|
242
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
243
|
+
0 && (module.exports = {
|
|
244
|
+
cereateSessionAuthzModule
|
|
245
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import * as _nestjs_common from '@nestjs/common';
|
|
2
|
+
import { AuthzProviderClass } from '../authz.provider.js';
|
|
3
|
+
import { CookieOptionsWithSecret } from '../utils/types.js';
|
|
4
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
5
|
+
import { SessionAlsType } from './session-authz-als.middleware.js';
|
|
6
|
+
import '@nestjs/common/interfaces';
|
|
7
|
+
import 'express';
|
|
8
|
+
import './session-authz.interface.js';
|
|
9
|
+
import 'express-session';
|
|
10
|
+
|
|
11
|
+
declare const createSessionAuthzService: <P = unknown, U = unknown>([AUTHZ_PROVIDER, ALS_PROVIDER]: [any, any]) => _nestjs_common.Type<Omit<{
|
|
12
|
+
readonly authzProvider: AuthzProviderClass<P, U>;
|
|
13
|
+
readonly als: AsyncLocalStorage<SessionAlsType<P, U>>;
|
|
14
|
+
logIn(user: U): Promise<void>;
|
|
15
|
+
logOut(): Promise<void>;
|
|
16
|
+
setCookie(name: string, value: string, options?: CookieOptionsWithSecret | undefined): void;
|
|
17
|
+
getUser(): U | undefined;
|
|
18
|
+
}, "als" | "authzProvider">>;
|
|
19
|
+
|
|
20
|
+
export { createSessionAuthzService };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
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;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
21
|
+
var session_authz_service_exports = {};
|
|
22
|
+
__export(session_authz_service_exports, {
|
|
23
|
+
createSessionAuthzService: () => createSessionAuthzService
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(session_authz_service_exports);
|
|
26
|
+
var import_node_async_hooks = require("node:async_hooks");
|
|
27
|
+
var import_common = require("@nestjs/common");
|
|
28
|
+
var import_authz = require("../authz.provider");
|
|
29
|
+
var import_errors = require("../errors");
|
|
30
|
+
var import_utils = require("../utils");
|
|
31
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
32
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
33
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
34
|
+
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;
|
|
35
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
36
|
+
}
|
|
37
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
38
|
+
function _ts_metadata(k, v) {
|
|
39
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
40
|
+
}
|
|
41
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
42
|
+
function _ts_param(paramIndex, decorator) {
|
|
43
|
+
return function(target, key) {
|
|
44
|
+
decorator(target, key, paramIndex);
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
__name(_ts_param, "_ts_param");
|
|
48
|
+
const createSessionAuthzService = /* @__PURE__ */ __name(([AUTHZ_PROVIDER, ALS_PROVIDER]) => {
|
|
49
|
+
var _a;
|
|
50
|
+
let SessionAuthzService = (_a = class {
|
|
51
|
+
constructor(authzProvider, als) {
|
|
52
|
+
__publicField(this, "authzProvider");
|
|
53
|
+
__publicField(this, "als");
|
|
54
|
+
this.authzProvider = authzProvider;
|
|
55
|
+
this.als = als;
|
|
56
|
+
if (typeof this.authzProvider.createPayload !== "function") {
|
|
57
|
+
throw new import_errors.AuthzError(`InternalError: Method 'createPayload' from abstract class 'AuthzProvider' must be implemented.`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async logIn(user) {
|
|
61
|
+
const store = (0, import_utils.getAlsStore)(this.als);
|
|
62
|
+
const payload = await this.authzProvider.createPayload(user);
|
|
63
|
+
return store.logIn(payload);
|
|
64
|
+
}
|
|
65
|
+
async logOut() {
|
|
66
|
+
const store = (0, import_utils.getAlsStore)(this.als);
|
|
67
|
+
return store.logOut();
|
|
68
|
+
}
|
|
69
|
+
setCookie(...rest) {
|
|
70
|
+
const store = (0, import_utils.getAlsStore)(this.als);
|
|
71
|
+
store.setCookie(...rest);
|
|
72
|
+
}
|
|
73
|
+
getUser() {
|
|
74
|
+
const store = (0, import_utils.getAlsStore)(this.als);
|
|
75
|
+
const user = store.user;
|
|
76
|
+
return user;
|
|
77
|
+
}
|
|
78
|
+
}, __name(_a, "SessionAuthzService"), _a);
|
|
79
|
+
SessionAuthzService = _ts_decorate([
|
|
80
|
+
_ts_param(0, (0, import_common.Inject)(AUTHZ_PROVIDER)),
|
|
81
|
+
_ts_param(1, (0, import_common.Inject)(ALS_PROVIDER)),
|
|
82
|
+
_ts_metadata("design:type", Function),
|
|
83
|
+
_ts_metadata("design:paramtypes", [
|
|
84
|
+
typeof import_authz.AuthzProviderClass === "undefined" ? Object : import_authz.AuthzProviderClass,
|
|
85
|
+
typeof import_node_async_hooks.AsyncLocalStorage === "undefined" ? Object : import_node_async_hooks.AsyncLocalStorage
|
|
86
|
+
])
|
|
87
|
+
], SessionAuthzService);
|
|
88
|
+
return (0, import_common.mixin)(SessionAuthzService);
|
|
89
|
+
}, "createSessionAuthzService");
|
|
90
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
91
|
+
0 && (module.exports = {
|
|
92
|
+
createSessionAuthzService
|
|
93
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as _nestjs_common from '@nestjs/common';
|
|
2
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
+
import { Request } from 'express';
|
|
4
|
+
import { AuthzProviderClass } from '../authz.provider.js';
|
|
5
|
+
import { SessionAlsType } from './session-authz-als.middleware.js';
|
|
6
|
+
import '../utils/types.js';
|
|
7
|
+
import '@nestjs/common/interfaces';
|
|
8
|
+
import './session-authz.interface.js';
|
|
9
|
+
import 'express-session';
|
|
10
|
+
|
|
11
|
+
declare const createSessionAuthzStrategy: ([SESSION_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]: [string, any, any]) => _nestjs_common.Type<Omit<{
|
|
12
|
+
readonly authzProvider: AuthzProviderClass<unknown, unknown>;
|
|
13
|
+
readonly als: AsyncLocalStorage<SessionAlsType<unknown, unknown>>;
|
|
14
|
+
validate(req: Request): Promise<{}>;
|
|
15
|
+
authenticate(req: Request, options?: any): any;
|
|
16
|
+
success(user: any, info?: any): void;
|
|
17
|
+
fail(challenge: any, status: number): void;
|
|
18
|
+
fail(status: number): void;
|
|
19
|
+
redirect(url: string, status?: number): void;
|
|
20
|
+
pass(): void;
|
|
21
|
+
error(err: Error): void;
|
|
22
|
+
}, "als" | "authzProvider">>;
|
|
23
|
+
|
|
24
|
+
export { createSessionAuthzStrategy };
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
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;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
21
|
+
var session_authz_strategy_exports = {};
|
|
22
|
+
__export(session_authz_strategy_exports, {
|
|
23
|
+
createSessionAuthzStrategy: () => createSessionAuthzStrategy
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(session_authz_strategy_exports);
|
|
26
|
+
var import_node_async_hooks = require("node:async_hooks");
|
|
27
|
+
var import_common = require("@nestjs/common");
|
|
28
|
+
var import_passport = require("@nestjs/passport");
|
|
29
|
+
var import_passport_custom = require("passport-custom");
|
|
30
|
+
var import_authz = require("../authz.provider");
|
|
31
|
+
var import_constants = require("../constants");
|
|
32
|
+
var import_errors = require("../errors");
|
|
33
|
+
var import_utils = require("../utils");
|
|
34
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
35
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
36
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
37
|
+
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;
|
|
38
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
39
|
+
}
|
|
40
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
41
|
+
function _ts_metadata(k, v) {
|
|
42
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
43
|
+
}
|
|
44
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
45
|
+
function _ts_param(paramIndex, decorator) {
|
|
46
|
+
return function(target, key) {
|
|
47
|
+
decorator(target, key, paramIndex);
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
__name(_ts_param, "_ts_param");
|
|
51
|
+
const createSessionAuthzStrategy = /* @__PURE__ */ __name(([SESSION_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]) => {
|
|
52
|
+
var _a;
|
|
53
|
+
let SessionAuthzStrategy = (_a = class extends (0, import_passport.PassportStrategy)(import_passport_custom.Strategy, SESSION_STRATEGY) {
|
|
54
|
+
constructor(authzProvider, als) {
|
|
55
|
+
super();
|
|
56
|
+
__publicField(this, "authzProvider");
|
|
57
|
+
__publicField(this, "als");
|
|
58
|
+
this.authzProvider = authzProvider, this.als = als;
|
|
59
|
+
if (typeof this.authzProvider.authenticate !== "function") {
|
|
60
|
+
throw new import_errors.AuthzError(`InternalError: Method 'authenticate' from abstract class 'AuthzProvider' must be implemented.`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async validate(req) {
|
|
64
|
+
const store = (0, import_utils.getAlsStore)(this.als);
|
|
65
|
+
const authOptions = store.authOptions;
|
|
66
|
+
req[import_constants.PASSPORT_PROPERTY] = authOptions.passportProperty;
|
|
67
|
+
const payload = req?.session?.[import_constants.SESSION_PASSPORT_KEY]?.user;
|
|
68
|
+
if (!payload) {
|
|
69
|
+
return [
|
|
70
|
+
null,
|
|
71
|
+
new import_errors.AuthzAnonymousError("AnonymousError: Cannnot find session.")
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
let user = void 0;
|
|
75
|
+
try {
|
|
76
|
+
user = await this.authzProvider.authenticate(payload);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
return [
|
|
79
|
+
null,
|
|
80
|
+
error instanceof Error ? new import_errors.AuthzVerificationError(`${error.name}: ${error.message}`, error) : new import_errors.AuthzVerificationError(`${error}`)
|
|
81
|
+
];
|
|
82
|
+
}
|
|
83
|
+
store.user = user;
|
|
84
|
+
if (!user) {
|
|
85
|
+
return [
|
|
86
|
+
null,
|
|
87
|
+
new import_errors.AuthzAnonymousError("AnonymousError: Cannnot find user.")
|
|
88
|
+
];
|
|
89
|
+
}
|
|
90
|
+
return user;
|
|
91
|
+
}
|
|
92
|
+
}, __name(_a, "SessionAuthzStrategy"), _a);
|
|
93
|
+
SessionAuthzStrategy = _ts_decorate([
|
|
94
|
+
_ts_param(0, (0, import_common.Inject)(AUTHZ_PROVIDER)),
|
|
95
|
+
_ts_param(1, (0, import_common.Inject)(ALS_PROVIDER)),
|
|
96
|
+
_ts_metadata("design:type", Function),
|
|
97
|
+
_ts_metadata("design:paramtypes", [
|
|
98
|
+
typeof import_authz.AuthzProviderClass === "undefined" ? Object : import_authz.AuthzProviderClass,
|
|
99
|
+
typeof import_node_async_hooks.AsyncLocalStorage === "undefined" ? Object : import_node_async_hooks.AsyncLocalStorage
|
|
100
|
+
])
|
|
101
|
+
], SessionAuthzStrategy);
|
|
102
|
+
return (0, import_common.mixin)(SessionAuthzStrategy);
|
|
103
|
+
}, "createSessionAuthzStrategy");
|
|
104
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
105
|
+
0 && (module.exports = {
|
|
106
|
+
createSessionAuthzStrategy
|
|
107
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var user_decorator_exports = {};
|
|
20
|
+
__export(user_decorator_exports, {
|
|
21
|
+
User: () => User,
|
|
22
|
+
userDecoratorFactory: () => userDecoratorFactory
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(user_decorator_exports);
|
|
25
|
+
var import_common = require("@nestjs/common");
|
|
26
|
+
var import_utils = require("./utils");
|
|
27
|
+
const userDecoratorFactory = /* @__PURE__ */ __name((_data, ctx) => {
|
|
28
|
+
const request = ctx.switchToHttp().getRequest();
|
|
29
|
+
return (0, import_utils.getPassportProperty)(request);
|
|
30
|
+
}, "userDecoratorFactory");
|
|
31
|
+
const User = (0, import_common.createParamDecorator)(userDecoratorFactory);
|
|
32
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
33
|
+
0 && (module.exports = {
|
|
34
|
+
User,
|
|
35
|
+
userDecoratorFactory
|
|
36
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Request } from 'express';
|
|
2
|
+
|
|
3
|
+
declare const normalCookieParser: (req: Request, _secrets?: string[], decode?: (str: string) => string | undefined) => {
|
|
4
|
+
cookies: Record<string, any>;
|
|
5
|
+
signedCookies: Record<string, any>;
|
|
6
|
+
};
|
|
7
|
+
declare const customCookieParser: (req: Request, secrets?: string[], decode?: (str: string) => string | undefined) => {
|
|
8
|
+
cookies: Record<string, any>;
|
|
9
|
+
signedCookies: Record<string, any>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export { customCookieParser, normalCookieParser };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var cookie_parsers_exports = {};
|
|
30
|
+
__export(cookie_parsers_exports, {
|
|
31
|
+
customCookieParser: () => customCookieParser,
|
|
32
|
+
normalCookieParser: () => normalCookieParser
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(cookie_parsers_exports);
|
|
35
|
+
var cookie = __toESM(require("cookie"));
|
|
36
|
+
var import_cookie_parser = __toESM(require("cookie-parser"));
|
|
37
|
+
const normalCookieParser = /* @__PURE__ */ __name((req, _secrets = [], decode) => {
|
|
38
|
+
let cookies = req.cookies || {};
|
|
39
|
+
let signedCookies = req.signedCookies || {};
|
|
40
|
+
if (!req.cookies && req.headers.cookie) {
|
|
41
|
+
const parsedCookies = cookie.parse(req.headers.cookie, {
|
|
42
|
+
decode
|
|
43
|
+
});
|
|
44
|
+
if (req.secret) {
|
|
45
|
+
signedCookies = import_cookie_parser.default.JSONCookies(import_cookie_parser.default.signedCookies(parsedCookies, [
|
|
46
|
+
req.secret
|
|
47
|
+
]));
|
|
48
|
+
}
|
|
49
|
+
cookies = import_cookie_parser.default.JSONCookies(parsedCookies);
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
cookies,
|
|
53
|
+
signedCookies
|
|
54
|
+
};
|
|
55
|
+
}, "normalCookieParser");
|
|
56
|
+
const customCookieParser = /* @__PURE__ */ __name((req, secrets = [], decode) => {
|
|
57
|
+
let cookies = {};
|
|
58
|
+
let signedCookies = {};
|
|
59
|
+
if (req.headers.cookie) {
|
|
60
|
+
const parsedCookies = cookie.parse(req.headers.cookie, {
|
|
61
|
+
decode
|
|
62
|
+
});
|
|
63
|
+
if (secrets.length) {
|
|
64
|
+
signedCookies = import_cookie_parser.default.JSONCookies(import_cookie_parser.default.signedCookies(parsedCookies, secrets));
|
|
65
|
+
}
|
|
66
|
+
cookies = import_cookie_parser.default.JSONCookies(parsedCookies);
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
cookies,
|
|
70
|
+
signedCookies
|
|
71
|
+
};
|
|
72
|
+
}, "customCookieParser");
|
|
73
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
74
|
+
0 && (module.exports = {
|
|
75
|
+
customCookieParser,
|
|
76
|
+
normalCookieParser
|
|
77
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AuthzProviderClass } from '../authz.provider.js';
|
|
2
|
+
import { AuthzDecoParams, MethodParameters, ApplyDecorators } from './types.js';
|
|
3
|
+
import '@nestjs/common';
|
|
4
|
+
import '@nestjs/common/interfaces';
|
|
5
|
+
import 'express';
|
|
6
|
+
|
|
7
|
+
declare const createAuthzDecoratorFactory: <T extends AuthzProviderClass<unknown, unknown>>(metaKey: string | Symbol) => (...args: AuthzDecoParams<MethodParameters<T, "authorize">[1]>) => ApplyDecorators;
|
|
8
|
+
|
|
9
|
+
export { createAuthzDecoratorFactory };
|