@trailmix-cms/cms 0.4.4 → 0.7.2

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 (267) hide show
  1. package/dist/auth.guard.d.ts +5 -13
  2. package/dist/auth.guard.d.ts.map +1 -1
  3. package/dist/auth.guard.js +24 -95
  4. package/dist/auth.guard.js.map +1 -1
  5. package/dist/collections/account.collection.d.ts +5 -3
  6. package/dist/collections/account.collection.d.ts.map +1 -1
  7. package/dist/collections/account.collection.js +15 -8
  8. package/dist/collections/account.collection.js.map +1 -1
  9. package/dist/collections/api-key.collection.d.ts +54 -0
  10. package/dist/collections/api-key.collection.d.ts.map +1 -0
  11. package/dist/collections/api-key.collection.js +142 -0
  12. package/dist/collections/api-key.collection.js.map +1 -0
  13. package/dist/collections/index.d.ts +4 -2
  14. package/dist/collections/index.d.ts.map +1 -1
  15. package/dist/collections/index.js +9 -5
  16. package/dist/collections/index.js.map +1 -1
  17. package/dist/collections/organization.collection.d.ts +20 -0
  18. package/dist/collections/organization.collection.d.ts.map +1 -0
  19. package/dist/collections/{file.collection.js → organization.collection.js} +17 -17
  20. package/dist/collections/organization.collection.js.map +1 -0
  21. package/dist/collections/role.collection.d.ts +32 -0
  22. package/dist/collections/role.collection.d.ts.map +1 -0
  23. package/dist/collections/role.collection.js +90 -0
  24. package/dist/collections/role.collection.js.map +1 -0
  25. package/dist/collections/security-audit.collection.d.ts +30 -0
  26. package/dist/collections/security-audit.collection.d.ts.map +1 -0
  27. package/dist/collections/security-audit.collection.js +79 -0
  28. package/dist/collections/security-audit.collection.js.map +1 -0
  29. package/dist/constants/cms-collection-names.d.ts +4 -2
  30. package/dist/constants/cms-collection-names.d.ts.map +1 -1
  31. package/dist/constants/cms-collection-names.js +4 -2
  32. package/dist/constants/cms-collection-names.js.map +1 -1
  33. package/dist/constants/provider-symbols.d.ts +10 -12
  34. package/dist/constants/provider-symbols.d.ts.map +1 -1
  35. package/dist/constants/provider-symbols.js +10 -12
  36. package/dist/constants/provider-symbols.js.map +1 -1
  37. package/dist/controllers/account.controller.d.ts +11 -15
  38. package/dist/controllers/account.controller.d.ts.map +1 -1
  39. package/dist/controllers/account.controller.js +69 -13
  40. package/dist/controllers/account.controller.js.map +1 -1
  41. package/dist/controllers/api-keys.controller.d.ts +13 -0
  42. package/dist/controllers/api-keys.controller.d.ts.map +1 -0
  43. package/dist/controllers/api-keys.controller.js +125 -0
  44. package/dist/controllers/api-keys.controller.js.map +1 -0
  45. package/dist/controllers/audit.controller.d.ts.map +1 -1
  46. package/dist/controllers/audit.controller.js +3 -3
  47. package/dist/controllers/audit.controller.js.map +1 -1
  48. package/dist/controllers/audits.controller.d.ts +10 -0
  49. package/dist/controllers/audits.controller.d.ts.map +1 -0
  50. package/dist/controllers/audits.controller.js +107 -0
  51. package/dist/controllers/audits.controller.js.map +1 -0
  52. package/dist/controllers/global-roles.controller.d.ts +16 -0
  53. package/dist/controllers/global-roles.controller.d.ts.map +1 -0
  54. package/dist/controllers/global-roles.controller.js +137 -0
  55. package/dist/controllers/global-roles.controller.js.map +1 -0
  56. package/dist/controllers/index.d.ts +6 -1
  57. package/dist/controllers/index.d.ts.map +1 -1
  58. package/dist/controllers/index.js +6 -1
  59. package/dist/controllers/index.js.map +1 -1
  60. package/dist/controllers/organization-roles.controller.d.ts +16 -0
  61. package/dist/controllers/organization-roles.controller.d.ts.map +1 -0
  62. package/dist/controllers/organization-roles.controller.js +145 -0
  63. package/dist/controllers/organization-roles.controller.js.map +1 -0
  64. package/dist/controllers/organizations.controller.d.ts +65 -0
  65. package/dist/controllers/organizations.controller.d.ts.map +1 -0
  66. package/dist/controllers/organizations.controller.js +140 -0
  67. package/dist/controllers/organizations.controller.js.map +1 -0
  68. package/dist/controllers/security-audits.controller.d.ts +11 -0
  69. package/dist/controllers/security-audits.controller.d.ts.map +1 -0
  70. package/dist/controllers/security-audits.controller.js +130 -0
  71. package/dist/controllers/security-audits.controller.js.map +1 -0
  72. package/dist/decorators/account.decorator.d.ts +1 -3
  73. package/dist/decorators/account.decorator.d.ts.map +1 -1
  74. package/dist/decorators/account.decorator.js +3 -10
  75. package/dist/decorators/account.decorator.js.map +1 -1
  76. package/dist/decorators/audit-context.decorator.d.ts +6 -0
  77. package/dist/decorators/audit-context.decorator.d.ts.map +1 -1
  78. package/dist/decorators/audit-context.decorator.js +12 -3
  79. package/dist/decorators/audit-context.decorator.js.map +1 -1
  80. package/dist/decorators/auth.decorator.d.ts +7 -6
  81. package/dist/decorators/auth.decorator.d.ts.map +1 -1
  82. package/dist/decorators/auth.decorator.js +38 -5
  83. package/dist/decorators/auth.decorator.js.map +1 -1
  84. package/dist/decorators/index.d.ts +4 -0
  85. package/dist/decorators/index.d.ts.map +1 -0
  86. package/dist/decorators/index.js +20 -0
  87. package/dist/decorators/index.js.map +1 -0
  88. package/dist/dto/account.dto.d.ts +33 -0
  89. package/dist/dto/account.dto.d.ts.map +1 -0
  90. package/dist/dto/account.dto.js +14 -0
  91. package/dist/dto/account.dto.js.map +1 -0
  92. package/dist/dto/api-key.dto.d.ts +89 -0
  93. package/dist/dto/api-key.dto.d.ts.map +1 -0
  94. package/dist/dto/api-key.dto.js +27 -0
  95. package/dist/dto/api-key.dto.js.map +1 -0
  96. package/dist/dto/audit.dto.d.ts +11 -5
  97. package/dist/dto/audit.dto.d.ts.map +1 -1
  98. package/dist/dto/audit.dto.js +1 -1
  99. package/dist/dto/audit.dto.js.map +1 -1
  100. package/dist/dto/global-role.dto.d.ts +99 -0
  101. package/dist/dto/global-role.dto.d.ts.map +1 -0
  102. package/dist/dto/global-role.dto.js +26 -0
  103. package/dist/dto/global-role.dto.js.map +1 -0
  104. package/dist/dto/organization-role.dto.d.ts +107 -0
  105. package/dist/dto/organization-role.dto.d.ts.map +1 -0
  106. package/dist/dto/organization-role.dto.js +26 -0
  107. package/dist/dto/organization-role.dto.js.map +1 -0
  108. package/dist/dto/organization.dto.d.ts +57 -0
  109. package/dist/dto/organization.dto.d.ts.map +1 -0
  110. package/dist/dto/organization.dto.js +32 -0
  111. package/dist/dto/organization.dto.js.map +1 -0
  112. package/dist/dto/security-audit.dto.d.ts +95 -0
  113. package/dist/dto/security-audit.dto.d.ts.map +1 -0
  114. package/dist/dto/security-audit.dto.js +26 -0
  115. package/dist/dto/security-audit.dto.js.map +1 -0
  116. package/dist/index.d.ts +7 -2
  117. package/dist/index.d.ts.map +1 -1
  118. package/dist/index.js +8 -3
  119. package/dist/index.js.map +1 -1
  120. package/dist/managers/global-role.manager.d.ts +42 -0
  121. package/dist/managers/global-role.manager.d.ts.map +1 -0
  122. package/dist/managers/global-role.manager.js +117 -0
  123. package/dist/managers/global-role.manager.js.map +1 -0
  124. package/dist/managers/index.d.ts +4 -0
  125. package/dist/managers/index.d.ts.map +1 -0
  126. package/dist/managers/index.js +20 -0
  127. package/dist/managers/index.js.map +1 -0
  128. package/dist/managers/organization-role.manager.d.ts +47 -0
  129. package/dist/managers/organization-role.manager.d.ts.map +1 -0
  130. package/dist/managers/organization-role.manager.js +218 -0
  131. package/dist/managers/organization-role.manager.js.map +1 -0
  132. package/dist/managers/organization.manager.d.ts +39 -0
  133. package/dist/managers/organization.manager.d.ts.map +1 -0
  134. package/dist/managers/organization.manager.js +196 -0
  135. package/dist/managers/organization.manager.js.map +1 -0
  136. package/dist/module.d.ts +92 -0
  137. package/dist/module.d.ts.map +1 -0
  138. package/dist/module.js +137 -0
  139. package/dist/module.js.map +1 -0
  140. package/dist/pipes/api-key.pipe.d.ts +8 -0
  141. package/dist/pipes/api-key.pipe.d.ts.map +1 -0
  142. package/dist/pipes/api-key.pipe.js +28 -0
  143. package/dist/pipes/api-key.pipe.js.map +1 -0
  144. package/dist/pipes/organization.pipe.d.ts +8 -0
  145. package/dist/pipes/organization.pipe.d.ts.map +1 -0
  146. package/dist/pipes/organization.pipe.js +28 -0
  147. package/dist/pipes/organization.pipe.js.map +1 -0
  148. package/dist/pipes/role.pipe.d.ts +8 -0
  149. package/dist/pipes/{file.pipe.d.ts.map → role.pipe.d.ts.map} +1 -1
  150. package/dist/pipes/{file.pipe.js → role.pipe.js} +8 -8
  151. package/dist/pipes/{file.pipe.js.map → role.pipe.js.map} +1 -1
  152. package/dist/services/account.service.d.ts +0 -2
  153. package/dist/services/account.service.d.ts.map +1 -1
  154. package/dist/services/account.service.js +1 -37
  155. package/dist/services/account.service.js.map +1 -1
  156. package/dist/services/api-key.service.d.ts +42 -0
  157. package/dist/services/api-key.service.d.ts.map +1 -0
  158. package/dist/services/api-key.service.js +306 -0
  159. package/dist/services/api-key.service.js.map +1 -0
  160. package/dist/services/auth.service.d.ts +50 -0
  161. package/dist/services/auth.service.d.ts.map +1 -0
  162. package/dist/services/auth.service.js +259 -0
  163. package/dist/services/auth.service.js.map +1 -0
  164. package/dist/services/authorization.service.d.ts +44 -9
  165. package/dist/services/authorization.service.d.ts.map +1 -1
  166. package/dist/services/authorization.service.js +107 -41
  167. package/dist/services/authorization.service.js.map +1 -1
  168. package/dist/services/feature.service.d.ts +23 -0
  169. package/dist/services/feature.service.d.ts.map +1 -0
  170. package/dist/services/feature.service.js +49 -0
  171. package/dist/services/feature.service.js.map +1 -0
  172. package/dist/services/global-role.service.d.ts +17 -0
  173. package/dist/services/global-role.service.d.ts.map +1 -0
  174. package/dist/services/global-role.service.js +99 -0
  175. package/dist/services/global-role.service.js.map +1 -0
  176. package/dist/services/index.d.ts +9 -0
  177. package/dist/services/index.d.ts.map +1 -0
  178. package/dist/services/index.js +25 -0
  179. package/dist/services/index.js.map +1 -0
  180. package/dist/services/organization-role.service.d.ts +33 -0
  181. package/dist/services/organization-role.service.d.ts.map +1 -0
  182. package/dist/services/organization-role.service.js +102 -0
  183. package/dist/services/organization-role.service.js.map +1 -0
  184. package/dist/services/organization.service.d.ts +29 -0
  185. package/dist/services/organization.service.d.ts.map +1 -0
  186. package/dist/services/organization.service.js +95 -0
  187. package/dist/services/organization.service.js.map +1 -0
  188. package/dist/types/feature-config.d.ts +9 -0
  189. package/dist/types/feature-config.d.ts.map +1 -0
  190. package/dist/types/feature-config.js +3 -0
  191. package/dist/types/feature-config.js.map +1 -0
  192. package/dist/types/hooks/auth-guard-hook.d.ts.map +1 -0
  193. package/dist/types/hooks/auth-guard-hook.js.map +1 -0
  194. package/dist/types/hooks/index.d.ts +3 -0
  195. package/dist/types/hooks/index.d.ts.map +1 -0
  196. package/dist/types/hooks/index.js +19 -0
  197. package/dist/types/hooks/index.js.map +1 -0
  198. package/dist/types/hooks/organization-delete-hook.d.ts +20 -0
  199. package/dist/types/hooks/organization-delete-hook.d.ts.map +1 -0
  200. package/dist/types/hooks/organization-delete-hook.js +3 -0
  201. package/dist/types/hooks/organization-delete-hook.js.map +1 -0
  202. package/dist/types/index.d.ts +5 -0
  203. package/dist/types/index.d.ts.map +1 -0
  204. package/dist/types/index.js +21 -0
  205. package/dist/types/index.js.map +1 -0
  206. package/dist/types/request-principal.d.ts +9 -0
  207. package/dist/types/request-principal.d.ts.map +1 -0
  208. package/dist/types/request-principal.js +3 -0
  209. package/dist/types/request-principal.js.map +1 -0
  210. package/dist/utils/provider-helpers.d.ts +6 -1
  211. package/dist/utils/provider-helpers.d.ts.map +1 -1
  212. package/dist/utils/provider-helpers.js +11 -1
  213. package/dist/utils/provider-helpers.js.map +1 -1
  214. package/package.json +52 -17
  215. package/test/unit/auth.guard.spec.ts +355 -0
  216. package/test/unit/collections/api-key.collection.spec.ts +416 -0
  217. package/test/unit/managers/global-role.manager.spec.ts +269 -0
  218. package/test/unit/managers/organization-role.manager.spec.ts +632 -0
  219. package/test/unit/managers/organization.manager.spec.ts +395 -0
  220. package/test/unit/module.spec.ts +596 -0
  221. package/test/unit/services/account.service.spec.ts +90 -0
  222. package/test/unit/services/api-key.service.spec.ts +1244 -0
  223. package/test/unit/services/auth.service.spec.ts +1036 -0
  224. package/test/unit/services/authorization.service.spec.ts +636 -0
  225. package/test/unit/services/feature.service.spec.ts +56 -0
  226. package/test/unit/services/global-role.service.spec.ts +289 -0
  227. package/test/unit/services/organization-role.service.spec.ts +300 -0
  228. package/test/unit/services/organization.service.spec.ts +385 -0
  229. package/test/utils/auth-guard.ts +114 -0
  230. package/test/utils/base.ts +16 -0
  231. package/test/utils/entities/account.ts +13 -0
  232. package/test/utils/entities/api-key.ts +15 -0
  233. package/test/utils/entities/audit.ts +18 -0
  234. package/test/utils/entities/index.ts +6 -0
  235. package/test/utils/entities/mapping.ts +20 -0
  236. package/test/utils/entities/organization.ts +13 -0
  237. package/test/utils/entities/role.ts +21 -0
  238. package/test/utils/entities/security-audit.ts +16 -0
  239. package/test/utils/index.ts +4 -0
  240. package/test/utils/models/audit-context.ts +10 -0
  241. package/test/utils/models/authorization.ts +7 -0
  242. package/test/utils/models/global-role.ts +22 -0
  243. package/test/utils/models/index.ts +5 -0
  244. package/test/utils/models/organization-role.ts +23 -0
  245. package/test/utils/models/publishable.ts +7 -0
  246. package/tsconfig.build.json +36 -0
  247. package/tsconfig.build.tsbuildinfo +1 -0
  248. package/dist/auth-guard-hook.d.ts.map +0 -1
  249. package/dist/auth-guard-hook.js.map +0 -1
  250. package/dist/cms.module.d.ts +0 -8
  251. package/dist/cms.module.d.ts.map +0 -1
  252. package/dist/cms.module.js +0 -44
  253. package/dist/cms.module.js.map +0 -1
  254. package/dist/cms.providers.d.ts +0 -120
  255. package/dist/cms.providers.d.ts.map +0 -1
  256. package/dist/cms.providers.js +0 -126
  257. package/dist/cms.providers.js.map +0 -1
  258. package/dist/collections/file.collection.d.ts +0 -21
  259. package/dist/collections/file.collection.d.ts.map +0 -1
  260. package/dist/collections/file.collection.js.map +0 -1
  261. package/dist/collections/text.collection.d.ts +0 -20
  262. package/dist/collections/text.collection.d.ts.map +0 -1
  263. package/dist/collections/text.collection.js +0 -56
  264. package/dist/collections/text.collection.js.map +0 -1
  265. package/dist/pipes/file.pipe.d.ts +0 -8
  266. /package/dist/{auth-guard-hook.d.ts → types/hooks/auth-guard-hook.d.ts} +0 -0
  267. /package/dist/{auth-guard-hook.js → types/hooks/auth-guard-hook.js} +0 -0
@@ -0,0 +1,306 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
19
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
20
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
21
+ 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;
22
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
23
+ };
24
+ var __importStar = (this && this.__importStar) || (function () {
25
+ var ownKeys = function(o) {
26
+ ownKeys = Object.getOwnPropertyNames || function (o) {
27
+ var ar = [];
28
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
29
+ return ar;
30
+ };
31
+ return ownKeys(o);
32
+ };
33
+ return function (mod) {
34
+ if (mod && mod.__esModule) return mod;
35
+ var result = {};
36
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
37
+ __setModuleDefault(result, mod);
38
+ return result;
39
+ };
40
+ })();
41
+ var __metadata = (this && this.__metadata) || function (k, v) {
42
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
43
+ };
44
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
45
+ return function (target, key) { decorator(target, key, paramIndex); }
46
+ };
47
+ var ApiKeyService_1;
48
+ Object.defineProperty(exports, "__esModule", { value: true });
49
+ exports.ApiKeyService = void 0;
50
+ const common_1 = require("@nestjs/common");
51
+ const trailmixModels = __importStar(require("@trailmix-cms/models"));
52
+ const collections_1 = require("../collections");
53
+ const authorization_service_1 = require("./authorization.service");
54
+ const feature_service_1 = require("./feature.service");
55
+ let ApiKeyService = ApiKeyService_1 = class ApiKeyService {
56
+ apiKeyCollection;
57
+ securityAuditCollection;
58
+ featureService;
59
+ authorizationService;
60
+ organizationCollection;
61
+ logger = new common_1.Logger(ApiKeyService_1.name);
62
+ constructor(apiKeyCollection, securityAuditCollection, featureService, authorizationService, organizationCollection) {
63
+ this.apiKeyCollection = apiKeyCollection;
64
+ this.securityAuditCollection = securityAuditCollection;
65
+ this.featureService = featureService;
66
+ this.authorizationService = authorizationService;
67
+ this.organizationCollection = organizationCollection;
68
+ }
69
+ /**
70
+ * Create a new API key with scope validation and authorization checks
71
+ */
72
+ async createApiKey(params, principal, auditContext) {
73
+ this.logger.log(`Creating API key with scope ${params.scope_type} for principal ${principal.entity._id}`);
74
+ // Check if scopes are configured and validate the requested scope
75
+ const allowedScopes = this.featureService.getApiKeyScopes();
76
+ if (!allowedScopes.includes(params.scope_type)) {
77
+ await this.securityAuditCollection.insertOne({
78
+ event_type: trailmixModels.SecurityAuditEventType.UnauthorizedAccess,
79
+ principal_id: principal.entity._id,
80
+ principal_type: principal.principal_type,
81
+ message: `Unauthorized attempt to create API key with scope ${params.scope_type} which is not in the allowed scopes`,
82
+ source: ApiKeyService_1.name,
83
+ });
84
+ throw new common_1.BadRequestException(`Scope type ${params.scope_type} is not allowed. Allowed scopes: ${allowedScopes.join(', ')}`);
85
+ }
86
+ const isGlobalAdmin = await this.authorizationService.isGlobalAdmin(principal.entity._id, principal.principal_type);
87
+ switch (params.scope_type) {
88
+ case trailmixModels.ApiKeyScope.Global: {
89
+ // Global-scoped: principal must be a global admin
90
+ if (!isGlobalAdmin) {
91
+ await this.securityAuditCollection.insertOne({
92
+ event_type: trailmixModels.SecurityAuditEventType.UnauthorizedAccess,
93
+ principal_id: principal.entity._id,
94
+ principal_type: principal.principal_type,
95
+ message: 'Unauthorized attempt to create global-scoped API key',
96
+ source: ApiKeyService_1.name,
97
+ });
98
+ throw new common_1.BadRequestException('Invalid scope type');
99
+ }
100
+ if (params.scope_id) {
101
+ throw new common_1.BadRequestException('Scope ID is not allowed for global-scoped API keys');
102
+ }
103
+ const apiKey = await this.apiKeyCollection.create(params, auditContext);
104
+ this.logger.log(`Successfully created API key ${apiKey._id} with scope ${params.scope_type}`);
105
+ return apiKey;
106
+ }
107
+ case trailmixModels.ApiKeyScope.Account: {
108
+ // Account-scoped: principal must be the account owner
109
+ if (!params.scope_id) {
110
+ throw new common_1.BadRequestException('Scope ID is required for account-scoped API keys');
111
+ }
112
+ if (isGlobalAdmin) {
113
+ const apiKey = await this.apiKeyCollection.create(params, auditContext);
114
+ this.logger.log(`Successfully created API key ${apiKey._id} with scope ${params.scope_type}`);
115
+ return apiKey;
116
+ }
117
+ if (principal.principal_type !== trailmixModels.Principal.Account) {
118
+ throw new common_1.BadRequestException('Only accounts can create account-scoped API keys');
119
+ }
120
+ if (!principal.entity._id.equals(params.scope_id)) {
121
+ await this.securityAuditCollection.insertOne({
122
+ event_type: trailmixModels.SecurityAuditEventType.UnauthorizedAccess,
123
+ principal_id: principal.entity._id,
124
+ principal_type: principal.principal_type,
125
+ message: 'Unauthorized attempt to create account-scoped API key for another principal',
126
+ source: ApiKeyService_1.name,
127
+ });
128
+ throw new common_1.BadRequestException('Scope ID must match principal ID for account-scoped API keys');
129
+ }
130
+ const apiKey = await this.apiKeyCollection.create(params, auditContext);
131
+ this.logger.log(`Successfully created API key ${apiKey._id} with scope ${params.scope_type}`);
132
+ return apiKey;
133
+ }
134
+ case trailmixModels.ApiKeyScope.Organization: {
135
+ // Organization-scoped: principal must have admin/owner role on the organization
136
+ if (!this.featureService.isOrganizationsEnabled()) {
137
+ this.logger.warn('Organizations feature must be enabled to create organization-scoped API keys');
138
+ throw new common_1.BadRequestException();
139
+ }
140
+ if (!this.organizationCollection) {
141
+ this.logger.error('Organization collections and services are not available despite organizations feature being enabled');
142
+ throw new common_1.InternalServerErrorException();
143
+ }
144
+ if (!params.scope_id) {
145
+ throw new common_1.BadRequestException('Scope ID is required for organization-scoped API keys');
146
+ }
147
+ if (isGlobalAdmin) {
148
+ const apiKey = await this.apiKeyCollection.create(params, auditContext);
149
+ this.logger.log(`Successfully created API key ${apiKey._id} with scope ${params.scope_type}`);
150
+ return apiKey;
151
+ }
152
+ const requiredRoles = [trailmixModels.RoleValue.Admin, trailmixModels.RoleValue.Owner];
153
+ // Check if user has admin or owner role on the organization
154
+ const accessResult = await this.authorizationService.resolveOrganizationAuthorization({
155
+ principal,
156
+ rolesAllowList: requiredRoles,
157
+ principalTypeAllowList: [trailmixModels.Principal.Account],
158
+ organizationId: params.scope_id,
159
+ });
160
+ if (!accessResult.hasAccess) {
161
+ await this.securityAuditCollection.insertOne({
162
+ event_type: trailmixModels.SecurityAuditEventType.UnauthorizedAccess,
163
+ principal_id: principal.entity._id,
164
+ principal_type: principal.principal_type,
165
+ message: `Unauthorized attempt to create organization-scoped API key for organization ${params.scope_id}`,
166
+ source: ApiKeyService_1.name,
167
+ });
168
+ // If the principal has at least reader organization role, throw a forbidden exception since they have access to the organization
169
+ if (accessResult.organizationRoles.some(role => [
170
+ trailmixModels.RoleValue.Owner,
171
+ trailmixModels.RoleValue.Admin,
172
+ trailmixModels.RoleValue.User,
173
+ trailmixModels.RoleValue.Reader,
174
+ ].includes(role.role))) {
175
+ throw new common_1.ForbiddenException(`Insufficient permissions to create API keys for organization ${params.scope_id}. You must have at lesat one of ${requiredRoles} role on the organization.`);
176
+ }
177
+ throw new common_1.BadRequestException(`Organization ${params.scope_id} does not exist `);
178
+ }
179
+ const apiKey = await this.apiKeyCollection.create(params, auditContext);
180
+ this.logger.log(`Successfully created API key ${apiKey._id} with scope ${params.scope_type}`);
181
+ return apiKey;
182
+ }
183
+ default: {
184
+ throw new common_1.InternalServerErrorException('Invalid scope type');
185
+ }
186
+ }
187
+ }
188
+ /**
189
+ * Get API keys with filtering based on principal permissions
190
+ */
191
+ async getApiKeys(principal, queryParams) {
192
+ this.logger.log(`Getting API keys for principal ${principal.entity._id}`);
193
+ const isGlobalAdmin = await this.authorizationService.isGlobalAdmin(principal.entity._id, principal.principal_type);
194
+ if (isGlobalAdmin) {
195
+ const filter = {
196
+ ...(queryParams.name ? { name: queryParams.name } : {}),
197
+ ...(queryParams.disabled ? { disabled: queryParams.disabled } : {}),
198
+ ...(queryParams.scope_type ? { "scope.type": queryParams.scope_type } : {}),
199
+ ...(queryParams.scope_id ? { "scope.id": queryParams.scope_id } : {}),
200
+ };
201
+ const apiKeys = await this.apiKeyCollection.find(filter);
202
+ return {
203
+ items: apiKeys,
204
+ count: apiKeys.length,
205
+ };
206
+ }
207
+ // Non global admins
208
+ if (!queryParams.scope_type) {
209
+ throw new common_1.BadRequestException('Scope type is required');
210
+ }
211
+ // Global-scoped API keys are not supported for non-global admins
212
+ if (queryParams.scope_type?.includes(trailmixModels.ApiKeyScope.Global)) {
213
+ await this.securityAuditCollection.insertOne({
214
+ event_type: trailmixModels.SecurityAuditEventType.UnauthorizedAccess,
215
+ principal_id: principal.entity._id,
216
+ principal_type: principal.principal_type,
217
+ message: 'Unauthorized attempt to get global-scoped API keys for non-global admins',
218
+ source: ApiKeyService_1.name,
219
+ });
220
+ throw new common_1.BadRequestException('Global-scoped API keys are not supported for non-global admins');
221
+ }
222
+ switch (queryParams.scope_type) {
223
+ case trailmixModels.ApiKeyScope.Account: {
224
+ // Account-scoped API keys
225
+ const filter = {
226
+ ...(queryParams.name ? { name: queryParams.name } : {}),
227
+ ...(queryParams.disabled ? { disabled: queryParams.disabled } : {}),
228
+ "scope.type": trailmixModels.ApiKeyScope.Account,
229
+ "scope.id": principal.entity._id,
230
+ };
231
+ const apiKeys = await this.apiKeyCollection.find(filter);
232
+ return {
233
+ items: apiKeys,
234
+ count: apiKeys.length,
235
+ };
236
+ }
237
+ case trailmixModels.ApiKeyScope.Organization: {
238
+ // Organization-scoped API keys
239
+ if (!this.featureService.isOrganizationsEnabled()) {
240
+ throw new common_1.BadRequestException('Organizations feature must be enabled to query organization-scoped API keys');
241
+ }
242
+ if (!queryParams.scope_id) {
243
+ throw new common_1.BadRequestException('Scope ID is required for organization-scoped API keys');
244
+ }
245
+ const organization_id = queryParams.scope_id;
246
+ const requiredRoles = [trailmixModels.RoleValue.Admin, trailmixModels.RoleValue.Owner];
247
+ // Check if user has admin or owner role on the organization
248
+ const accessResult = await this.authorizationService.resolveOrganizationAuthorization({
249
+ principal,
250
+ rolesAllowList: requiredRoles,
251
+ principalTypeAllowList: [trailmixModels.Principal.Account],
252
+ organizationId: organization_id,
253
+ });
254
+ if (!accessResult.hasAccess) {
255
+ throw new common_1.ForbiddenException(`Insufficient permissions to create API keys for organization ${organization_id}. You must have at lesat one of ${requiredRoles} role on the organization.`);
256
+ }
257
+ const filter = {
258
+ ...(queryParams.name ? { name: queryParams.name } : {}),
259
+ ...(queryParams.disabled ? { disabled: queryParams.disabled } : {}),
260
+ "scope.type": trailmixModels.ApiKeyScope.Organization,
261
+ "scope.id": organization_id,
262
+ };
263
+ const apiKeys = await this.apiKeyCollection.find(filter);
264
+ return {
265
+ items: apiKeys,
266
+ count: apiKeys.length,
267
+ };
268
+ }
269
+ default: {
270
+ throw new common_1.InternalServerErrorException('Invalid scope type');
271
+ }
272
+ }
273
+ }
274
+ /**
275
+ * Get a single API key by entity, checking authorization
276
+ */
277
+ async getApiKey(apiKey, principal) {
278
+ const hasAccess = await this.authorizationService.authorizeApiKeyAccessForPrincipal(principal, apiKey.scope_type, apiKey.scope_id);
279
+ if (!hasAccess) {
280
+ throw new common_1.NotFoundException('API key not found');
281
+ }
282
+ return apiKey;
283
+ }
284
+ /**
285
+ * Delete an API key, checking authorization first
286
+ */
287
+ async deleteApiKey(apiKey, principal, auditContext) {
288
+ const hasAccess = await this.authorizationService.authorizeApiKeyAccessForPrincipal(principal, apiKey.scope_type, apiKey.scope_id);
289
+ if (!hasAccess) {
290
+ throw new common_1.NotFoundException('API key not found');
291
+ }
292
+ await this.apiKeyCollection.deleteOne(apiKey._id, auditContext);
293
+ this.logger.log(`Successfully deleted API key ${apiKey._id}`);
294
+ }
295
+ };
296
+ exports.ApiKeyService = ApiKeyService;
297
+ exports.ApiKeyService = ApiKeyService = ApiKeyService_1 = __decorate([
298
+ (0, common_1.Injectable)(),
299
+ __param(4, (0, common_1.Optional)()),
300
+ __param(4, (0, common_1.Inject)(collections_1.OrganizationCollection)),
301
+ __metadata("design:paramtypes", [collections_1.ApiKeyCollection,
302
+ collections_1.SecurityAuditCollection,
303
+ feature_service_1.FeatureService,
304
+ authorization_service_1.AuthorizationService, Object])
305
+ ], ApiKeyService);
306
+ //# sourceMappingURL=api-key.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api-key.service.js","sourceRoot":"","sources":["../../src/services/api-key.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAgK;AAEhK,qEAAuD;AAGvD,gDAAmG;AACnG,mEAA+D;AAC/D,uDAAmD;AAW5C,IAAM,aAAa,qBAAnB,MAAM,aAAa;IAID;IACA;IACA;IACA;IAC4C;IAPhD,MAAM,GAAG,IAAI,eAAM,CAAC,eAAa,CAAC,IAAI,CAAC,CAAC;IAEzD,YACqB,gBAAkC,EAClC,uBAAgD,EAChD,cAA8B,EAC9B,oBAA0C,EACE,sBAA0D;QAJtG,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,4BAAuB,GAAvB,uBAAuB,CAAyB;QAChD,mB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AAC;IAClB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,YAAY,CACd,MAAoC,EACpC,SAA2B,EAC3B,YAA+C;QAE/C,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,oBAAoB,CAAC,iCAAiC,CAAC,SAAS,EAAE,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC;QACnI,IAAI,CAAC,SAAS,EAAE,CAAC;YACb,MAAM,IAAI,0BAAiB,CAAC,mBAAmB,CAAC,CAAC;QACrD,CAAC;QAED,MAAM,IAAI,CAAC,gBAAgB,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,EAAE,YAAY,CAAC,CAAC;QAChE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,gCAAgC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC;IAClE,CAAC;CACJ,CAAA;AArRY,sCAAa;wBAAb,aAAa;IADzB,IAAA,mBAAU,GAAE;IASJ,WAAA,IAAA,iBAAQ,GAAE,CAAA;IAAE,WAAA,IAAA,eAAM,EAAC,oCAAsB,CAAC,CAAA;qCAJR,8BAAgB;QACT,qCAAuB;QAChC,gCAAc;QACR,4CAAoB;GAPtD,aAAa,CAqRzB"}
@@ -0,0 +1,50 @@
1
+ import { ExecutionContext } from '@nestjs/common';
2
+ import * as trailmixModels from '@trailmix-cms/models';
3
+ import { AccountCollection, ApiKeyCollection, SecurityAuditCollection } from '../collections';
4
+ import { AccountService } from './account.service';
5
+ import { GlobalRoleService } from './global-role.service';
6
+ import { type AuthGuardHook, type RequestPrincipal } from '../types';
7
+ export declare const AuthResult: {
8
+ readonly IsValid: "isValid";
9
+ readonly Unauthorized: "unauthorized";
10
+ readonly Forbidden: "forbidden";
11
+ };
12
+ export type AuthResult = typeof AuthResult[keyof typeof AuthResult];
13
+ export declare class AuthService {
14
+ private accountService;
15
+ private accountCollection;
16
+ private globalRoleService;
17
+ private securityAuditCollection;
18
+ private authGuardHook?;
19
+ private apiKeyCollection?;
20
+ private readonly logger;
21
+ constructor(accountService: AccountService, accountCollection: AccountCollection, globalRoleService: GlobalRoleService, securityAuditCollection: SecurityAuditCollection, authGuardHook?: AuthGuardHook | undefined, apiKeyCollection?: ApiKeyCollection | undefined);
22
+ /**
23
+ * Validate authentication and authorization for a request
24
+ * @param principal The principal from the request (null if not authenticated)
25
+ * @param config The authentication configuration
26
+ * @param requestUrl The request URL for audit logging
27
+ * @returns AuthResult
28
+ */
29
+ validateAuth(principal: RequestPrincipal | null, config: {
30
+ allowAnonymous: boolean;
31
+ requiredPrincipalTypes: trailmixModels.Principal[];
32
+ requiredGlobalRoles: (trailmixModels.RoleValue | string)[];
33
+ requiredApiKeyScopes: trailmixModels.ApiKeyScope[];
34
+ }, requestUrl: string): Promise<AuthResult>;
35
+ /**
36
+ * Get the account from the request principal, if the principal is an account API key, it will return the account associated with the API key
37
+ * @param principal The principal
38
+ * @returns The account
39
+ */
40
+ getAccountFromPrincipal(principal: RequestPrincipal): Promise<trailmixModels.Account.Entity | null>;
41
+ /**
42
+ * Get the principal from the request context
43
+ * @param context The execution context
44
+ * @returns The principal or null if not found
45
+ */
46
+ getPrincipal(context: ExecutionContext): Promise<RequestPrincipal | null>;
47
+ private getAccount;
48
+ private getApiKey;
49
+ }
50
+ //# sourceMappingURL=auth.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.service.d.ts","sourceRoot":"","sources":["../../src/services/auth.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAGtH,OAAO,KAAK,cAAc,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAC9F,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAE1D,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,g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this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
19
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
20
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
21
+ 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;
22
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
23
+ };
24
+ var __importStar = (this && this.__importStar) || (function () {
25
+ var ownKeys = function(o) {
26
+ ownKeys = Object.getOwnPropertyNames || function (o) {
27
+ var ar = [];
28
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
29
+ return ar;
30
+ };
31
+ return ownKeys(o);
32
+ };
33
+ return function (mod) {
34
+ if (mod && mod.__esModule) return mod;
35
+ var result = {};
36
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
37
+ __setModuleDefault(result, mod);
38
+ return result;
39
+ };
40
+ })();
41
+ var __metadata = (this && this.__metadata) || function (k, v) {
42
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
43
+ };
44
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
45
+ return function (target, key) { decorator(target, key, paramIndex); }
46
+ };
47
+ var AuthService_1;
48
+ Object.defineProperty(exports, "__esModule", { value: true });
49
+ exports.AuthService = exports.AuthResult = void 0;
50
+ const common_1 = require("@nestjs/common");
51
+ const fastify_1 = require("@clerk/fastify");
52
+ const trailmixModels = __importStar(require("@trailmix-cms/models"));
53
+ const collections_1 = require("../collections");
54
+ const account_service_1 = require("./account.service");
55
+ const global_role_service_1 = require("./global-role.service");
56
+ const constants_1 = require("../constants");
57
+ exports.AuthResult = {
58
+ IsValid: 'isValid',
59
+ Unauthorized: 'unauthorized',
60
+ Forbidden: 'forbidden',
61
+ };
62
+ let AuthService = AuthService_1 = class AuthService {
63
+ accountService;
64
+ accountCollection;
65
+ globalRoleService;
66
+ securityAuditCollection;
67
+ authGuardHook;
68
+ apiKeyCollection;
69
+ logger = new common_1.Logger(AuthService_1.name);
70
+ constructor(accountService, accountCollection, globalRoleService, securityAuditCollection, authGuardHook, apiKeyCollection) {
71
+ this.accountService = accountService;
72
+ this.accountCollection = accountCollection;
73
+ this.globalRoleService = globalRoleService;
74
+ this.securityAuditCollection = securityAuditCollection;
75
+ this.authGuardHook = authGuardHook;
76
+ this.apiKeyCollection = apiKeyCollection;
77
+ }
78
+ /**
79
+ * Validate authentication and authorization for a request
80
+ * @param principal The principal from the request (null if not authenticated)
81
+ * @param config The authentication configuration
82
+ * @param requestUrl The request URL for audit logging
83
+ * @returns AuthResult
84
+ */
85
+ async validateAuth(principal, config, requestUrl) {
86
+ const { allowAnonymous, requiredPrincipalTypes, requiredGlobalRoles, requiredApiKeyScopes } = config;
87
+ if (!principal) {
88
+ if (allowAnonymous) {
89
+ return exports.AuthResult.IsValid;
90
+ }
91
+ return exports.AuthResult.Unauthorized;
92
+ }
93
+ if (allowAnonymous) {
94
+ return exports.AuthResult.IsValid;
95
+ }
96
+ // Check if principal type is required, if no required principal types, allow any principal type
97
+ if (requiredPrincipalTypes.length > 0 && !requiredPrincipalTypes.includes(principal.principal_type)) {
98
+ await this.securityAuditCollection.insertOne({
99
+ event_type: trailmixModels.SecurityAuditEventType.UnauthorizedAccess,
100
+ principal_id: principal.entity._id,
101
+ principal_type: principal.principal_type,
102
+ message: `Unauthorized access to ${requestUrl}, required principal type not found: ${requiredPrincipalTypes.join(', ')}`,
103
+ source: AuthService_1.name,
104
+ });
105
+ return exports.AuthResult.Forbidden;
106
+ }
107
+ // Check if API key scope is required, if no required API key scopes, allow any API key scope
108
+ if (principal.principal_type === trailmixModels.Principal.ApiKey && requiredApiKeyScopes.length > 0 && !requiredApiKeyScopes.includes(principal.entity.scope_type)) {
109
+ await this.securityAuditCollection.insertOne({
110
+ event_type: trailmixModels.SecurityAuditEventType.UnauthorizedAccess,
111
+ principal_id: principal.entity._id,
112
+ principal_type: principal.principal_type,
113
+ message: `Unauthorized access to ${requestUrl}, required API key scope is not allowed:${requiredApiKeyScopes.join(', ')}`,
114
+ source: AuthService_1.name,
115
+ });
116
+ return exports.AuthResult.Forbidden;
117
+ }
118
+ // If no roles are required, allow any authenticated principal
119
+ if (requiredGlobalRoles.length == 0) {
120
+ return exports.AuthResult.IsValid;
121
+ }
122
+ const globalRoleAssignments = await this.globalRoleService.find({
123
+ principal_id: principal.entity._id,
124
+ principal_type: principal.principal_type,
125
+ });
126
+ if (!globalRoleAssignments) {
127
+ await this.securityAuditCollection.insertOne({
128
+ event_type: trailmixModels.SecurityAuditEventType.UnauthorizedAccess,
129
+ principal_id: principal.entity._id,
130
+ principal_type: principal.principal_type,
131
+ message: `Unauthorized access to ${requestUrl}, no global role assignments found`,
132
+ source: AuthService_1.name,
133
+ });
134
+ return exports.AuthResult.Forbidden;
135
+ }
136
+ // Check if user has required role or is global admin
137
+ const hasRole = requiredGlobalRoles.some((role) => globalRoleAssignments.some((assignment) => assignment.role === role)) ||
138
+ globalRoleAssignments.some((assignment) => assignment.role === trailmixModels.RoleValue.Admin);
139
+ if (!hasRole) {
140
+ await this.securityAuditCollection.insertOne({
141
+ event_type: trailmixModels.SecurityAuditEventType.UnauthorizedAccess,
142
+ principal_id: principal.entity._id,
143
+ principal_type: principal.principal_type,
144
+ message: `Unauthorized access to ${requestUrl}, required role not found: ${requiredGlobalRoles.join(', ')}`,
145
+ source: AuthService_1.name,
146
+ });
147
+ return exports.AuthResult.Forbidden;
148
+ }
149
+ return exports.AuthResult.IsValid;
150
+ }
151
+ /**
152
+ * Get the account from the request principal, if the principal is an account API key, it will return the account associated with the API key
153
+ * @param principal The principal
154
+ * @returns The account
155
+ */
156
+ async getAccountFromPrincipal(principal) {
157
+ if (principal.principal_type === trailmixModels.Principal.Account) {
158
+ return principal.entity;
159
+ }
160
+ const apiKey = principal.entity;
161
+ if (apiKey.scope_type !== trailmixModels.ApiKeyScope.Account) {
162
+ throw new Error('API key is not account-scoped');
163
+ }
164
+ const account = await this.accountCollection.findOne({ _id: apiKey.scope_id });
165
+ if (!account) {
166
+ throw new Error('Account not found');
167
+ }
168
+ return account;
169
+ }
170
+ /**
171
+ * Get the principal from the request context
172
+ * @param context The execution context
173
+ * @returns The principal or null if not found
174
+ */
175
+ async getPrincipal(context) {
176
+ const apiKey = await this.getApiKey(context);
177
+ if (apiKey) {
178
+ return {
179
+ entity: apiKey,
180
+ principal_type: trailmixModels.Principal.ApiKey,
181
+ };
182
+ }
183
+ const account = await this.getAccount(context);
184
+ if (!account) {
185
+ return null;
186
+ }
187
+ return {
188
+ entity: account,
189
+ principal_type: trailmixModels.Principal.Account,
190
+ };
191
+ }
192
+ async getAccount(context) {
193
+ const request = context.switchToHttp().getRequest();
194
+ const auth = (0, fastify_1.getAuth)(request);
195
+ if (!auth.userId) {
196
+ return null;
197
+ }
198
+ // TODO: Cache user
199
+ // const cachedUser = await this.userCache.getUser(auth.userId);
200
+ // if (cachedUser) {
201
+ // return {
202
+ // account: cachedUser.account,
203
+ // userPublicMetadata: cachedUser.metadata,
204
+ // };
205
+ // }
206
+ const existingAccount = await this.accountService.getAccount(auth.userId);
207
+ if (existingAccount) {
208
+ return existingAccount;
209
+ }
210
+ const account = await this.accountService.upsertAccount(auth.userId);
211
+ // TODO: Lock this step to prevent race conditions
212
+ this.logger.log(`Validating account using auth guard hook: ${auth.userId}`);
213
+ this.logger.log(`Auth guard hook: ${this.authGuardHook}`);
214
+ if (this.authGuardHook) {
215
+ const authGuardHookresult = await this.authGuardHook.onHook(account);
216
+ if (!authGuardHookresult) {
217
+ this.logger.error('Failed to validate account using auth guard hook', {
218
+ userId: auth.userId,
219
+ accountId: account?._id,
220
+ });
221
+ throw new Error('Failed to validate account using auth guard hook');
222
+ }
223
+ }
224
+ // TODO: Cache user
225
+ // await this.userCache.cacheUser(auth.userId, {
226
+ // account: account!,
227
+ // metadata: user.publicMetadata,
228
+ // });
229
+ return account;
230
+ }
231
+ async getApiKey(context) {
232
+ if (!this.apiKeyCollection) {
233
+ return null;
234
+ }
235
+ const request = context.switchToHttp().getRequest();
236
+ const apiKey = request.headers[trailmixModels.API_KEY_HEADER];
237
+ if (!apiKey) {
238
+ return null;
239
+ }
240
+ // TODO: Cache api key
241
+ const apiKeyEntity = await this.apiKeyCollection.findOne({ api_key: apiKey });
242
+ if (!apiKeyEntity || apiKeyEntity.disabled) {
243
+ return null;
244
+ }
245
+ return apiKeyEntity;
246
+ }
247
+ };
248
+ exports.AuthService = AuthService;
249
+ exports.AuthService = AuthService = AuthService_1 = __decorate([
250
+ (0, common_1.Injectable)(),
251
+ __param(4, (0, common_1.Optional)()),
252
+ __param(4, (0, common_1.Inject)(constants_1.PROVIDER_SYMBOLS.AUTH_GUARD_HOOK)),
253
+ __param(5, (0, common_1.Optional)()),
254
+ __metadata("design:paramtypes", [account_service_1.AccountService,
255
+ collections_1.AccountCollection,
256
+ global_role_service_1.GlobalRoleService,
257
+ collections_1.SecurityAuditCollection, Object, collections_1.ApiKeyCollection])
258
+ ], AuthService);
259
+ //# sourceMappingURL=auth.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file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