@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.
Files changed (66) hide show
  1. package/README.md +0 -0
  2. package/dist/authz.provider.d.ts +7 -0
  3. package/dist/authz.provider.js +34 -0
  4. package/dist/constants.d.ts +11 -0
  5. package/dist/constants.js +46 -0
  6. package/dist/errors.d.ts +12 -0
  7. package/dist/errors.js +60 -0
  8. package/dist/index.d.ts +21 -0
  9. package/dist/index.js +43 -0
  10. package/dist/jwt/extract-jwt.d.ts +71 -0
  11. package/dist/jwt/extract-jwt.js +162 -0
  12. package/dist/jwt/index.d.ts +16 -0
  13. package/dist/jwt/index.js +30 -0
  14. package/dist/jwt/jwt-authz-als.middleware.d.ts +28 -0
  15. package/dist/jwt/jwt-authz-als.middleware.js +83 -0
  16. package/dist/jwt/jwt-authz.guard.d.ts +60 -0
  17. package/dist/jwt/jwt-authz.guard.js +182 -0
  18. package/dist/jwt/jwt-authz.interface.d.ts +58 -0
  19. package/dist/jwt/jwt-authz.interface.js +94 -0
  20. package/dist/jwt/jwt-authz.module.d.ts +80 -0
  21. package/dist/jwt/jwt-authz.module.js +244 -0
  22. package/dist/jwt/jwt-authz.service.d.ts +33 -0
  23. package/dist/jwt/jwt-authz.service.js +144 -0
  24. package/dist/jwt/jwt-authz.strategy.d.ts +40 -0
  25. package/dist/jwt/jwt-authz.strategy.js +194 -0
  26. package/dist/session/index.d.ts +12 -0
  27. package/dist/session/index.js +27 -0
  28. package/dist/session/session-authz-als.middleware.d.ts +25 -0
  29. package/dist/session/session-authz-als.middleware.js +134 -0
  30. package/dist/session/session-authz.guard.d.ts +43 -0
  31. package/dist/session/session-authz.guard.js +142 -0
  32. package/dist/session/session-authz.interface.d.ts +23 -0
  33. package/dist/session/session-authz.interface.js +43 -0
  34. package/dist/session/session-authz.module.d.ts +71 -0
  35. package/dist/session/session-authz.module.js +245 -0
  36. package/dist/session/session-authz.service.d.ts +20 -0
  37. package/dist/session/session-authz.service.js +93 -0
  38. package/dist/session/session-authz.strategy.d.ts +24 -0
  39. package/dist/session/session-authz.strategy.js +107 -0
  40. package/dist/user.decorator.d.ts +6 -0
  41. package/dist/user.decorator.js +36 -0
  42. package/dist/utils/cookie-parsers.d.ts +12 -0
  43. package/dist/utils/cookie-parsers.js +77 -0
  44. package/dist/utils/create-authz-decorator-factory.d.ts +9 -0
  45. package/dist/utils/create-authz-decorator-factory.js +56 -0
  46. package/dist/utils/create-set-cookie-fn.d.ts +9 -0
  47. package/dist/utils/create-set-cookie-fn.js +46 -0
  48. package/dist/utils/generics.d.ts +6 -0
  49. package/dist/utils/generics.js +66 -0
  50. package/dist/utils/get-allow-anonymous.d.ts +11 -0
  51. package/dist/utils/get-allow-anonymous.js +30 -0
  52. package/dist/utils/get-als-store.d.ts +5 -0
  53. package/dist/utils/get-als-store.js +35 -0
  54. package/dist/utils/get-context-authz-meta-params-list.d.ts +12 -0
  55. package/dist/utils/get-context-authz-meta-params-list.js +36 -0
  56. package/dist/utils/get-passport-property.d.ts +3 -0
  57. package/dist/utils/get-passport-property.js +31 -0
  58. package/dist/utils/index.d.ts +16 -0
  59. package/dist/utils/index.js +41 -0
  60. package/dist/utils/merge-dynamic-module-configs.d.ts +5 -0
  61. package/dist/utils/merge-dynamic-module-configs.js +50 -0
  62. package/dist/utils/msgpackrs.d.ts +4 -0
  63. package/dist/utils/msgpackrs.js +37 -0
  64. package/dist/utils/types.d.ts +56 -0
  65. package/dist/utils/types.js +15 -0
  66. package/package.json +68 -0
package/README.md ADDED
File without changes
@@ -0,0 +1,7 @@
1
+ declare abstract class AuthzProviderClass<Payload, User> {
2
+ abstract createPayload(user: User): Payload | Promise<Payload>;
3
+ abstract authenticate(payload: Payload): User | Promise<User>;
4
+ authorize(_uesr: User, _metaData?: unknown): boolean | Promise<boolean>;
5
+ }
6
+
7
+ export { AuthzProviderClass };
@@ -0,0 +1,34 @@
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 authz_provider_exports = {};
20
+ __export(authz_provider_exports, {
21
+ AuthzProviderClass: () => AuthzProviderClass
22
+ });
23
+ module.exports = __toCommonJS(authz_provider_exports);
24
+ const _AuthzProviderClass = class _AuthzProviderClass {
25
+ authorize(_uesr, _metaData) {
26
+ return true;
27
+ }
28
+ };
29
+ __name(_AuthzProviderClass, "AuthzProviderClass");
30
+ let AuthzProviderClass = _AuthzProviderClass;
31
+ // Annotate the CommonJS export names for ESM import in node:
32
+ 0 && (module.exports = {
33
+ AuthzProviderClass
34
+ });
@@ -0,0 +1,11 @@
1
+ declare const DEFAULT_PASSPORT_PROPERTY_VALUE = "user";
2
+ declare const PASSPORT_PROPERTY: unique symbol;
3
+ declare const PREFIX = "`<`:";
4
+ declare const ROUTES_OPTIONS: unique symbol;
5
+ declare enum JwtValidationType {
6
+ JWT = 0,
7
+ REFRESH = 1
8
+ }
9
+ declare const SESSION_PASSPORT_KEY = "passport";
10
+
11
+ export { DEFAULT_PASSPORT_PROPERTY_VALUE, JwtValidationType, PASSPORT_PROPERTY, PREFIX, ROUTES_OPTIONS, SESSION_PASSPORT_KEY };
@@ -0,0 +1,46 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var constants_exports = {};
19
+ __export(constants_exports, {
20
+ DEFAULT_PASSPORT_PROPERTY_VALUE: () => DEFAULT_PASSPORT_PROPERTY_VALUE,
21
+ JwtValidationType: () => JwtValidationType,
22
+ PASSPORT_PROPERTY: () => PASSPORT_PROPERTY,
23
+ PREFIX: () => PREFIX,
24
+ ROUTES_OPTIONS: () => ROUTES_OPTIONS,
25
+ SESSION_PASSPORT_KEY: () => SESSION_PASSPORT_KEY
26
+ });
27
+ module.exports = __toCommonJS(constants_exports);
28
+ const DEFAULT_PASSPORT_PROPERTY_VALUE = "user";
29
+ const PASSPORT_PROPERTY = Symbol("PASSPORT_PROPERTY");
30
+ const PREFIX = "`<`:";
31
+ const ROUTES_OPTIONS = Symbol("ROUTES_OPTIONS");
32
+ var JwtValidationType = /* @__PURE__ */ function(JwtValidationType2) {
33
+ JwtValidationType2[JwtValidationType2["JWT"] = 0] = "JWT";
34
+ JwtValidationType2[JwtValidationType2["REFRESH"] = 1] = "REFRESH";
35
+ return JwtValidationType2;
36
+ }({});
37
+ const SESSION_PASSPORT_KEY = "passport";
38
+ // Annotate the CommonJS export names for ESM import in node:
39
+ 0 && (module.exports = {
40
+ DEFAULT_PASSPORT_PROPERTY_VALUE,
41
+ JwtValidationType,
42
+ PASSPORT_PROPERTY,
43
+ PREFIX,
44
+ ROUTES_OPTIONS,
45
+ SESSION_PASSPORT_KEY
46
+ });
@@ -0,0 +1,12 @@
1
+ declare class AuthzError extends Error {
2
+ cause?: unknown;
3
+ constructor(message?: string, cause?: unknown);
4
+ }
5
+ declare class AuthzVerificationError extends AuthzError {
6
+ constructor(message?: string, cause?: unknown);
7
+ }
8
+ declare class AuthzAnonymousError extends AuthzError {
9
+ constructor(message?: string, cause?: unknown);
10
+ }
11
+
12
+ export { AuthzAnonymousError, AuthzError, AuthzVerificationError };
package/dist/errors.js ADDED
@@ -0,0 +1,60 @@
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 errors_exports = {};
22
+ __export(errors_exports, {
23
+ AuthzAnonymousError: () => AuthzAnonymousError,
24
+ AuthzError: () => AuthzError,
25
+ AuthzVerificationError: () => AuthzVerificationError
26
+ });
27
+ module.exports = __toCommonJS(errors_exports);
28
+ const _AuthzError = class _AuthzError extends Error {
29
+ constructor(message, cause) {
30
+ super(message);
31
+ __publicField(this, "cause");
32
+ this.name = "AuthzError";
33
+ this.cause = cause;
34
+ Object.setPrototypeOf(this, new.target.prototype);
35
+ }
36
+ };
37
+ __name(_AuthzError, "AuthzError");
38
+ let AuthzError = _AuthzError;
39
+ const _AuthzVerificationError = class _AuthzVerificationError extends AuthzError {
40
+ constructor(message, cause) {
41
+ super(message, cause);
42
+ this.name = "AuthzVerificationError";
43
+ }
44
+ };
45
+ __name(_AuthzVerificationError, "AuthzVerificationError");
46
+ let AuthzVerificationError = _AuthzVerificationError;
47
+ const _AuthzAnonymousError = class _AuthzAnonymousError extends AuthzError {
48
+ constructor(message, cause) {
49
+ super(message, cause);
50
+ this.name = "AuthzAnonymousError";
51
+ }
52
+ };
53
+ __name(_AuthzAnonymousError, "AuthzAnonymousError");
54
+ let AuthzAnonymousError = _AuthzAnonymousError;
55
+ // Annotate the CommonJS export names for ESM import in node:
56
+ 0 && (module.exports = {
57
+ AuthzAnonymousError,
58
+ AuthzError,
59
+ AuthzVerificationError
60
+ });
@@ -0,0 +1,21 @@
1
+ export { AuthzProviderClass } from './authz.provider.js';
2
+ export { User } from './user.decorator.js';
3
+ export { AuthzError, AuthzVerificationError } from './errors.js';
4
+ export { ExtractJwt, JwtFromRequestFunction } from './jwt/extract-jwt.js';
5
+ export { createJwtAuthzModule } from './jwt/jwt-authz.module.js';
6
+ export { cereateSessionAuthzModule } from './session/session-authz.module.js';
7
+ import '@nestjs/common';
8
+ import 'cookie';
9
+ import '@nestjs/core';
10
+ import './jwt/jwt-authz.interface.js';
11
+ import 'crypto';
12
+ import 'jsonwebtoken';
13
+ import './utils/types.js';
14
+ import '@nestjs/common/interfaces';
15
+ import 'express';
16
+ import 'node:async_hooks';
17
+ import './jwt/jwt-authz-als.middleware.js';
18
+ import './constants.js';
19
+ import 'express-session';
20
+ import './session/session-authz-als.middleware.js';
21
+ import './session/session-authz.interface.js';
package/dist/index.js ADDED
@@ -0,0 +1,43 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var src_exports = {};
19
+ __export(src_exports, {
20
+ AuthzError: () => import_errors.AuthzError,
21
+ AuthzProviderClass: () => import_authz.AuthzProviderClass,
22
+ AuthzVerificationError: () => import_errors.AuthzVerificationError,
23
+ ExtractJwt: () => import_jwt.ExtractJwt,
24
+ User: () => import_user.User,
25
+ cereateSessionAuthzModule: () => import_session.cereateSessionAuthzModule,
26
+ createJwtAuthzModule: () => import_jwt.createJwtAuthzModule
27
+ });
28
+ module.exports = __toCommonJS(src_exports);
29
+ var import_authz = require("./authz.provider");
30
+ var import_user = require("./user.decorator");
31
+ var import_errors = require("./errors");
32
+ var import_jwt = require("./jwt");
33
+ var import_session = require("./session");
34
+ // Annotate the CommonJS export names for ESM import in node:
35
+ 0 && (module.exports = {
36
+ AuthzError,
37
+ AuthzProviderClass,
38
+ AuthzVerificationError,
39
+ ExtractJwt,
40
+ User,
41
+ cereateSessionAuthzModule,
42
+ createJwtAuthzModule
43
+ });
@@ -0,0 +1,71 @@
1
+ import { ParseOptions } from 'cookie';
2
+
3
+ declare const parseAuthHeader: (hdrValue: unknown) => {
4
+ scheme: string;
5
+ value: string;
6
+ } | null;
7
+ interface JwtFromRequestFunction<T = any> {
8
+ (req: T): string | null;
9
+ }
10
+ declare const ExtractJwt: {
11
+ /**
12
+ * Creates an extractor function to retrieve a token from the request header.
13
+ *
14
+ * @param {string} header_name - The name of the header to extract the token from.
15
+ * @returns {JwtFromRequestFunction} A function that takes a request object and returns the extracted token.
16
+ */
17
+ fromHeader: (header_name: string) => JwtFromRequestFunction;
18
+ /**
19
+ * Creates an extractor function to retrieve a token from a field in the request body.
20
+ *
21
+ * @param {string} field_name - The name of the field to extract the token from.
22
+ * @returns {JwtFromRequestFunction} A function that takes a request object and returns the extracted token.
23
+ */
24
+ fromBodyField: (field_name: string) => JwtFromRequestFunction;
25
+ /**
26
+ * Creates an extractor function to retrieve a token from a query parameter in the URL.
27
+ *
28
+ * @param {string} param_name - The name of the query parameter to extract the token from.
29
+ * @returns {JwtFromRequestFunction} A function that takes a request object and returns the extracted token.
30
+ */
31
+ fromUrlQueryParameter: (param_name: string) => JwtFromRequestFunction;
32
+ /**
33
+ * Creates an extractor function to retrieve a token from the authorization header with a specific scheme.
34
+ *
35
+ * @param {string} auth_scheme - The authorization scheme (e.g., 'Bearer').
36
+ * @returns {JwtFromRequestFunction} A function that takes a request object and returns the extracted token.
37
+ */
38
+ fromAuthHeaderWithScheme: (auth_scheme: string) => JwtFromRequestFunction;
39
+ /**
40
+ * Creates an extractor function to retrieve a token from the authorization header as a Bearer token.
41
+ *
42
+ * @returns {JwtFromRequestFunction} A function that takes a request object and returns the extracted token.
43
+ */
44
+ fromAuthHeaderAsBearerToken: () => JwtFromRequestFunction;
45
+ /**
46
+ * Creates an extractor function to retrieve a token from the request header. Respects cookie-parser middleware if applied.
47
+ *
48
+ * @param cookie_name - The name of the cookie to extract the token from.
49
+ * @param options - Options to parse request cookie header.
50
+ * @returns A function that takes a request object and returns the extracted token.
51
+ */
52
+ fromCookies: (cookie_name: string, options?: {
53
+ /**
54
+ * a string or array used for parsing signed cookies.
55
+ */
56
+ secret?: string | string[];
57
+ /**
58
+ * Extract token from signed cookie. Default to true if secret was provided.
59
+ */
60
+ signed?: boolean;
61
+ } & ParseOptions) => JwtFromRequestFunction;
62
+ /**
63
+ * Creates an extractor function that combines multiple extractor functions.
64
+ *
65
+ * @param {JwtFromRequestFunction[]} extractors - An array of extractor functions.
66
+ * @returns {JwtFromRequestFunction} A function that takes a request object and returns the extracted token.
67
+ */
68
+ fromExtractors: (extractors: JwtFromRequestFunction[]) => JwtFromRequestFunction;
69
+ };
70
+
71
+ export { ExtractJwt, type JwtFromRequestFunction, parseAuthHeader };
@@ -0,0 +1,162 @@
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 extract_jwt_exports = {};
20
+ __export(extract_jwt_exports, {
21
+ ExtractJwt: () => ExtractJwt,
22
+ parseAuthHeader: () => parseAuthHeader
23
+ });
24
+ module.exports = __toCommonJS(extract_jwt_exports);
25
+ var import_node_url = require("node:url");
26
+ var import_utils = require("../utils");
27
+ const re = /(\S+)\s+(\S+)/;
28
+ const parseAuthHeader = /* @__PURE__ */ __name((hdrValue) => {
29
+ if (typeof hdrValue !== "string") {
30
+ return null;
31
+ }
32
+ var matches = hdrValue.match(re);
33
+ return matches && {
34
+ scheme: matches[1],
35
+ value: matches[2]
36
+ };
37
+ }, "parseAuthHeader");
38
+ const AUTH_HEADER = "authorization";
39
+ const BEARER_AUTH_SCHEME = "bearer";
40
+ const ExtractJwt = {
41
+ /**
42
+ * Creates an extractor function to retrieve a token from the request header.
43
+ *
44
+ * @param {string} header_name - The name of the header to extract the token from.
45
+ * @returns {JwtFromRequestFunction} A function that takes a request object and returns the extracted token.
46
+ */
47
+ fromHeader: /* @__PURE__ */ __name((header_name) => {
48
+ return function(request) {
49
+ let token = null;
50
+ if (request.headers[header_name]) {
51
+ token = request.headers[header_name];
52
+ }
53
+ return token;
54
+ };
55
+ }, "fromHeader"),
56
+ /**
57
+ * Creates an extractor function to retrieve a token from a field in the request body.
58
+ *
59
+ * @param {string} field_name - The name of the field to extract the token from.
60
+ * @returns {JwtFromRequestFunction} A function that takes a request object and returns the extracted token.
61
+ */
62
+ fromBodyField: /* @__PURE__ */ __name((field_name) => {
63
+ return function(request) {
64
+ let token = null;
65
+ if (request.body && Object.prototype.hasOwnProperty.call(request.body, field_name)) {
66
+ token = request.body[field_name];
67
+ }
68
+ return token;
69
+ };
70
+ }, "fromBodyField"),
71
+ /**
72
+ * Creates an extractor function to retrieve a token from a query parameter in the URL.
73
+ *
74
+ * @param {string} param_name - The name of the query parameter to extract the token from.
75
+ * @returns {JwtFromRequestFunction} A function that takes a request object and returns the extracted token.
76
+ */
77
+ fromUrlQueryParameter: /* @__PURE__ */ __name((param_name) => {
78
+ return function(request) {
79
+ let token = null;
80
+ const parsed_url = (0, import_node_url.parse)(request.url, true);
81
+ if (parsed_url.query && Object.prototype.hasOwnProperty.call(parsed_url.query, param_name)) {
82
+ token = parsed_url.query[param_name];
83
+ }
84
+ return token;
85
+ };
86
+ }, "fromUrlQueryParameter"),
87
+ /**
88
+ * Creates an extractor function to retrieve a token from the authorization header with a specific scheme.
89
+ *
90
+ * @param {string} auth_scheme - The authorization scheme (e.g., 'Bearer').
91
+ * @returns {JwtFromRequestFunction} A function that takes a request object and returns the extracted token.
92
+ */
93
+ fromAuthHeaderWithScheme: /* @__PURE__ */ __name((auth_scheme) => {
94
+ var auth_scheme_lower = auth_scheme.toLowerCase();
95
+ return function(request) {
96
+ let token = null;
97
+ if (request.headers[AUTH_HEADER]) {
98
+ var auth_params = parseAuthHeader(request.headers[AUTH_HEADER]);
99
+ if (auth_params && auth_scheme_lower === auth_params.scheme.toLowerCase()) {
100
+ token = auth_params.value;
101
+ }
102
+ }
103
+ return token;
104
+ };
105
+ }, "fromAuthHeaderWithScheme"),
106
+ /**
107
+ * Creates an extractor function to retrieve a token from the authorization header as a Bearer token.
108
+ *
109
+ * @returns {JwtFromRequestFunction} A function that takes a request object and returns the extracted token.
110
+ */
111
+ fromAuthHeaderAsBearerToken: /* @__PURE__ */ __name(() => {
112
+ return ExtractJwt.fromAuthHeaderWithScheme(BEARER_AUTH_SCHEME);
113
+ }, "fromAuthHeaderAsBearerToken"),
114
+ /**
115
+ * Creates an extractor function to retrieve a token from the request header. Respects cookie-parser middleware if applied.
116
+ *
117
+ * @param cookie_name - The name of the cookie to extract the token from.
118
+ * @param options - Options to parse request cookie header.
119
+ * @returns A function that takes a request object and returns the extracted token.
120
+ */
121
+ fromCookies: /* @__PURE__ */ __name((cookie_name, options) => {
122
+ const cookie_name_lower = cookie_name.toLowerCase();
123
+ const { secret, decode } = options ?? {};
124
+ const secrets = (0, import_utils.normalizedArray)(secret) ?? [];
125
+ const signed = options?.signed ?? Boolean(secrets.length);
126
+ const targetParser = Boolean(secrets.length) ? import_utils.customCookieParser : import_utils.normalCookieParser;
127
+ return (req) => {
128
+ const { cookies, signedCookies } = targetParser(req, secrets, decode);
129
+ let token = null;
130
+ const targetCookies = signed ? signedCookies : cookies;
131
+ if (targetCookies[cookie_name_lower]) {
132
+ token = targetCookies[cookie_name_lower];
133
+ }
134
+ return token;
135
+ };
136
+ }, "fromCookies"),
137
+ /**
138
+ * Creates an extractor function that combines multiple extractor functions.
139
+ *
140
+ * @param {JwtFromRequestFunction[]} extractors - An array of extractor functions.
141
+ * @returns {JwtFromRequestFunction} A function that takes a request object and returns the extracted token.
142
+ */
143
+ fromExtractors: /* @__PURE__ */ __name((extractors) => {
144
+ if (!Array.isArray(extractors)) {
145
+ throw new TypeError("extractors.fromExtractors expects an array");
146
+ }
147
+ return function(request) {
148
+ let token = null;
149
+ let index = 0;
150
+ while (!token && index < extractors.length) {
151
+ token = extractors[index].call(this, request);
152
+ index++;
153
+ }
154
+ return token;
155
+ };
156
+ }, "fromExtractors")
157
+ };
158
+ // Annotate the CommonJS export names for ESM import in node:
159
+ 0 && (module.exports = {
160
+ ExtractJwt,
161
+ parseAuthHeader
162
+ });
@@ -0,0 +1,16 @@
1
+ export { ExtractJwt, JwtFromRequestFunction } from './extract-jwt.js';
2
+ export { createJwtAuthzModule } from './jwt-authz.module.js';
3
+ import 'cookie';
4
+ import '@nestjs/core';
5
+ import './jwt-authz.interface.js';
6
+ import 'crypto';
7
+ import 'jsonwebtoken';
8
+ import '../utils/types.js';
9
+ import '@nestjs/common';
10
+ import '@nestjs/common/interfaces';
11
+ import 'express';
12
+ import '../authz.provider.js';
13
+ import 'node:async_hooks';
14
+ import '../errors.js';
15
+ import './jwt-authz-als.middleware.js';
16
+ import '../constants.js';
@@ -0,0 +1,30 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var jwt_exports = {};
19
+ __export(jwt_exports, {
20
+ ExtractJwt: () => import_extract_jwt.ExtractJwt,
21
+ createJwtAuthzModule: () => import_jwt_authz.createJwtAuthzModule
22
+ });
23
+ module.exports = __toCommonJS(jwt_exports);
24
+ var import_extract_jwt = require("./extract-jwt");
25
+ var import_jwt_authz = require("./jwt-authz.module");
26
+ // Annotate the CommonJS export names for ESM import in node:
27
+ 0 && (module.exports = {
28
+ ExtractJwt,
29
+ createJwtAuthzModule
30
+ });
@@ -0,0 +1,28 @@
1
+ import * as _nestjs_common from '@nestjs/common';
2
+ import { AsyncLocalStorage } from 'node:async_hooks';
3
+ import { Request, Response, NextFunction } from 'express';
4
+ import { JwtValidationType } from '../constants.js';
5
+ import { CookieOptionsWithSecret } from '../utils/types.js';
6
+ import { JwtAuthzOptions } from './jwt-authz.interface.js';
7
+ import '@nestjs/common/interfaces';
8
+ import '../authz.provider.js';
9
+ import 'crypto';
10
+ import 'jsonwebtoken';
11
+ import './extract-jwt.js';
12
+ import 'cookie';
13
+
14
+ interface JwtAlsType<U> {
15
+ user?: U;
16
+ jwtVerifiedBy?: JwtValidationType;
17
+ allowAnonymous?: boolean;
18
+ guardResult?: boolean;
19
+ authOptions: JwtAuthzOptions;
20
+ setCookie: (name: string, value: string, options?: CookieOptionsWithSecret) => void;
21
+ }
22
+ declare const createJwtAuthzAlsMiddleware: ([ALS_PROVIDER, JWT_AUTHZ_OPTIONS]: [any, any]) => _nestjs_common.Type<Omit<{
23
+ readonly als: AsyncLocalStorage<JwtAlsType<unknown>>;
24
+ readonly jwtAuthzOptions: JwtAuthzOptions;
25
+ use(req: Request, res: Response, next: NextFunction): void;
26
+ }, "als" | "jwtAuthzOptions">>;
27
+
28
+ export { type JwtAlsType, createJwtAuthzAlsMiddleware };
@@ -0,0 +1,83 @@
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 jwt_authz_als_middleware_exports = {};
22
+ __export(jwt_authz_als_middleware_exports, {
23
+ createJwtAuthzAlsMiddleware: () => createJwtAuthzAlsMiddleware
24
+ });
25
+ module.exports = __toCommonJS(jwt_authz_als_middleware_exports);
26
+ var import_node_async_hooks = require("node:async_hooks");
27
+ var import_common = require("@nestjs/common");
28
+ var import_utils = require("../utils");
29
+ function _ts_decorate(decorators, target, key, desc) {
30
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
31
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
32
+ 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;
33
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
34
+ }
35
+ __name(_ts_decorate, "_ts_decorate");
36
+ function _ts_metadata(k, v) {
37
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
38
+ }
39
+ __name(_ts_metadata, "_ts_metadata");
40
+ function _ts_param(paramIndex, decorator) {
41
+ return function(target, key) {
42
+ decorator(target, key, paramIndex);
43
+ };
44
+ }
45
+ __name(_ts_param, "_ts_param");
46
+ const createJwtAuthzAlsMiddleware = /* @__PURE__ */ __name(([ALS_PROVIDER, JWT_AUTHZ_OPTIONS]) => {
47
+ var _a;
48
+ let JwtAuthzAlsMiddleware = (_a = class {
49
+ constructor(als, jwtAuthzOptions) {
50
+ __publicField(this, "als");
51
+ __publicField(this, "jwtAuthzOptions");
52
+ this.als = als;
53
+ this.jwtAuthzOptions = jwtAuthzOptions;
54
+ }
55
+ use(req, res, next) {
56
+ this.als.run({
57
+ user: void 0,
58
+ jwtVerifiedBy: void 0,
59
+ allowAnonymous: void 0,
60
+ guardResult: void 0,
61
+ // a workaround to pass jwtAuthzOptions to passport strategy.
62
+ authOptions: this.jwtAuthzOptions,
63
+ setCookie: (0, import_utils.createSetCookieFn)(req, res)
64
+ }, () => {
65
+ next();
66
+ });
67
+ }
68
+ }, __name(_a, "JwtAuthzAlsMiddleware"), _a);
69
+ JwtAuthzAlsMiddleware = _ts_decorate([
70
+ _ts_param(0, (0, import_common.Inject)(ALS_PROVIDER)),
71
+ _ts_param(1, (0, import_common.Inject)(JWT_AUTHZ_OPTIONS)),
72
+ _ts_metadata("design:type", Function),
73
+ _ts_metadata("design:paramtypes", [
74
+ typeof import_node_async_hooks.AsyncLocalStorage === "undefined" ? Object : import_node_async_hooks.AsyncLocalStorage,
75
+ typeof JwtAuthzOptions === "undefined" ? Object : JwtAuthzOptions
76
+ ])
77
+ ], JwtAuthzAlsMiddleware);
78
+ return (0, import_common.mixin)(JwtAuthzAlsMiddleware);
79
+ }, "createJwtAuthzAlsMiddleware");
80
+ // Annotate the CommonJS export names for ESM import in node:
81
+ 0 && (module.exports = {
82
+ createJwtAuthzAlsMiddleware
83
+ });