@globaltracking/auth-middleware 2.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 (118) hide show
  1. package/README.md +372 -0
  2. package/dist/config.d.ts +9 -0
  3. package/dist/config.d.ts.map +1 -0
  4. package/dist/config.js +110 -0
  5. package/dist/config.js.map +1 -0
  6. package/dist/errors.d.ts +26 -0
  7. package/dist/errors.d.ts.map +1 -0
  8. package/dist/errors.js +42 -0
  9. package/dist/errors.js.map +1 -0
  10. package/dist/extract-user.d.ts +10 -0
  11. package/dist/extract-user.d.ts.map +1 -0
  12. package/dist/extract-user.js +30 -0
  13. package/dist/extract-user.js.map +1 -0
  14. package/dist/index.d.ts +16 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +42 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/middlewares/authenticate.d.ts +10 -0
  19. package/dist/middlewares/authenticate.d.ts.map +1 -0
  20. package/dist/middlewares/authenticate.js +26 -0
  21. package/dist/middlewares/authenticate.js.map +1 -0
  22. package/dist/middlewares/error-handler.d.ts +12 -0
  23. package/dist/middlewares/error-handler.d.ts.map +1 -0
  24. package/dist/middlewares/error-handler.js +22 -0
  25. package/dist/middlewares/error-handler.js.map +1 -0
  26. package/dist/middlewares/require-permission.d.ts +12 -0
  27. package/dist/middlewares/require-permission.d.ts.map +1 -0
  28. package/dist/middlewares/require-permission.js +63 -0
  29. package/dist/middlewares/require-permission.js.map +1 -0
  30. package/dist/middlewares/require-role.d.ts +12 -0
  31. package/dist/middlewares/require-role.d.ts.map +1 -0
  32. package/dist/middlewares/require-role.js +30 -0
  33. package/dist/middlewares/require-role.js.map +1 -0
  34. package/dist/middlewares/require-self.d.ts +10 -0
  35. package/dist/middlewares/require-self.d.ts.map +1 -0
  36. package/dist/middlewares/require-self.js +40 -0
  37. package/dist/middlewares/require-self.js.map +1 -0
  38. package/dist/middlewares/require-tenant.d.ts +11 -0
  39. package/dist/middlewares/require-tenant.d.ts.map +1 -0
  40. package/dist/middlewares/require-tenant.js +25 -0
  41. package/dist/middlewares/require-tenant.js.map +1 -0
  42. package/dist/nestjs/constants.d.ts +5 -0
  43. package/dist/nestjs/constants.d.ts.map +1 -0
  44. package/dist/nestjs/constants.js +8 -0
  45. package/dist/nestjs/constants.js.map +1 -0
  46. package/dist/nestjs/decorators/index.d.ts +44 -0
  47. package/dist/nestjs/decorators/index.d.ts.map +1 -0
  48. package/dist/nestjs/decorators/index.js +61 -0
  49. package/dist/nestjs/decorators/index.js.map +1 -0
  50. package/dist/nestjs/filters/auth-exception.filter.d.ts +16 -0
  51. package/dist/nestjs/filters/auth-exception.filter.d.ts.map +1 -0
  52. package/dist/nestjs/filters/auth-exception.filter.js +37 -0
  53. package/dist/nestjs/filters/auth-exception.filter.js.map +1 -0
  54. package/dist/nestjs/gt-auth.module.d.ts +31 -0
  55. package/dist/nestjs/gt-auth.module.d.ts.map +1 -0
  56. package/dist/nestjs/gt-auth.module.js +102 -0
  57. package/dist/nestjs/gt-auth.module.js.map +1 -0
  58. package/dist/nestjs/guards/auth.guard.d.ts +13 -0
  59. package/dist/nestjs/guards/auth.guard.d.ts.map +1 -0
  60. package/dist/nestjs/guards/auth.guard.js +53 -0
  61. package/dist/nestjs/guards/auth.guard.js.map +1 -0
  62. package/dist/nestjs/guards/internal-only.guard.d.ts +15 -0
  63. package/dist/nestjs/guards/internal-only.guard.d.ts.map +1 -0
  64. package/dist/nestjs/guards/internal-only.guard.js +51 -0
  65. package/dist/nestjs/guards/internal-only.guard.js.map +1 -0
  66. package/dist/nestjs/guards/permissions.guard.d.ts +23 -0
  67. package/dist/nestjs/guards/permissions.guard.d.ts.map +1 -0
  68. package/dist/nestjs/guards/permissions.guard.js +134 -0
  69. package/dist/nestjs/guards/permissions.guard.js.map +1 -0
  70. package/dist/nestjs/index.d.ts +12 -0
  71. package/dist/nestjs/index.d.ts.map +1 -0
  72. package/dist/nestjs/index.js +40 -0
  73. package/dist/nestjs/index.js.map +1 -0
  74. package/dist/nestjs/interceptors/org-context.interceptor.d.ts +21 -0
  75. package/dist/nestjs/interceptors/org-context.interceptor.d.ts.map +1 -0
  76. package/dist/nestjs/interceptors/org-context.interceptor.js +63 -0
  77. package/dist/nestjs/interceptors/org-context.interceptor.js.map +1 -0
  78. package/dist/nestjs/middleware/trusted-headers.middleware.d.ts +15 -0
  79. package/dist/nestjs/middleware/trusted-headers.middleware.d.ts.map +1 -0
  80. package/dist/nestjs/middleware/trusted-headers.middleware.js +42 -0
  81. package/dist/nestjs/middleware/trusted-headers.middleware.js.map +1 -0
  82. package/dist/nestjs.d.ts +2 -0
  83. package/dist/nestjs.d.ts.map +1 -0
  84. package/dist/nestjs.js +18 -0
  85. package/dist/nestjs.js.map +1 -0
  86. package/dist/strategies/gateway-header.strategy.d.ts +13 -0
  87. package/dist/strategies/gateway-header.strategy.d.ts.map +1 -0
  88. package/dist/strategies/gateway-header.strategy.js +51 -0
  89. package/dist/strategies/gateway-header.strategy.js.map +1 -0
  90. package/dist/strategies/index.d.ts +5 -0
  91. package/dist/strategies/index.d.ts.map +1 -0
  92. package/dist/strategies/index.js +10 -0
  93. package/dist/strategies/index.js.map +1 -0
  94. package/dist/strategies/jwt.strategy.d.ts +13 -0
  95. package/dist/strategies/jwt.strategy.d.ts.map +1 -0
  96. package/dist/strategies/jwt.strategy.js +94 -0
  97. package/dist/strategies/jwt.strategy.js.map +1 -0
  98. package/dist/strategies/strategy.interface.d.ts +15 -0
  99. package/dist/strategies/strategy.interface.d.ts.map +1 -0
  100. package/dist/strategies/strategy.interface.js +3 -0
  101. package/dist/strategies/strategy.interface.js.map +1 -0
  102. package/dist/strategies/trusted-headers.strategy.d.ts +16 -0
  103. package/dist/strategies/trusted-headers.strategy.d.ts.map +1 -0
  104. package/dist/strategies/trusted-headers.strategy.js +50 -0
  105. package/dist/strategies/trusted-headers.strategy.js.map +1 -0
  106. package/dist/types.d.ts +78 -0
  107. package/dist/types.d.ts.map +1 -0
  108. package/dist/types.js +3 -0
  109. package/dist/types.js.map +1 -0
  110. package/dist/utils/helpers.d.ts +14 -0
  111. package/dist/utils/helpers.d.ts.map +1 -0
  112. package/dist/utils/helpers.js +26 -0
  113. package/dist/utils/helpers.js.map +1 -0
  114. package/dist/utils/jwt.d.ts +12 -0
  115. package/dist/utils/jwt.d.ts.map +1 -0
  116. package/dist/utils/jwt.js +104 -0
  117. package/dist/utils/jwt.js.map +1 -0
  118. package/package.json +82 -0
@@ -0,0 +1,102 @@
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;
7
+ };
8
+ var GtAuthModule_1;
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.GtAuthModule = void 0;
11
+ const common_1 = require("@nestjs/common");
12
+ const config_1 = require("../config");
13
+ const constants_1 = require("./constants");
14
+ const trusted_headers_middleware_1 = require("./middleware/trusted-headers.middleware");
15
+ const internal_only_guard_1 = require("./guards/internal-only.guard");
16
+ const permissions_guard_1 = require("./guards/permissions.guard");
17
+ const auth_guard_1 = require("./guards/auth.guard");
18
+ const org_context_interceptor_1 = require("./interceptors/org-context.interceptor");
19
+ const auth_exception_filter_1 = require("./filters/auth-exception.filter");
20
+ /**
21
+ * NestJS dynamic module for Global Tracking authentication and authorization.
22
+ *
23
+ * Provides: InternalOnlyGuard, GtPermissionsGuard, GtAuthGuard,
24
+ * OrgContextInterceptor, AuthExceptionFilter, and the GtTrustedHeadersMiddleware.
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * GtAuthModule.forRoot({
29
+ * strategies: ['trusted-headers'],
30
+ * internalGatewayToken: 'secret',
31
+ * adminRoles: ['system_admin', 'org_admin'],
32
+ * rbacServiceUrl: 'http://gt-rbac-service:3000',
33
+ * })
34
+ * ```
35
+ */
36
+ let GtAuthModule = GtAuthModule_1 = class GtAuthModule {
37
+ configure(consumer) {
38
+ consumer.apply(trusted_headers_middleware_1.GtTrustedHeadersMiddleware).forRoutes('*');
39
+ }
40
+ static forRoot(options = {}) {
41
+ (0, config_1.initAuth)(options);
42
+ return {
43
+ module: GtAuthModule_1,
44
+ global: true,
45
+ providers: [
46
+ {
47
+ provide: constants_1.GT_AUTH_CONFIG,
48
+ useFactory: () => (0, config_1.getConfig)(),
49
+ },
50
+ internal_only_guard_1.InternalOnlyGuard,
51
+ permissions_guard_1.GtPermissionsGuard,
52
+ auth_guard_1.GtAuthGuard,
53
+ org_context_interceptor_1.OrgContextInterceptor,
54
+ auth_exception_filter_1.AuthExceptionFilter,
55
+ ],
56
+ exports: [
57
+ constants_1.GT_AUTH_CONFIG,
58
+ internal_only_guard_1.InternalOnlyGuard,
59
+ permissions_guard_1.GtPermissionsGuard,
60
+ auth_guard_1.GtAuthGuard,
61
+ org_context_interceptor_1.OrgContextInterceptor,
62
+ auth_exception_filter_1.AuthExceptionFilter,
63
+ ],
64
+ };
65
+ }
66
+ static forRootAsync(options) {
67
+ return {
68
+ module: GtAuthModule_1,
69
+ global: true,
70
+ imports: options.imports || [],
71
+ providers: [
72
+ {
73
+ provide: constants_1.GT_AUTH_CONFIG,
74
+ useFactory: async (...args) => {
75
+ const config = await options.useFactory(...args);
76
+ (0, config_1.initAuth)(config);
77
+ return (0, config_1.getConfig)();
78
+ },
79
+ inject: options.inject || [],
80
+ },
81
+ internal_only_guard_1.InternalOnlyGuard,
82
+ permissions_guard_1.GtPermissionsGuard,
83
+ auth_guard_1.GtAuthGuard,
84
+ org_context_interceptor_1.OrgContextInterceptor,
85
+ auth_exception_filter_1.AuthExceptionFilter,
86
+ ],
87
+ exports: [
88
+ constants_1.GT_AUTH_CONFIG,
89
+ internal_only_guard_1.InternalOnlyGuard,
90
+ permissions_guard_1.GtPermissionsGuard,
91
+ auth_guard_1.GtAuthGuard,
92
+ org_context_interceptor_1.OrgContextInterceptor,
93
+ auth_exception_filter_1.AuthExceptionFilter,
94
+ ],
95
+ };
96
+ }
97
+ };
98
+ exports.GtAuthModule = GtAuthModule;
99
+ exports.GtAuthModule = GtAuthModule = GtAuthModule_1 = __decorate([
100
+ (0, common_1.Module)({})
101
+ ], GtAuthModule);
102
+ //# sourceMappingURL=gt-auth.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gt-auth.module.js","sourceRoot":"","sources":["../../src/nestjs/gt-auth.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAuF;AAEvF,sCAAgD;AAChD,2CAA6C;AAC7C,wFAAqF;AACrF,sEAAiE;AACjE,kEAAgE;AAChE,oDAAkD;AAClD,oFAA+E;AAC/E,2EAAsE;AAUtE;;;;;;;;;;;;;;;GAeG;AAEI,IAAM,YAAY,oBAAlB,MAAM,YAAY;IACvB,SAAS,CAAC,QAA4B;QACpC,QAAQ,CAAC,KAAK,CAAC,uDAA0B,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAC5D,CAAC;IAED,MAAM,CAAC,OAAO,CAAC,UAA+B,EAAE;QAC9C,IAAA,iBAAQ,EAAC,OAAO,CAAC,CAAC;QAElB,OAAO;YACL,MAAM,EAAE,cAAY;YACpB,MAAM,EAAE,IAAI;YACZ,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,0BAAc;oBACvB,UAAU,EAAE,GAAG,EAAE,CAAC,IAAA,kBAAS,GAAE;iBAC9B;gBACD,uCAAiB;gBACjB,sCAAkB;gBAClB,wBAAW;gBACX,+CAAqB;gBACrB,2CAAmB;aACpB;YACD,OAAO,EAAE;gBACP,0BAAc;gBACd,uCAAiB;gBACjB,sCAAkB;gBAClB,wBAAW;gBACX,+CAAqB;gBACrB,2CAAmB;aACpB;SACF,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,YAAY,CAAC,OAAiC;QACnD,OAAO;YACL,MAAM,EAAE,cAAY;YACpB,MAAM,EAAE,IAAI;YACZ,OAAO,EAAE,OAAO,CAAC,OAAO,IAAI,EAAE;YAC9B,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,0BAAc;oBACvB,UAAU,EAAE,KAAK,EAAE,GAAG,IAAW,EAAE,EAAE;wBACnC,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC,CAAC;wBACjD,IAAA,iBAAQ,EAAC,MAAM,CAAC,CAAC;wBACjB,OAAO,IAAA,kBAAS,GAAE,CAAC;oBACrB,CAAC;oBACD,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,EAAE;iBAC7B;gBACD,uCAAiB;gBACjB,sCAAkB;gBAClB,wBAAW;gBACX,+CAAqB;gBACrB,2CAAmB;aACpB;YACD,OAAO,EAAE;gBACP,0BAAc;gBACd,uCAAiB;gBACjB,sCAAkB;gBAClB,wBAAW;gBACX,+CAAqB;gBACrB,2CAAmB;aACpB;SACF,CAAC;IACJ,CAAC;CACF,CAAA;AAhEY,oCAAY;uBAAZ,YAAY;IADxB,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,YAAY,CAgExB"}
@@ -0,0 +1,13 @@
1
+ import { CanActivate, ExecutionContext } from '@nestjs/common';
2
+ import { Reflector } from '@nestjs/core';
3
+ /**
4
+ * Optional NestJS guard that runs the core extractUser() logic.
5
+ * Use this when you need guard-based auth extraction instead of middleware.
6
+ * Skips @Public() routes.
7
+ */
8
+ export declare class GtAuthGuard implements CanActivate {
9
+ private readonly reflector;
10
+ constructor(reflector: Reflector);
11
+ canActivate(context: ExecutionContext): boolean;
12
+ }
13
+ //# sourceMappingURL=auth.guard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.guard.d.ts","sourceRoot":"","sources":["../../../src/nestjs/guards/auth.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAqC,MAAM,gBAAgB,CAAC;AAClG,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAIzC;;;;GAIG;AACH,qBACa,WAAY,YAAW,WAAW;IACjC,OAAO,CAAC,QAAQ,CAAC,SAAS;gBAAT,SAAS,EAAE,SAAS;IAEjD,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO;CAwBhD"}
@@ -0,0 +1,53 @@
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;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.GtAuthGuard = void 0;
13
+ const common_1 = require("@nestjs/common");
14
+ const core_1 = require("@nestjs/core");
15
+ const extract_user_1 = require("../../extract-user");
16
+ const constants_1 = require("../constants");
17
+ /**
18
+ * Optional NestJS guard that runs the core extractUser() logic.
19
+ * Use this when you need guard-based auth extraction instead of middleware.
20
+ * Skips @Public() routes.
21
+ */
22
+ let GtAuthGuard = class GtAuthGuard {
23
+ constructor(reflector) {
24
+ this.reflector = reflector;
25
+ }
26
+ canActivate(context) {
27
+ const isPublic = this.reflector.getAllAndOverride(constants_1.IS_PUBLIC_KEY, [
28
+ context.getHandler(),
29
+ context.getClass(),
30
+ ]);
31
+ if (isPublic) {
32
+ return true;
33
+ }
34
+ const request = context.switchToHttp().getRequest();
35
+ // If user was already set by middleware, skip
36
+ if (request.user) {
37
+ return true;
38
+ }
39
+ try {
40
+ request.user = (0, extract_user_1.extractUser)(request);
41
+ return true;
42
+ }
43
+ catch {
44
+ throw new common_1.UnauthorizedException('Authentication required');
45
+ }
46
+ }
47
+ };
48
+ exports.GtAuthGuard = GtAuthGuard;
49
+ exports.GtAuthGuard = GtAuthGuard = __decorate([
50
+ (0, common_1.Injectable)(),
51
+ __metadata("design:paramtypes", [core_1.Reflector])
52
+ ], GtAuthGuard);
53
+ //# sourceMappingURL=auth.guard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.guard.js","sourceRoot":"","sources":["../../../src/nestjs/guards/auth.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAAkG;AAClG,uCAAyC;AACzC,qDAAiD;AACjD,4CAA6C;AAE7C;;;;GAIG;AAEI,IAAM,WAAW,GAAjB,MAAM,WAAW;IACtB,YAA6B,SAAoB;QAApB,cAAS,GAAT,SAAS,CAAW;IAAG,CAAC;IAErD,WAAW,CAAC,OAAyB;QACnC,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAU,yBAAa,EAAE;YACxE,OAAO,CAAC,UAAU,EAAE;YACpB,OAAO,CAAC,QAAQ,EAAE;SACnB,CAAC,CAAC;QAEH,IAAI,QAAQ,EAAE,CAAC;YACb,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;QAEpD,8CAA8C;QAC9C,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;YACjB,OAAO,IAAI,CAAC;QACd,CAAC;QAED,IAAI,CAAC;YACH,OAAO,CAAC,IAAI,GAAG,IAAA,0BAAW,EAAC,OAAO,CAAC,CAAC;YACpC,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,IAAI,8BAAqB,CAAC,yBAAyB,CAAC,CAAC;QAC7D,CAAC;IACH,CAAC;CACF,CAAA;AA3BY,kCAAW;sBAAX,WAAW;IADvB,IAAA,mBAAU,GAAE;qCAE6B,gBAAS;GADtC,WAAW,CA2BvB"}
@@ -0,0 +1,15 @@
1
+ import { CanActivate, ExecutionContext } from '@nestjs/common';
2
+ import { Reflector } from '@nestjs/core';
3
+ import { ResolvedAuthConfig } from '../../types';
4
+ /**
5
+ * Guard that ensures requests come through the trusted gateway.
6
+ * Validates X-Gateway-Token against the configured internalGatewayToken.
7
+ * Skips validation for @Public() routes.
8
+ */
9
+ export declare class InternalOnlyGuard implements CanActivate {
10
+ private readonly config;
11
+ private readonly reflector;
12
+ constructor(config: ResolvedAuthConfig, reflector: Reflector);
13
+ canActivate(context: ExecutionContext): boolean;
14
+ }
15
+ //# sourceMappingURL=internal-only.guard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal-only.guard.d.ts","sourceRoot":"","sources":["../../../src/nestjs/guards/internal-only.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAA0C,MAAM,gBAAgB,CAAC;AACvG,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD;;;;GAIG;AACH,qBACa,iBAAkB,YAAW,WAAW;IAEzB,OAAO,CAAC,QAAQ,CAAC,MAAM;IAC/C,OAAO,CAAC,QAAQ,CAAC,SAAS;gBADe,MAAM,EAAE,kBAAkB,EAClD,SAAS,EAAE,SAAS;IAGvC,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO;CAmBhD"}
@@ -0,0 +1,51 @@
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;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.InternalOnlyGuard = void 0;
16
+ const common_1 = require("@nestjs/common");
17
+ const core_1 = require("@nestjs/core");
18
+ const constants_1 = require("../constants");
19
+ /**
20
+ * Guard that ensures requests come through the trusted gateway.
21
+ * Validates X-Gateway-Token against the configured internalGatewayToken.
22
+ * Skips validation for @Public() routes.
23
+ */
24
+ let InternalOnlyGuard = class InternalOnlyGuard {
25
+ constructor(config, reflector) {
26
+ this.config = config;
27
+ this.reflector = reflector;
28
+ }
29
+ canActivate(context) {
30
+ const isPublic = this.reflector.getAllAndOverride(constants_1.IS_PUBLIC_KEY, [
31
+ context.getHandler(),
32
+ context.getClass(),
33
+ ]);
34
+ if (isPublic) {
35
+ return true;
36
+ }
37
+ const request = context.switchToHttp().getRequest();
38
+ const gatewayToken = request.headers[this.config.trustedHeaderNames.gatewayToken];
39
+ if (!this.config.internalGatewayToken || gatewayToken !== this.config.internalGatewayToken) {
40
+ throw new common_1.ForbiddenException('Direct access not allowed');
41
+ }
42
+ return true;
43
+ }
44
+ };
45
+ exports.InternalOnlyGuard = InternalOnlyGuard;
46
+ exports.InternalOnlyGuard = InternalOnlyGuard = __decorate([
47
+ (0, common_1.Injectable)(),
48
+ __param(0, (0, common_1.Inject)(constants_1.GT_AUTH_CONFIG)),
49
+ __metadata("design:paramtypes", [Object, core_1.Reflector])
50
+ ], InternalOnlyGuard);
51
+ //# sourceMappingURL=internal-only.guard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal-only.guard.js","sourceRoot":"","sources":["../../../src/nestjs/guards/internal-only.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAuG;AACvG,uCAAyC;AACzC,4CAA6D;AAG7D;;;;GAIG;AAEI,IAAM,iBAAiB,GAAvB,MAAM,iBAAiB;IAC5B,YAC2C,MAA0B,EAClD,SAAoB;QADI,WAAM,GAAN,MAAM,CAAoB;QAClD,cAAS,GAAT,SAAS,CAAW;IACpC,CAAC;IAEJ,WAAW,CAAC,OAAyB;QACnC,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAU,yBAAa,EAAE;YACxE,OAAO,CAAC,UAAU,EAAE;YACpB,OAAO,CAAC,QAAQ,EAAE;SACnB,CAAC,CAAC;QAEH,IAAI,QAAQ,EAAE,CAAC;YACb,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;QACpD,MAAM,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,kBAAkB,CAAC,YAAY,CAAC,CAAC;QAElF,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,oBAAoB,IAAI,YAAY,KAAK,IAAI,CAAC,MAAM,CAAC,oBAAoB,EAAE,CAAC;YAC3F,MAAM,IAAI,2BAAkB,CAAC,2BAA2B,CAAC,CAAC;QAC5D,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAA;AAzBY,8CAAiB;4BAAjB,iBAAiB;IAD7B,IAAA,mBAAU,GAAE;IAGR,WAAA,IAAA,eAAM,EAAC,0BAAc,CAAC,CAAA;6CACK,gBAAS;GAH5B,iBAAiB,CAyB7B"}
@@ -0,0 +1,23 @@
1
+ import { CanActivate, ExecutionContext } from '@nestjs/common';
2
+ import { Reflector } from '@nestjs/core';
3
+ import { ResolvedAuthConfig } from '../../types';
4
+ /**
5
+ * Hybrid permission guard with 3-tier resolution:
6
+ *
7
+ * 1. If user.permissions is non-empty → in-memory check (fast, from JWT)
8
+ * 2. If permissionResolver is configured → call it (for RBAC service's own ResolveService)
9
+ * 3. If rbacServiceUrl is configured → HTTP call to RBAC service
10
+ * 4. Else → deny (fail-closed)
11
+ *
12
+ * Admin roles (config.adminRoles) always bypass.
13
+ */
14
+ export declare class GtPermissionsGuard implements CanActivate {
15
+ private readonly config;
16
+ private readonly reflector;
17
+ private readonly logger;
18
+ constructor(config: ResolvedAuthConfig, reflector: Reflector);
19
+ canActivate(context: ExecutionContext): Promise<boolean>;
20
+ private resolveViaFunction;
21
+ private resolveViaHttp;
22
+ }
23
+ //# sourceMappingURL=permissions.guard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"permissions.guard.d.ts","sourceRoot":"","sources":["../../../src/nestjs/guards/permissions.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAkD,MAAM,gBAAgB,CAAC;AAC/G,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAY,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAE3D;;;;;;;;;GASG;AACH,qBACa,kBAAmB,YAAW,WAAW;IAI1B,OAAO,CAAC,QAAQ,CAAC,MAAM;IAC/C,OAAO,CAAC,QAAQ,CAAC,SAAS;IAJ5B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuC;gBAGnB,MAAM,EAAE,kBAAkB,EAClD,SAAS,EAAE,SAAS;IAGjC,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;YA8DhD,kBAAkB;YAwBlB,cAAc;CA8C7B"}
@@ -0,0 +1,134 @@
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;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ var GtPermissionsGuard_1;
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.GtPermissionsGuard = void 0;
17
+ const common_1 = require("@nestjs/common");
18
+ const core_1 = require("@nestjs/core");
19
+ const constants_1 = require("../constants");
20
+ /**
21
+ * Hybrid permission guard with 3-tier resolution:
22
+ *
23
+ * 1. If user.permissions is non-empty → in-memory check (fast, from JWT)
24
+ * 2. If permissionResolver is configured → call it (for RBAC service's own ResolveService)
25
+ * 3. If rbacServiceUrl is configured → HTTP call to RBAC service
26
+ * 4. Else → deny (fail-closed)
27
+ *
28
+ * Admin roles (config.adminRoles) always bypass.
29
+ */
30
+ let GtPermissionsGuard = GtPermissionsGuard_1 = class GtPermissionsGuard {
31
+ constructor(config, reflector) {
32
+ this.config = config;
33
+ this.reflector = reflector;
34
+ this.logger = new common_1.Logger(GtPermissionsGuard_1.name);
35
+ }
36
+ async canActivate(context) {
37
+ const isPublic = this.reflector.getAllAndOverride(constants_1.IS_PUBLIC_KEY, [
38
+ context.getHandler(),
39
+ context.getClass(),
40
+ ]);
41
+ if (isPublic) {
42
+ return true;
43
+ }
44
+ const requiredPermissions = this.reflector.getAllAndOverride(constants_1.PERMISSIONS_KEY, [context.getHandler(), context.getClass()]);
45
+ if (!requiredPermissions || requiredPermissions.length === 0) {
46
+ return true;
47
+ }
48
+ const request = context.switchToHttp().getRequest();
49
+ const user = request.user;
50
+ if (!user) {
51
+ throw new common_1.ForbiddenException('No user context found');
52
+ }
53
+ // Admin roles bypass all permission checks
54
+ if (this.config.adminRoles.includes(user.role)) {
55
+ return true;
56
+ }
57
+ // Tier 1: In-memory check from JWT claims
58
+ if (user.permissions && user.permissions.length > 0) {
59
+ const userPerms = new Set(user.permissions);
60
+ const hasAll = requiredPermissions.every((p) => userPerms.has(p));
61
+ if (!hasAll) {
62
+ throw new common_1.ForbiddenException('You do not have the required permissions to access this resource');
63
+ }
64
+ return true;
65
+ }
66
+ // Tier 2: Custom permission resolver (e.g., RBAC service's own ResolveService)
67
+ if (this.config.permissionResolver) {
68
+ return this.resolveViaFunction(user, requiredPermissions);
69
+ }
70
+ // Tier 3: HTTP call to RBAC service
71
+ if (this.config.rbacServiceUrl) {
72
+ return this.resolveViaHttp(user, requiredPermissions);
73
+ }
74
+ // Fail-closed: no resolution mechanism available
75
+ this.logger.warn(`No permission resolution mechanism configured. Denying access for user ${user.userId}.`);
76
+ throw new common_1.ForbiddenException('You do not have the required permissions to access this resource');
77
+ }
78
+ async resolveViaFunction(user, requiredPermissions) {
79
+ for (const perm of requiredPermissions) {
80
+ const [resource, action] = perm.split(':');
81
+ if (!resource || !action)
82
+ continue;
83
+ const hasPermission = await this.config.permissionResolver(user.orgId, user.userId, resource, action);
84
+ if (!hasPermission) {
85
+ throw new common_1.ForbiddenException('You do not have the required permissions to access this resource');
86
+ }
87
+ }
88
+ return true;
89
+ }
90
+ async resolveViaHttp(user, requiredPermissions) {
91
+ for (const perm of requiredPermissions) {
92
+ const [resource, action] = perm.split(':');
93
+ if (!resource || !action)
94
+ continue;
95
+ const url = `${this.config.rbacServiceUrl}/v1/resolve/check`;
96
+ try {
97
+ const response = await fetch(url, {
98
+ method: 'POST',
99
+ headers: {
100
+ 'Content-Type': 'application/json',
101
+ 'x-user-id': user.userId,
102
+ 'x-org-id': user.orgId,
103
+ 'x-user-role': user.role,
104
+ ...(this.config.internalGatewayToken
105
+ ? { 'x-gateway-token': this.config.internalGatewayToken }
106
+ : {}),
107
+ },
108
+ body: JSON.stringify({ resource, action }),
109
+ });
110
+ if (!response.ok) {
111
+ throw new common_1.ForbiddenException('You do not have the required permissions to access this resource');
112
+ }
113
+ const body = await response.json();
114
+ if (!body.allowed) {
115
+ throw new common_1.ForbiddenException('You do not have the required permissions to access this resource');
116
+ }
117
+ }
118
+ catch (err) {
119
+ if (err instanceof common_1.ForbiddenException)
120
+ throw err;
121
+ this.logger.error(`RBAC service call failed: ${err}`, err.stack);
122
+ throw new common_1.ForbiddenException('Permission check failed — RBAC service unavailable');
123
+ }
124
+ }
125
+ return true;
126
+ }
127
+ };
128
+ exports.GtPermissionsGuard = GtPermissionsGuard;
129
+ exports.GtPermissionsGuard = GtPermissionsGuard = GtPermissionsGuard_1 = __decorate([
130
+ (0, common_1.Injectable)(),
131
+ __param(0, (0, common_1.Inject)(constants_1.GT_AUTH_CONFIG)),
132
+ __metadata("design:paramtypes", [Object, core_1.Reflector])
133
+ ], GtPermissionsGuard);
134
+ //# sourceMappingURL=permissions.guard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"permissions.guard.js","sourceRoot":"","sources":["../../../src/nestjs/guards/permissions.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAA+G;AAC/G,uCAAyC;AACzC,4CAA8E;AAG9E;;;;;;;;;GASG;AAEI,IAAM,kBAAkB,0BAAxB,MAAM,kBAAkB;IAG7B,YAC0B,MAA2C,EAClD,SAAoB;QADI,WAAM,GAAN,MAAM,CAAoB;QAClD,cAAS,GAAT,SAAS,CAAW;QAJtB,WAAM,GAAG,IAAI,eAAM,CAAC,oBAAkB,CAAC,IAAI,CAAC,CAAC;IAK3D,CAAC;IAEJ,KAAK,CAAC,WAAW,CAAC,OAAyB;QACzC,MAAM,QAAQ,GAAG,IAA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YACJ,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAA;AA5IY,gDAAkB;6BAAlB,kBAAkB;IAD9B,IAAA,mBAAU,GAAE;IAKR,WAAA,IAAA,eAAM,EAAC,0BAAc,CAAC,CAAA;6CACK,gBAAS;GAL5B,kBAAkB,CA4I9B"}
@@ -0,0 +1,12 @@
1
+ export { GtAuthModule, GtAuthModuleOptions, GtAuthModuleAsyncOptions } from './gt-auth.module';
2
+ export { GT_AUTH_CONFIG, IS_PUBLIC_KEY, PERMISSIONS_KEY, ROLES_KEY } from './constants';
3
+ export { InternalOnlyGuard } from './guards/internal-only.guard';
4
+ export { GtPermissionsGuard } from './guards/permissions.guard';
5
+ export { GtAuthGuard } from './guards/auth.guard';
6
+ export { CurrentUser, CurrentOrg, Public, RequirePermissions, RequireRoles } from './decorators/index';
7
+ export { GtTrustedHeadersMiddleware } from './middleware/trusted-headers.middleware';
8
+ export { OrgContextInterceptor } from './interceptors/org-context.interceptor';
9
+ export { AuthExceptionFilter } from './filters/auth-exception.filter';
10
+ export { AuthUser, RequestUser, AuthConfig, AuthStrategy, UserRole } from '../types';
11
+ export { AuthError, UnauthorizedError, ForbiddenError } from '../errors';
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/nestjs/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAC;AAG/F,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAGxF,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAGlD,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAGvG,OAAO,EAAE,0BAA0B,EAAE,MAAM,yCAAyC,CAAC;AAGrF,OAAO,EAAE,qBAAqB,EAAE,MAAM,wCAAwC,CAAC;AAG/E,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAGtE,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACrF,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC"}
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ForbiddenError = exports.UnauthorizedError = exports.AuthError = exports.AuthExceptionFilter = exports.OrgContextInterceptor = exports.GtTrustedHeadersMiddleware = exports.RequireRoles = exports.RequirePermissions = exports.Public = exports.CurrentOrg = exports.CurrentUser = exports.GtAuthGuard = exports.GtPermissionsGuard = exports.InternalOnlyGuard = exports.ROLES_KEY = exports.PERMISSIONS_KEY = exports.IS_PUBLIC_KEY = exports.GT_AUTH_CONFIG = exports.GtAuthModule = void 0;
4
+ // Module
5
+ var gt_auth_module_1 = require("./gt-auth.module");
6
+ Object.defineProperty(exports, "GtAuthModule", { enumerable: true, get: function () { return gt_auth_module_1.GtAuthModule; } });
7
+ // Constants
8
+ var constants_1 = require("./constants");
9
+ Object.defineProperty(exports, "GT_AUTH_CONFIG", { enumerable: true, get: function () { return constants_1.GT_AUTH_CONFIG; } });
10
+ Object.defineProperty(exports, "IS_PUBLIC_KEY", { enumerable: true, get: function () { return constants_1.IS_PUBLIC_KEY; } });
11
+ Object.defineProperty(exports, "PERMISSIONS_KEY", { enumerable: true, get: function () { return constants_1.PERMISSIONS_KEY; } });
12
+ Object.defineProperty(exports, "ROLES_KEY", { enumerable: true, get: function () { return constants_1.ROLES_KEY; } });
13
+ // Guards
14
+ var internal_only_guard_1 = require("./guards/internal-only.guard");
15
+ Object.defineProperty(exports, "InternalOnlyGuard", { enumerable: true, get: function () { return internal_only_guard_1.InternalOnlyGuard; } });
16
+ var permissions_guard_1 = require("./guards/permissions.guard");
17
+ Object.defineProperty(exports, "GtPermissionsGuard", { enumerable: true, get: function () { return permissions_guard_1.GtPermissionsGuard; } });
18
+ var auth_guard_1 = require("./guards/auth.guard");
19
+ Object.defineProperty(exports, "GtAuthGuard", { enumerable: true, get: function () { return auth_guard_1.GtAuthGuard; } });
20
+ // Decorators
21
+ var index_1 = require("./decorators/index");
22
+ Object.defineProperty(exports, "CurrentUser", { enumerable: true, get: function () { return index_1.CurrentUser; } });
23
+ Object.defineProperty(exports, "CurrentOrg", { enumerable: true, get: function () { return index_1.CurrentOrg; } });
24
+ Object.defineProperty(exports, "Public", { enumerable: true, get: function () { return index_1.Public; } });
25
+ Object.defineProperty(exports, "RequirePermissions", { enumerable: true, get: function () { return index_1.RequirePermissions; } });
26
+ Object.defineProperty(exports, "RequireRoles", { enumerable: true, get: function () { return index_1.RequireRoles; } });
27
+ // Middleware
28
+ var trusted_headers_middleware_1 = require("./middleware/trusted-headers.middleware");
29
+ Object.defineProperty(exports, "GtTrustedHeadersMiddleware", { enumerable: true, get: function () { return trusted_headers_middleware_1.GtTrustedHeadersMiddleware; } });
30
+ // Interceptors
31
+ var org_context_interceptor_1 = require("./interceptors/org-context.interceptor");
32
+ Object.defineProperty(exports, "OrgContextInterceptor", { enumerable: true, get: function () { return org_context_interceptor_1.OrgContextInterceptor; } });
33
+ // Filters
34
+ var auth_exception_filter_1 = require("./filters/auth-exception.filter");
35
+ Object.defineProperty(exports, "AuthExceptionFilter", { enumerable: true, get: function () { return auth_exception_filter_1.AuthExceptionFilter; } });
36
+ var errors_1 = require("../errors");
37
+ Object.defineProperty(exports, "AuthError", { enumerable: true, get: function () { return errors_1.AuthError; } });
38
+ Object.defineProperty(exports, "UnauthorizedError", { enumerable: true, get: function () { return errors_1.UnauthorizedError; } });
39
+ Object.defineProperty(exports, "ForbiddenError", { enumerable: true, get: function () { return errors_1.ForbiddenError; } });
40
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/nestjs/index.ts"],"names":[],"mappings":";;;AAAA,SAAS;AACT,mDAA+F;AAAtF,8GAAA,YAAY,OAAA;AAErB,YAAY;AACZ,yCAAwF;AAA/E,2GAAA,cAAc,OAAA;AAAE,0GAAA,aAAa,OAAA;AAAE,4GAAA,eAAe,OAAA;AAAE,sGAAA,SAAS,OAAA;AAElE,SAAS;AACT,oEAAiE;AAAxD,wHAAA,iBAAiB,OAAA;AAC1B,gEAAgE;AAAvD,uHAAA,kBAAkB,OAAA;AAC3B,kDAAkD;AAAzC,yGAAA,WAAW,OAAA;AAEpB,aAAa;AACb,4CAAuG;AAA9F,oGAAA,WAAW,OAAA;AAAE,mGAAA,UAAU,OAAA;AAAE,+FAAA,MAAM,OAAA;AAAE,2GAAA,kBAAkB,OAAA;AAAE,qGAAA,YAAY,OAAA;AAE1E,aAAa;AACb,sFAAqF;AAA5E,wIAAA,0BAA0B,OAAA;AAEnC,eAAe;AACf,kFAA+E;AAAtE,gIAAA,qBAAqB,OAAA;AAE9B,UAAU;AACV,yEAAsE;AAA7D,4HAAA,mBAAmB,OAAA;AAI5B,oCAAyE;AAAhE,mGAAA,SAAS,OAAA;AAAE,2GAAA,iBAAiB,OAAA;AAAE,wGAAA,cAAc,OAAA"}
@@ -0,0 +1,21 @@
1
+ import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
2
+ import { Reflector } from '@nestjs/core';
3
+ import { Observable } from 'rxjs';
4
+ import { DataSource } from 'typeorm';
5
+ /**
6
+ * Interceptor that sets PostgreSQL session variable for Row-Level Security.
7
+ * Runs `SET LOCAL app.current_org_id = $1` inside a transaction so RLS policies
8
+ * can filter rows by the current organization.
9
+ *
10
+ * Attaches the transaction's EntityManager to `request.entityManager` so
11
+ * services can use it for all queries within the request scope.
12
+ *
13
+ * Skips @Public() routes and requests without a valid orgId.
14
+ */
15
+ export declare class OrgContextInterceptor implements NestInterceptor {
16
+ private readonly dataSource;
17
+ private readonly reflector;
18
+ constructor(dataSource: DataSource, reflector: Reflector);
19
+ intercept(context: ExecutionContext, next: CallHandler): Observable<any>;
20
+ }
21
+ //# sourceMappingURL=org-context.interceptor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"org-context.interceptor.d.ts","sourceRoot":"","sources":["../../../src/nestjs/interceptors/org-context.interceptor.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,gBAAgB,EAEhB,eAAe,EAChB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,UAAU,EAAmB,MAAM,MAAM,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAMrC;;;;;;;;;GASG;AACH,qBACa,qBAAsB,YAAW,eAAe;IAEzD,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,QAAQ,CAAC,SAAS;gBADT,UAAU,EAAE,UAAU,EACtB,SAAS,EAAE,SAAS;IAGvC,SAAS,CAAC,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,CAAC,GAAG,CAAC;CA6BzE"}
@@ -0,0 +1,63 @@
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;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.OrgContextInterceptor = void 0;
13
+ const common_1 = require("@nestjs/common");
14
+ const core_1 = require("@nestjs/core");
15
+ const rxjs_1 = require("rxjs");
16
+ const typeorm_1 = require("typeorm");
17
+ const constants_1 = require("../constants");
18
+ const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
19
+ /**
20
+ * Interceptor that sets PostgreSQL session variable for Row-Level Security.
21
+ * Runs `SET LOCAL app.current_org_id = $1` inside a transaction so RLS policies
22
+ * can filter rows by the current organization.
23
+ *
24
+ * Attaches the transaction's EntityManager to `request.entityManager` so
25
+ * services can use it for all queries within the request scope.
26
+ *
27
+ * Skips @Public() routes and requests without a valid orgId.
28
+ */
29
+ let OrgContextInterceptor = class OrgContextInterceptor {
30
+ constructor(dataSource, reflector) {
31
+ this.dataSource = dataSource;
32
+ this.reflector = reflector;
33
+ }
34
+ intercept(context, next) {
35
+ const isPublic = this.reflector.getAllAndOverride(constants_1.IS_PUBLIC_KEY, [
36
+ context.getHandler(),
37
+ context.getClass(),
38
+ ]);
39
+ if (isPublic) {
40
+ return next.handle();
41
+ }
42
+ const request = context.switchToHttp().getRequest();
43
+ const user = request.user;
44
+ if (!user?.orgId) {
45
+ return next.handle();
46
+ }
47
+ // Validate orgId is a valid UUID to prevent SQL injection
48
+ if (!UUID_REGEX.test(user.orgId)) {
49
+ return next.handle();
50
+ }
51
+ return (0, rxjs_1.from)(this.dataSource.transaction(async (manager) => {
52
+ await manager.query(`SET LOCAL app.current_org_id = $1`, [user.orgId]);
53
+ request.entityManager = manager;
54
+ })).pipe((0, rxjs_1.switchMap)(() => next.handle()));
55
+ }
56
+ };
57
+ exports.OrgContextInterceptor = OrgContextInterceptor;
58
+ exports.OrgContextInterceptor = OrgContextInterceptor = __decorate([
59
+ (0, common_1.Injectable)(),
60
+ __metadata("design:paramtypes", [typeorm_1.DataSource,
61
+ core_1.Reflector])
62
+ ], OrgContextInterceptor);
63
+ //# sourceMappingURL=org-context.interceptor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"org-context.interceptor.js","sourceRoot":"","sources":["../../../src/nestjs/interceptors/org-context.interceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAKwB;AACxB,uCAAyC;AACzC,+BAAmD;AACnD,qCAAqC;AAErC,4CAA6C;AAE7C,MAAM,UAAU,GAAG,iEAAiE,CAAC;AAErF;;;;;;;;;GASG;AAEI,IAAM,qBAAqB,GAA3B,MAAM,qBAAqB;IAChC,YACmB,UAAsB,EACtB,SAAoB;QADpB,eAAU,GAAV,UAAU,CAAY;QACtB,cAAS,GAAT,SAAS,CAAW;IACpC,CAAC;IAEJ,SAAS,CAAC,OAAyB,EAAE,IAAiB;QACpD,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAU,yBAAa,EAAE;YACxE,OAAO,CAAC,UAAU,EAAE;YACpB,OAAO,CAAC,QAAQ,EAAE;SACnB,CAAC,CAAC;QAEH,IAAI,QAAQ,EAAE,CAAC;YACb,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;QACvB,CAAC;QAED,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;QACpD,MAAM,IAAI,GAAyB,OAAO,CAAC,IAAI,CAAC;QAEhD,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC;YACjB,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;QACvB,CAAC;QAED,0DAA0D;QAC1D,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACjC,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;QACvB,CAAC;QAED,OAAO,IAAA,WAAI,EACT,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;YAC5C,MAAM,OAAO,CAAC,KAAK,CAAC,mCAAmC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;YACvE,OAAO,CAAC,aAAa,GAAG,OAAO,CAAC;QAClC,CAAC,CAAC,CACH,CAAC,IAAI,CAAC,IAAA,gBAAS,EAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IACzC,CAAC;CACF,CAAA;AAnCY,sDAAqB;gCAArB,qBAAqB;IADjC,IAAA,mBAAU,GAAE;qCAGoB,oBAAU;QACX,gBAAS;GAH5B,qBAAqB,CAmCjC"}
@@ -0,0 +1,15 @@
1
+ import { NestMiddleware } from '@nestjs/common';
2
+ import { Request, Response, NextFunction } from 'express';
3
+ /**
4
+ * NestJS middleware that wraps the core extractUser() function.
5
+ * Sets req.user and req.tenantId from the configured strategy chain.
6
+ *
7
+ * Register in your module's configure():
8
+ * ```
9
+ * consumer.apply(GtTrustedHeadersMiddleware).forRoutes('*');
10
+ * ```
11
+ */
12
+ export declare class GtTrustedHeadersMiddleware implements NestMiddleware {
13
+ use(req: Request, _res: Response, next: NextFunction): void;
14
+ }
15
+ //# sourceMappingURL=trusted-headers.middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trusted-headers.middleware.d.ts","sourceRoot":"","sources":["../../../src/nestjs/middleware/trusted-headers.middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAI1D;;;;;;;;GAQG;AACH,qBACa,0BAA2B,YAAW,cAAc;IAC/D,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,YAAY,GAAG,IAAI;CAa5D"}