@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.
Files changed (65) hide show
  1. package/dist/authz.provider.d.ts +1 -3
  2. package/dist/authz.provider.js +29 -48
  3. package/dist/constants.d.ts +6 -8
  4. package/dist/constants.js +13 -46
  5. package/dist/errors.d.ts +3 -5
  6. package/dist/errors.js +35 -60
  7. package/dist/index.d.ts +5 -21
  8. package/dist/index.js +16 -45
  9. package/dist/jwt/extract-jwt.d.ts +4 -7
  10. package/dist/jwt/extract-jwt.js +138 -158
  11. package/dist/jwt/index.d.ts +2 -16
  12. package/dist/jwt/index.js +7 -30
  13. package/dist/jwt/jwt-authz-als.middleware.d.ts +6 -16
  14. package/dist/jwt/jwt-authz-als.middleware.js +44 -81
  15. package/dist/jwt/jwt-authz.guard.d.ts +8 -21
  16. package/dist/jwt/jwt-authz.guard.js +130 -175
  17. package/dist/jwt/jwt-authz.interface.d.ts +16 -25
  18. package/dist/jwt/jwt-authz.interface.js +69 -92
  19. package/dist/jwt/jwt-authz.module.d.ts +19 -31
  20. package/dist/jwt/jwt-authz.module.js +250 -299
  21. package/dist/jwt/jwt-authz.service.d.ts +6 -17
  22. package/dist/jwt/jwt-authz.service.js +116 -162
  23. package/dist/jwt/jwt-authz.strategy.d.ts +5 -17
  24. package/dist/jwt/jwt-authz.strategy.js +132 -192
  25. package/dist/session/index.d.ts +1 -12
  26. package/dist/session/index.js +5 -27
  27. package/dist/session/session-authz-als.middleware.d.ts +5 -12
  28. package/dist/session/session-authz-als.middleware.js +98 -130
  29. package/dist/session/session-authz.guard.d.ts +6 -15
  30. package/dist/session/session-authz.guard.js +98 -135
  31. package/dist/session/session-authz.interface.d.ts +5 -12
  32. package/dist/session/session-authz.interface.js +19 -42
  33. package/dist/session/session-authz.module.d.ts +18 -26
  34. package/dist/session/session-authz.module.js +241 -290
  35. package/dist/session/session-authz.service.d.ts +5 -13
  36. package/dist/session/session-authz.service.js +69 -105
  37. package/dist/session/session-authz.strategy.d.ts +4 -12
  38. package/dist/session/session-authz.strategy.js +68 -105
  39. package/dist/user.decorator.d.ts +3 -6
  40. package/dist/user.decorator.js +26 -35
  41. package/dist/utils/cookie-parsers.d.ts +3 -6
  42. package/dist/utils/cookie-parsers.js +66 -74
  43. package/dist/utils/create-authz-decorator-factory.d.ts +3 -9
  44. package/dist/utils/create-authz-decorator-factory.js +32 -54
  45. package/dist/utils/create-set-cookie-fn.d.ts +3 -9
  46. package/dist/utils/create-set-cookie-fn.js +23 -45
  47. package/dist/utils/generics.d.ts +4 -6
  48. package/dist/utils/generics.js +37 -63
  49. package/dist/utils/get-allow-anonymous.d.ts +2 -9
  50. package/dist/utils/get-allow-anonymous.js +7 -29
  51. package/dist/utils/get-als-store.d.ts +2 -5
  52. package/dist/utils/get-als-store.js +11 -34
  53. package/dist/utils/get-context-authz-meta-params-list.d.ts +2 -9
  54. package/dist/utils/get-context-authz-meta-params-list.js +16 -35
  55. package/dist/utils/get-passport-property.d.ts +1 -3
  56. package/dist/utils/get-passport-property.js +8 -30
  57. package/dist/utils/index.d.ts +11 -16
  58. package/dist/utils/index.js +26 -40
  59. package/dist/utils/merge-dynamic-module-configs.d.ts +2 -5
  60. package/dist/utils/merge-dynamic-module-configs.js +14 -49
  61. package/dist/utils/msgpackrs.d.ts +2 -4
  62. package/dist/utils/msgpackrs.js +11 -35
  63. package/dist/utils/types.d.ts +23 -26
  64. package/dist/utils/types.js +2 -15
  65. package/package.json +7 -7
@@ -1,18 +1,9 @@
1
- import * as _nestjs_common from '@nestjs/common';
2
- import { AuthzProviderClass } from '../authz.provider.js';
3
- import { CookieOptionsWithSecret, DeepReadonly } from '../utils/types.js';
4
1
  import { AsyncLocalStorage } from 'node:async_hooks';
5
- import { JwtAlsType } from './jwt-authz-als.middleware.js';
6
- import { JwtAuthzOptions } from './jwt-authz.interface.js';
7
- import '@nestjs/common/interfaces';
8
- import 'express';
9
- import '../constants.js';
10
- import 'crypto';
11
- import 'jsonwebtoken';
12
- import './extract-jwt.js';
13
- import 'cookie';
14
-
15
- declare const createJwtAuthzService: <P = unknown, U = unknown>([AUTHZ_PROVIDER, JWT_AUTHZ_OPTIONS, ALS_PROVIDER]: [any, any, any]) => _nestjs_common.Type<Omit<{
2
+ import { AuthzProviderClass } from '../authz.provider';
3
+ import { type DeepReadonly } from '../utils';
4
+ import type { JwtAlsType } from './jwt-authz-als.middleware';
5
+ import type { JwtAuthzOptions } from './jwt-authz.interface';
6
+ export declare const createJwtAuthzService: <P = unknown, U = unknown>([AUTHZ_PROVIDER, JWT_AUTHZ_OPTIONS, ALS_PROVIDER]: [any, any, any]) => import("@nestjs/common").Type<Omit<{
16
7
  readonly authzProvider: AuthzProviderClass<P, U>;
17
8
  readonly jwtAuthzOptions: JwtAuthzOptions;
18
9
  readonly als: AsyncLocalStorage<JwtAlsType<U>>;
@@ -44,11 +35,9 @@ declare const createJwtAuthzService: <P = unknown, U = unknown>([AUTHZ_PROVIDER,
44
35
  /**
45
36
  * Sets a secure HTTP cookie with the given name, value, and optional cookie options.
46
37
  */
47
- setCookie(name: string, value: string, options?: CookieOptionsWithSecret | undefined): void;
38
+ setCookie(name: string, value: string, options?: import("../utils").CookieOptionsWithSecret | undefined): void;
48
39
  /**
49
40
  * Retrieves the current user associated with the request, if available.
50
41
  */
51
42
  getUser(): DeepReadonly<U> | undefined;
52
43
  }, "als" | "jwtAuthzOptions" | "authzProvider">>;
53
-
54
- export { createJwtAuthzService };
@@ -1,165 +1,119 @@
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 });
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;
12
7
  };
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;
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
20
10
  };
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 jwt_authz_service_exports = {};
32
- __export(jwt_authz_service_exports, {
33
- createJwtAuthzService: () => createJwtAuthzService
34
- });
35
- module.exports = __toCommonJS(jwt_authz_service_exports);
36
- var import_node_async_hooks = require("node:async_hooks");
37
- var import_common = require("@nestjs/common");
38
- var import_jsonwebtoken = __toESM(require("jsonwebtoken"));
39
- var import_authz = require("../authz.provider");
40
- var import_constants = require("../constants");
41
- var import_errors = require("../errors");
42
- var import_utils = require("../utils");
43
- function _ts_decorate(decorators, target, key, desc) {
44
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
45
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
46
- 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;
47
- return c > 3 && r && Object.defineProperty(target, key, r), r;
48
- }
49
- __name(_ts_decorate, "_ts_decorate");
50
- function _ts_metadata(k, v) {
51
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
52
- }
53
- __name(_ts_metadata, "_ts_metadata");
54
- function _ts_param(paramIndex, decorator) {
55
- return function(target, key) {
56
- decorator(target, key, paramIndex);
57
- };
58
- }
59
- __name(_ts_param, "_ts_param");
60
- const createJwtAuthzService = /* @__PURE__ */ __name(([AUTHZ_PROVIDER, JWT_AUTHZ_OPTIONS, ALS_PROVIDER]) => {
61
- var _a;
62
- let JwtAuthzService = (_a = class {
63
- constructor(authzProvider, jwtAuthzOptions, als) {
64
- __publicField(this, "authzProvider");
65
- __publicField(this, "jwtAuthzOptions");
66
- __publicField(this, "als");
67
- this.authzProvider = authzProvider;
68
- this.jwtAuthzOptions = jwtAuthzOptions;
69
- this.als = als;
70
- if (typeof this.authzProvider.createPayload !== "function") {
71
- throw new import_errors.AuthzError(`InternalError: Method 'createPayload' from abstract class 'AuthzProvider' must be implemented.`);
72
- }
73
- if (!jwtAuthzOptions.jwt?.sign) {
74
- throw new import_errors.AuthzError(`InternalError: Missing JWT sign options.`);
75
- }
76
- if (this.jwtAuthzOptions.refresh && !this.jwtAuthzOptions.refresh.sign) {
77
- throw new import_errors.AuthzError(`InternalError: Missing Refresh sign options.`);
78
- }
79
- }
80
- /**
81
- * Creates a JWT token with a payload generated by AuthzProviderClass.createPayload(). Optionally, includes a refresh token if configured.
82
- *
83
- * @param user - User entity
84
- * @returns
85
- * - `token` : The generated JWT access token.
86
- * - `refresh` (optional): The generated refresh token, if enabled.
87
- */
88
- async logIn(user) {
89
- const payload = await this.authzProvider.createPayload(user);
90
- const token = import_jsonwebtoken.default.sign(payload, this.jwtAuthzOptions.jwt.secretOrPrivateKey, this.jwtAuthzOptions.jwt.sign);
91
- if (this.jwtAuthzOptions.refresh) {
92
- const refresh = import_jsonwebtoken.default.sign({
93
- data: (0, import_utils.encodeMsgpackrString)(payload)
94
- }, this.jwtAuthzOptions.refresh.secretOrPrivateKey, this.jwtAuthzOptions.refresh.sign);
95
- return {
96
- token,
97
- refresh
98
- };
99
- }
100
- return {
101
- token
102
- };
103
- }
104
- /**
105
- * Refreshes the JWT token for the provided user. If no user is provided, it attempts to retrieve the
106
- * current user and generate a new token.
107
- *
108
- * @param [user] - User entity
109
- * @returns
110
- */
111
- async refresh(user) {
112
- if (!this.jwtAuthzOptions.refresh) {
113
- console.warn(`'refresh' method can only be called when configured in module options.`);
114
- return void 0;
115
- }
116
- let userParams = user;
117
- if (!user) {
118
- const store = (0, import_utils.getAlsStore)(this.als);
119
- if (store.jwtVerifiedBy !== import_constants.JwtValidationType.REFRESH) {
120
- throw new import_errors.AuthzError(`InvocationError: Calling 'refresh' method without user parameter can only be called under @Refresh().`);
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
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.createJwtAuthzService = void 0;
19
+ const node_async_hooks_1 = require("node:async_hooks");
20
+ const common_1 = require("@nestjs/common");
21
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
22
+ const authz_provider_1 = require("../authz.provider");
23
+ const constants_1 = require("../constants");
24
+ const errors_1 = require("../errors");
25
+ const utils_1 = require("../utils");
26
+ const createJwtAuthzService = ([AUTHZ_PROVIDER, JWT_AUTHZ_OPTIONS, ALS_PROVIDER]) => {
27
+ let JwtAuthzService = class JwtAuthzService {
28
+ constructor(authzProvider, jwtAuthzOptions, als) {
29
+ this.authzProvider = authzProvider;
30
+ this.jwtAuthzOptions = jwtAuthzOptions;
31
+ this.als = als;
32
+ if (typeof this.authzProvider.createPayload !== 'function') {
33
+ throw new errors_1.AuthzError(`InternalError: Method 'createPayload' from abstract class 'AuthzProvider' must be implemented.`);
34
+ }
35
+ if (!jwtAuthzOptions.jwt?.sign) {
36
+ throw new errors_1.AuthzError(`InternalError: Missing JWT sign options.`);
37
+ }
38
+ if (this.jwtAuthzOptions.refresh && !this.jwtAuthzOptions.refresh.sign) {
39
+ throw new errors_1.AuthzError(`InternalError: Missing Refresh sign options.`);
40
+ }
41
+ }
42
+ /**
43
+ * Creates a JWT token with a payload generated by AuthzProviderClass.createPayload(). Optionally, includes a refresh token if configured.
44
+ *
45
+ * @param user - User entity
46
+ * @returns
47
+ * - `token` : The generated JWT access token.
48
+ * - `refresh` (optional): The generated refresh token, if enabled.
49
+ */
50
+ async logIn(user) {
51
+ const payload = (await this.authzProvider.createPayload(user));
52
+ const token = jsonwebtoken_1.default.sign(payload, this.jwtAuthzOptions.jwt.secretOrPrivateKey, this.jwtAuthzOptions.jwt.sign);
53
+ if (this.jwtAuthzOptions.refresh) {
54
+ const refresh = jsonwebtoken_1.default.sign({
55
+ data: (0, utils_1.encodeMsgpackrString)(payload)
56
+ }, this.jwtAuthzOptions.refresh.secretOrPrivateKey, this.jwtAuthzOptions.refresh.sign);
57
+ return {
58
+ token,
59
+ refresh
60
+ };
61
+ }
62
+ return {
63
+ token
64
+ };
65
+ }
66
+ /**
67
+ * Refreshes the JWT token for the provided user. If no user is provided, it attempts to retrieve the
68
+ * current user and generate a new token.
69
+ *
70
+ * @param [user] - User entity
71
+ * @returns
72
+ */
73
+ async refresh(user) {
74
+ if (!this.jwtAuthzOptions.refresh) {
75
+ console.warn(`'refresh' method can only be called when configured in module options.`);
76
+ return undefined;
77
+ }
78
+ let userParams = user;
79
+ if (!user) {
80
+ const store = (0, utils_1.getAlsStore)(this.als);
81
+ if (store.jwtVerifiedBy !== constants_1.JwtValidationType.REFRESH) {
82
+ throw new errors_1.AuthzError(`InvocationError: Calling 'refresh' method without user parameter can only be called under @Refresh().`);
83
+ }
84
+ userParams = store.user;
85
+ }
86
+ if (!userParams) {
87
+ throw new errors_1.AuthzError(`ParameterError: User data is undefined.`);
88
+ }
89
+ const payload = (await this.authzProvider.createPayload(userParams));
90
+ const token = jsonwebtoken_1.default.sign(payload, this.jwtAuthzOptions.jwt.secretOrPrivateKey, this.jwtAuthzOptions.jwt.sign);
91
+ return {
92
+ token
93
+ };
121
94
  }
122
- userParams = store.user;
123
- }
124
- if (!userParams) {
125
- throw new import_errors.AuthzError(`ParameterError: User data is undefined.`);
126
- }
127
- const payload = await this.authzProvider.createPayload(userParams);
128
- const token = import_jsonwebtoken.default.sign(payload, this.jwtAuthzOptions.jwt.secretOrPrivateKey, this.jwtAuthzOptions.jwt.sign);
129
- return {
130
- token
131
- };
132
- }
133
- /**
134
- * Sets a secure HTTP cookie with the given name, value, and optional cookie options.
135
- */
136
- setCookie(...rest) {
137
- const store = (0, import_utils.getAlsStore)(this.als);
138
- store.setCookie(...rest);
139
- }
140
- /**
141
- * Retrieves the current user associated with the request, if available.
142
- */
143
- getUser() {
144
- const store = (0, import_utils.getAlsStore)(this.als);
145
- const user = store.user;
146
- return user;
147
- }
148
- }, __name(_a, "JwtAuthzService"), _a);
149
- JwtAuthzService = _ts_decorate([
150
- _ts_param(0, (0, import_common.Inject)(AUTHZ_PROVIDER)),
151
- _ts_param(1, (0, import_common.Inject)(JWT_AUTHZ_OPTIONS)),
152
- _ts_param(2, (0, import_common.Inject)(ALS_PROVIDER)),
153
- _ts_metadata("design:type", Function),
154
- _ts_metadata("design:paramtypes", [
155
- typeof import_authz.AuthzProviderClass === "undefined" ? Object : import_authz.AuthzProviderClass,
156
- typeof JwtAuthzOptions === "undefined" ? Object : JwtAuthzOptions,
157
- typeof import_node_async_hooks.AsyncLocalStorage === "undefined" ? Object : import_node_async_hooks.AsyncLocalStorage
158
- ])
159
- ], JwtAuthzService);
160
- return (0, import_common.mixin)(JwtAuthzService);
161
- }, "createJwtAuthzService");
162
- // Annotate the CommonJS export names for ESM import in node:
163
- 0 && (module.exports = {
164
- createJwtAuthzService
165
- });
95
+ /**
96
+ * Sets a secure HTTP cookie with the given name, value, and optional cookie options.
97
+ */
98
+ setCookie(...rest) {
99
+ const store = (0, utils_1.getAlsStore)(this.als);
100
+ store.setCookie(...rest);
101
+ }
102
+ /**
103
+ * Retrieves the current user associated with the request, if available.
104
+ */
105
+ getUser() {
106
+ const store = (0, utils_1.getAlsStore)(this.als);
107
+ const user = store.user;
108
+ return user;
109
+ }
110
+ };
111
+ JwtAuthzService = __decorate([
112
+ __param(0, (0, common_1.Inject)(AUTHZ_PROVIDER)),
113
+ __param(1, (0, common_1.Inject)(JWT_AUTHZ_OPTIONS)),
114
+ __param(2, (0, common_1.Inject)(ALS_PROVIDER)),
115
+ __metadata("design:paramtypes", [authz_provider_1.AuthzProviderClass, Object, node_async_hooks_1.AsyncLocalStorage])
116
+ ], JwtAuthzService);
117
+ return (0, common_1.mixin)(JwtAuthzService);
118
+ };
119
+ exports.createJwtAuthzService = createJwtAuthzService;
@@ -1,28 +1,16 @@
1
- import * as _nestjs_common from '@nestjs/common';
2
1
  import { AsyncLocalStorage } from 'node:async_hooks';
3
- import { Request } from 'express';
4
- import { AuthzProviderClass } from '../authz.provider.js';
5
- import { JwtAlsType } from './jwt-authz-als.middleware.js';
6
- import '../constants.js';
7
- import '../utils/types.js';
8
- import '@nestjs/common/interfaces';
9
- import './jwt-authz.interface.js';
10
- import 'crypto';
11
- import 'jsonwebtoken';
12
- import './extract-jwt.js';
13
- import 'cookie';
14
-
15
- declare const createJwtStrategy: ([JWT_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]: [string, any, any]) => _nestjs_common.Type<Omit<{
2
+ import type { Request } from 'express';
3
+ import { AuthzProviderClass } from '../authz.provider';
4
+ import type { JwtAlsType } from './jwt-authz-als.middleware';
5
+ export declare const createJwtStrategy: ([JWT_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]: [string, any, any]) => import("@nestjs/common").Type<Omit<{
16
6
  readonly authzProvider: AuthzProviderClass<unknown, unknown>;
17
7
  readonly als: AsyncLocalStorage<JwtAlsType<unknown>>;
18
8
  validate(req: Request): Promise<{}>;
19
9
  authenticate(req: Request, options?: any): any;
20
10
  }, "als" | "authzProvider">>;
21
- declare const createRefreshStrategy: ([JWT_REFRESH_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]: [string, any, any]) => _nestjs_common.Type<Omit<{
11
+ export declare const createRefreshStrategy: ([JWT_REFRESH_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]: [string, any, any]) => import("@nestjs/common").Type<Omit<{
22
12
  readonly authzProvider: AuthzProviderClass<unknown, unknown>;
23
13
  readonly als: AsyncLocalStorage<JwtAlsType<unknown>>;
24
14
  validate(req: Request): Promise<{}>;
25
15
  authenticate(req: Request, options?: any): any;
26
16
  }, "als" | "authzProvider">>;
27
-
28
- export { createJwtStrategy, createRefreshStrategy };