@terreno/api 57.19.2 → 57.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +443 -0
- package/biome.jsonc +1 -0
- package/dist/accountSecurity/accessActions.d.ts +38 -0
- package/dist/accountSecurity/accessActions.js +157 -0
- package/dist/accountSecurity/accountSecurityApp.d.ts +219 -0
- package/dist/accountSecurity/accountSecurityApp.js +438 -0
- package/dist/accountSecurity/accountSecurityService.d.ts +200 -0
- package/dist/accountSecurity/accountSecurityService.js +481 -0
- package/dist/accountSecurity/credentialActions.d.ts +16 -0
- package/dist/accountSecurity/credentialActions.js +189 -0
- package/dist/accountSecurity/membershipActions.d.ts +15 -0
- package/dist/accountSecurity/membershipActions.js +81 -0
- package/dist/accountSecurity/orgSecurityRoutes.d.ts +50 -0
- package/dist/accountSecurity/orgSecurityRoutes.js +149 -0
- package/dist/accountSecurity/securityEventHooks.d.ts +44 -0
- package/dist/accountSecurity/securityEventHooks.js +223 -0
- package/dist/accountSecurity/securityEvents.d.ts +162 -0
- package/dist/accountSecurity/securityEvents.js +114 -0
- package/dist/accountSecurity/securityNotifications.d.ts +125 -0
- package/dist/accountSecurity/securityNotifications.js +333 -0
- package/dist/api.d.ts +3 -3
- package/dist/api.js +38 -6
- package/dist/audit/auditEventModel.d.ts +5 -3
- package/dist/audit/auditEventModel.js +8 -6
- package/dist/audit/processQueue.js +4 -2
- package/dist/audit/record.d.ts +2 -2
- package/dist/audit/record.js +3 -0
- package/dist/authRecovery.js +29 -1
- package/dist/betterAuth.d.ts +179 -10
- package/dist/betterAuthApp.d.ts +15 -0
- package/dist/betterAuthApp.js +23 -0
- package/dist/betterAuthChallengeHeaders.d.ts +37 -0
- package/dist/betterAuthChallengeHeaders.js +180 -0
- package/dist/betterAuthFeatures.d.ts +54 -0
- package/dist/betterAuthFeatures.js +239 -0
- package/dist/betterAuthMethods.d.ts +73 -0
- package/dist/betterAuthMethods.js +55 -0
- package/dist/betterAuthOAuthHandoff.d.ts +10 -2
- package/dist/betterAuthOAuthHandoff.js +222 -24
- package/dist/betterAuthSetPassword.d.ts +33 -0
- package/dist/betterAuthSetPassword.js +166 -0
- package/dist/betterAuthSetup.d.ts +57 -8
- package/dist/betterAuthSetup.js +327 -36
- package/dist/consentApp.d.ts +9 -4
- package/dist/consentApp.js +344 -28
- package/dist/consentStatus.d.ts +60 -0
- package/dist/consentStatus.js +111 -0
- package/dist/errors.d.ts +6 -0
- package/dist/errors.js +41 -2
- package/dist/expressServer.js +3 -2
- package/dist/frameworkModelNames.d.ts +4 -0
- package/dist/frameworkModelNames.js +4 -0
- package/dist/index.d.ts +18 -2
- package/dist/index.js +29 -3
- package/dist/mcp/adminTracking.d.ts +28 -0
- package/dist/mcp/adminTracking.js +80 -0
- package/dist/mcp/auth.d.ts +6 -0
- package/dist/mcp/auth.js +12 -2
- package/dist/mcp/oauth.js +151 -11
- package/dist/mcp/oauthMetadata.d.ts +2 -1
- package/dist/mcp/oauthMetadata.js +7 -4
- package/dist/mcp/server.d.ts +8 -0
- package/dist/mcp/server.js +48 -4
- package/dist/models/consentResponse.js +51 -0
- package/dist/models/mcpOAuthClient.js +78 -2
- package/dist/models/mcpOAuthToken.js +55 -4
- package/dist/models/mcpRequestLog.d.ts +2 -0
- package/dist/models/mcpRequestLog.js +73 -0
- package/dist/models/mcpServiceToken.js +4 -0
- package/dist/models/mcpToolSetting.d.ts +2 -0
- package/dist/models/mcpToolSetting.js +119 -0
- package/dist/models/versionConfig.d.ts +2 -0
- package/dist/orgs/membershipGuards.d.ts +7 -0
- package/dist/orgs/membershipGuards.js +32 -0
- package/dist/orgs/orgsApp.d.ts +19 -1
- package/dist/orgs/orgsApp.js +55 -24
- package/dist/rateLimit/policies.js +8 -0
- package/dist/rbac/access.js +2 -1
- package/dist/rbac/auditModel.d.ts +2 -1
- package/dist/rbac/resolve.d.ts +9 -1
- package/dist/rbac/resolve.js +21 -16
- package/dist/rbac/roleManager.d.ts +6 -0
- package/dist/rbac/roleManager.js +8 -7
- package/dist/rbac/roleModel.d.ts +1 -0
- package/dist/rbac/roleModel.js +45 -3
- package/dist/rbac/statements.d.ts +2 -2
- package/dist/rbac/statements.js +10 -1
- package/dist/rbac/types.d.ts +6 -0
- package/dist/rbac/userWriteGuards.js +2 -1
- package/dist/schemaMetadata.js +8 -1
- package/dist/seedRunner.js +7 -2
- package/dist/terrenoApp.d.ts +16 -0
- package/dist/terrenoApp.js +18 -0
- package/dist/tests/models.d.ts +22 -22
- package/dist/tests/models.js +15 -1
- package/dist/tests/testHelper.d.ts +6 -12
- package/dist/tests.d.ts +1 -0
- package/dist/tests.js +3 -1
- package/dist/types/consentResponse.d.ts +8 -0
- package/dist/types/mcpAdmin.d.ts +58 -0
- package/dist/types/mcpAdmin.js +2 -0
- package/dist/types/mcpOAuth.d.ts +37 -2
- package/dist/types/mcpServiceToken.d.ts +2 -0
- package/e2e/smoke.ts +193 -0
- package/package.json +10 -5
- package/src/__snapshots__/openApi.test.ts.snap +288 -0
- package/src/__snapshots__/openApiBuilder.test.ts.snap +96 -0
- package/src/accountSecurity/accessActions.test.ts +723 -0
- package/src/accountSecurity/accessActions.ts +173 -0
- package/src/accountSecurity/accountSecurity.test.ts +947 -0
- package/src/accountSecurity/accountSecurityApp.ts +720 -0
- package/src/accountSecurity/accountSecurityService.ts +826 -0
- package/src/accountSecurity/credentialActions.test.ts +867 -0
- package/src/accountSecurity/credentialActions.ts +260 -0
- package/src/accountSecurity/membershipActions.ts +89 -0
- package/src/accountSecurity/orgSecurityRoutes.test.ts +841 -0
- package/src/accountSecurity/orgSecurityRoutes.ts +232 -0
- package/src/accountSecurity/securityEventHooks.ts +294 -0
- package/src/accountSecurity/securityEvents.ts +165 -0
- package/src/accountSecurity/securityNotifications.test.ts +1323 -0
- package/src/accountSecurity/securityNotifications.ts +516 -0
- package/src/adminModelRouter.test.ts +8 -6
- package/src/api.arrayOperations.test.ts +102 -22
- package/src/api.hooks.test.ts +12 -11
- package/src/api.query.test.ts +2 -2
- package/src/api.test.ts +8 -7
- package/src/api.ts +41 -10
- package/src/audit/auditApp.test.ts +1 -1
- package/src/audit/auditEventModel.ts +10 -7
- package/src/audit/cloudTasksEnqueue.test.ts +19 -14
- package/src/audit/processQueue.ts +4 -2
- package/src/audit/record.test.ts +15 -0
- package/src/audit/record.ts +10 -2
- package/src/auth.test.ts +21 -7
- package/src/authRecovery.test.ts +33 -10
- package/src/authRecovery.ts +34 -1
- package/src/authRecoveryBetterAuth.test.ts +169 -0
- package/src/authVerification.test.ts +7 -7
- package/src/betterAuth.ts +194 -8
- package/src/betterAuthApp.ts +32 -0
- package/src/betterAuthChallengeHeaders.test.ts +476 -0
- package/src/betterAuthChallengeHeaders.ts +227 -0
- package/src/betterAuthFeatures.test.ts +490 -0
- package/src/betterAuthFeatures.ts +283 -0
- package/src/betterAuthLinking.test.ts +846 -0
- package/src/betterAuthMethods.test.ts +138 -0
- package/src/betterAuthMethods.ts +98 -0
- package/src/betterAuthOAuthHandoff.test.ts +482 -69
- package/src/betterAuthOAuthHandoff.ts +293 -33
- package/src/betterAuthOAuthProxy.test.ts +421 -0
- package/src/betterAuthPasswordReset.test.ts +190 -0
- package/src/betterAuthSetPassword.test.ts +378 -0
- package/src/betterAuthSetPassword.ts +213 -0
- package/src/betterAuthSetup.test.ts +115 -57
- package/src/betterAuthSetup.ts +457 -38
- package/src/collectionRegistry.test.ts +4 -4
- package/src/consentApp.test.ts +869 -2
- package/src/consentApp.ts +438 -36
- package/src/consentStatus.test.ts +226 -0
- package/src/consentStatus.ts +171 -0
- package/src/errors.test.ts +81 -0
- package/src/errors.ts +50 -1
- package/src/expressServer.test.ts +29 -0
- package/src/expressServer.ts +3 -2
- package/src/frameworkModelNames.test.ts +8 -0
- package/src/frameworkModelNames.ts +4 -0
- package/src/githubAuth.test.ts +11 -11
- package/src/index.ts +38 -0
- package/src/isolated/migrateCliConnect.isolated.ts +3 -2
- package/src/mcp/actionTools.test.ts +11 -5
- package/src/mcp/adminTracking.test.ts +272 -0
- package/src/mcp/adminTracking.ts +104 -0
- package/src/mcp/auth.ts +18 -2
- package/src/mcp/oauth.test.ts +463 -2
- package/src/mcp/oauth.ts +184 -13
- package/src/mcp/oauthMetadata.ts +5 -2
- package/src/mcp/server.ts +67 -4
- package/src/mcp/serviceTokens.test.ts +2 -1
- package/src/mcp/toolGenerator.test.ts +9 -5
- package/src/migrations/cli.test.ts +15 -14
- package/src/migrations/cli.ts +1 -1
- package/src/migrations/generate.test.ts +2 -2
- package/src/migrations/load.test.ts +1 -1
- package/src/migrations/lock.test.ts +8 -3
- package/src/migrations/runner.test.ts +16 -5
- package/src/migrations/schemaCatalog.test.ts +4 -1
- package/src/models/consentResponse.ts +53 -0
- package/src/models/mcpOAuthClient.ts +100 -3
- package/src/models/mcpOAuthToken.test.ts +42 -0
- package/src/models/mcpOAuthToken.ts +62 -5
- package/src/models/mcpRequestLog.ts +81 -0
- package/src/models/mcpServiceToken.test.ts +23 -0
- package/src/models/mcpServiceToken.ts +10 -0
- package/src/models/mcpToolSetting.test.ts +69 -0
- package/src/models/mcpToolSetting.ts +138 -0
- package/src/models/versionConfig.ts +2 -0
- package/src/notifications/notificationsApp.test.ts +5 -8
- package/src/notifiers/slackWebApi.test.ts +30 -8
- package/src/openApi.test.ts +4 -4
- package/src/openApiValidator.test.ts +3 -3
- package/src/orgs/membershipGuards.ts +39 -0
- package/src/orgs/orgPermissions.test.ts +2 -2
- package/src/orgs/orgPlugin.test.ts +4 -3
- package/src/orgs/orgRbac.test.ts +1 -1
- package/src/orgs/orgSecurityDefects.test.ts +3 -1
- package/src/orgs/organizationSettings.test.ts +1 -1
- package/src/orgs/orgsApp.test.ts +104 -3
- package/src/orgs/orgsApp.ts +69 -28
- package/src/permissions.test.ts +2 -2
- package/src/rateLimit/policies.test.ts +53 -0
- package/src/rateLimit/policies.ts +8 -0
- package/src/rateLimit/redisStore.ioredis.test.ts +3 -2
- package/src/rbac/access.ts +4 -1
- package/src/rbac/assertAllowed.test.ts +1 -1
- package/src/rbac/auditModel.ts +10 -2
- package/src/rbac/realtimeAccess.test.ts +3 -2
- package/src/rbac/resolve.test.ts +65 -0
- package/src/rbac/resolve.ts +37 -17
- package/src/rbac/roleManager.test.ts +64 -0
- package/src/rbac/roleManager.ts +15 -6
- package/src/rbac/roleModel.test.ts +174 -1
- package/src/rbac/roleModel.ts +66 -3
- package/src/rbac/routes.test.ts +58 -0
- package/src/rbac/statements.test.ts +4 -1
- package/src/rbac/statements.ts +11 -1
- package/src/rbac/types.ts +6 -1
- package/src/rbac/userWriteGuards.test.ts +56 -5
- package/src/rbac/userWriteGuards.ts +5 -2
- package/src/realtime/realtime.test.ts +35 -31
- package/src/schemaMetadata.test.ts +39 -0
- package/src/schemaMetadata.ts +11 -1
- package/src/scrubAdminFields.test.ts +1 -1
- package/src/seedRunner.test.ts +56 -0
- package/src/seedRunner.ts +7 -2
- package/src/sync/adminWindowMutation.test.ts +4 -3
- package/src/sync/executors.test.ts +1 -1
- package/src/sync/scripts/compactTombstones.test.ts +4 -3
- package/src/sync/sync.test.ts +1 -1
- package/src/sync/syncPhaseC.test.ts +14 -11
- package/src/sync/syncRoutes.test.ts +8 -8
- package/src/sync/syncSeqPlugin.test.ts +1 -2
- package/src/sync/syncSocket.test.ts +5 -4
- package/src/terrenoApp.mcpServiceTokens.test.ts +2 -2
- package/src/terrenoApp.migrations.test.ts +8 -7
- package/src/terrenoApp.ts +34 -0
- package/src/tests/accountSecurityTestApp.ts +290 -0
- package/src/tests/betterAuthStubs.ts +189 -0
- package/src/tests/bunSetup.ts +1 -1
- package/src/tests/mockOidcProvider.ts +105 -0
- package/src/tests/models.ts +23 -0
- package/src/tests/softwareAuthenticator.ts +183 -0
- package/src/tests/testEnv.ts +7 -0
- package/src/tests.ts +1 -0
- package/src/types/consentResponse.ts +8 -0
- package/src/types/mcpAdmin.ts +80 -0
- package/src/types/mcpOAuth.ts +44 -3
- package/src/types/mcpServiceToken.ts +5 -0
- package/src/webhooks/verifiers/hmac.test.ts +1 -1
- package/tsconfig.typecheck.json +14 -0
|
@@ -20,6 +20,14 @@ import {AdminOwnerTransformer} from "./transformers";
|
|
|
20
20
|
|
|
21
21
|
type TestUser = Awaited<ReturnType<typeof setupDb>>[number];
|
|
22
22
|
|
|
23
|
+
/** Returns a document a postUpdate hook captured, failing the test if the hook never ran. */
|
|
24
|
+
const captured = (doc: Food | undefined): Food => {
|
|
25
|
+
if (!doc) {
|
|
26
|
+
throw new Error("Expected postUpdate to capture a document");
|
|
27
|
+
}
|
|
28
|
+
return doc;
|
|
29
|
+
};
|
|
30
|
+
|
|
23
31
|
describe("model array operations", () => {
|
|
24
32
|
let _server: TestAgent;
|
|
25
33
|
let app: express.Application;
|
|
@@ -219,7 +227,7 @@ describe("model array operations", () => {
|
|
|
219
227
|
|
|
220
228
|
expect(updatedCategory.updated).not.toBe(updatedCategory.created);
|
|
221
229
|
expect(unchangedCategory.updated).toBe(unchangedCategory.created);
|
|
222
|
-
expect(updatedCategory
|
|
230
|
+
expect(updatedCategory?.name).toBe("Updated Category");
|
|
223
231
|
// Unchanged.
|
|
224
232
|
expect(updatedCategory.show).toBe(true);
|
|
225
233
|
expect(unchangedCategory.show).toBe(true);
|
|
@@ -268,8 +276,8 @@ describe("model array operations", () => {
|
|
|
268
276
|
expect(postUpdateDoc).not.toBe(postUpdatePrevDoc);
|
|
269
277
|
|
|
270
278
|
// Verify the content is different (new category added)
|
|
271
|
-
expect(postUpdateDoc.categories).toHaveLength(3);
|
|
272
|
-
expect(postUpdatePrevDoc.categories).toHaveLength(2);
|
|
279
|
+
expect(captured(postUpdateDoc).categories).toHaveLength(3);
|
|
280
|
+
expect(captured(postUpdatePrevDoc).categories).toHaveLength(2);
|
|
273
281
|
|
|
274
282
|
// Reset for next test
|
|
275
283
|
postUpdateCalled = false;
|
|
@@ -294,15 +302,15 @@ describe("model array operations", () => {
|
|
|
294
302
|
expect(postUpdateDoc).not.toBe(postUpdatePrevDoc);
|
|
295
303
|
|
|
296
304
|
// Verify the content is different (category updated)
|
|
297
|
-
const updatedCategory = postUpdateDoc.categories.find(
|
|
298
|
-
(c: FoodCategory) => c._id
|
|
305
|
+
const updatedCategory = captured(postUpdateDoc).categories.find(
|
|
306
|
+
(c: FoodCategory) => c._id?.toString() === categoryId.toString()
|
|
299
307
|
);
|
|
300
|
-
const prevCategory = postUpdatePrevDoc.categories.find(
|
|
301
|
-
(c: FoodCategory) => c._id
|
|
308
|
+
const prevCategory = captured(postUpdatePrevDoc).categories.find(
|
|
309
|
+
(c: FoodCategory) => c._id?.toString() === categoryId.toString()
|
|
302
310
|
);
|
|
303
311
|
|
|
304
|
-
expect(updatedCategory
|
|
305
|
-
expect(prevCategory
|
|
312
|
+
expect(updatedCategory?.name).toBe("Updated Category");
|
|
313
|
+
expect(prevCategory?.name).toBe("Fruit");
|
|
306
314
|
|
|
307
315
|
// Reset for next test
|
|
308
316
|
postUpdateCalled = false;
|
|
@@ -320,11 +328,11 @@ describe("model array operations", () => {
|
|
|
320
328
|
expect(postUpdateDoc).not.toBe(postUpdatePrevDoc);
|
|
321
329
|
|
|
322
330
|
// Verify the content is different (category removed)
|
|
323
|
-
const remainingCategories = postUpdateDoc.categories.filter(
|
|
324
|
-
(c: FoodCategory) => c._id
|
|
331
|
+
const remainingCategories = captured(postUpdateDoc).categories.filter(
|
|
332
|
+
(c: FoodCategory) => c._id?.toString() === categoryId.toString()
|
|
325
333
|
);
|
|
326
|
-
const prevCategories = postUpdatePrevDoc.categories.filter(
|
|
327
|
-
(c: FoodCategory) => c._id
|
|
334
|
+
const prevCategories = captured(postUpdatePrevDoc).categories.filter(
|
|
335
|
+
(c: FoodCategory) => c._id?.toString() === categoryId.toString()
|
|
328
336
|
);
|
|
329
337
|
|
|
330
338
|
expect(remainingCategories).toHaveLength(0);
|
|
@@ -371,10 +379,10 @@ describe("model array operations", () => {
|
|
|
371
379
|
expect(postUpdateDoc).not.toBe(postUpdatePrevDoc);
|
|
372
380
|
|
|
373
381
|
// Verify the content is different (new tag added)
|
|
374
|
-
expect(postUpdateDoc.tags).toHaveLength(3);
|
|
375
|
-
expect(postUpdatePrevDoc.tags).toHaveLength(2);
|
|
376
|
-
expect(postUpdateDoc.tags).toContain("organic");
|
|
377
|
-
expect(postUpdatePrevDoc.tags).not.toContain("organic");
|
|
382
|
+
expect(captured(postUpdateDoc).tags).toHaveLength(3);
|
|
383
|
+
expect(captured(postUpdatePrevDoc).tags).toHaveLength(2);
|
|
384
|
+
expect(captured(postUpdateDoc).tags).toContain("organic");
|
|
385
|
+
expect(captured(postUpdatePrevDoc).tags).not.toContain("organic");
|
|
378
386
|
|
|
379
387
|
// Reset for next test
|
|
380
388
|
postUpdateCalled = false;
|
|
@@ -388,10 +396,10 @@ describe("model array operations", () => {
|
|
|
388
396
|
expect(postUpdateDoc).not.toBe(postUpdatePrevDoc);
|
|
389
397
|
|
|
390
398
|
// Verify the content is different (tag updated)
|
|
391
|
-
expect(postUpdateDoc.tags).toContain("super-healthy");
|
|
392
|
-
expect(postUpdatePrevDoc.tags).toContain("healthy");
|
|
393
|
-
expect(postUpdateDoc.tags).not.toContain("healthy");
|
|
394
|
-
expect(postUpdatePrevDoc.tags).not.toContain("super-healthy");
|
|
399
|
+
expect(captured(postUpdateDoc).tags).toContain("super-healthy");
|
|
400
|
+
expect(captured(postUpdatePrevDoc).tags).toContain("healthy");
|
|
401
|
+
expect(captured(postUpdateDoc).tags).not.toContain("healthy");
|
|
402
|
+
expect(captured(postUpdatePrevDoc).tags).not.toContain("super-healthy");
|
|
395
403
|
});
|
|
396
404
|
});
|
|
397
405
|
|
|
@@ -829,7 +837,8 @@ describe("array subdocument PATCH with a stripping preUpdate", () => {
|
|
|
829
837
|
read: [Permissions.IsAdmin],
|
|
830
838
|
update: [Permissions.IsAdmin],
|
|
831
839
|
},
|
|
832
|
-
|
|
840
|
+
// The hook returns the stripped update body, not a whole document.
|
|
841
|
+
preUpdate: (body) => preUpdate(body) as Food,
|
|
833
842
|
})
|
|
834
843
|
);
|
|
835
844
|
agent = await authAsUser(app, "admin");
|
|
@@ -869,4 +878,75 @@ describe("array subdocument PATCH with a stripping preUpdate", () => {
|
|
|
869
878
|
expect(stored?.categories?.[1]?.show).toBe(true);
|
|
870
879
|
expect(String(stored?.categories?.[1]?._id)).toBe(String(categoryId));
|
|
871
880
|
});
|
|
881
|
+
|
|
882
|
+
it("persists nested subdocument arrays across repeated PATCHes", async () => {
|
|
883
|
+
await mountStrippingRouter((body) => body);
|
|
884
|
+
const categoryId = apple.categories[0]._id;
|
|
885
|
+
await agent
|
|
886
|
+
.patch(`/food/${apple._id}/categories/${categoryId}`)
|
|
887
|
+
.send({categories: {addendums: [{text: "First"}]}})
|
|
888
|
+
.expect(200);
|
|
889
|
+
|
|
890
|
+
const secondPatch = await agent
|
|
891
|
+
.patch(`/food/${apple._id}/categories/${categoryId}`)
|
|
892
|
+
.send({categories: {addendums: [{text: "Second"}]}})
|
|
893
|
+
.expect(200);
|
|
894
|
+
|
|
895
|
+
expect(secondPatch.body.data.categories[0].addendums[0].text).toBe("Second");
|
|
896
|
+
const stored = await FoodModel.findById(apple._id).lean();
|
|
897
|
+
expect(stored?.categories?.[0]?.addendums?.[0]?.text).toBe("Second");
|
|
898
|
+
});
|
|
899
|
+
|
|
900
|
+
it("updates a sibling field when an array item already has nested subdocuments", async () => {
|
|
901
|
+
await mountStrippingRouter((body) => body);
|
|
902
|
+
const categoryId = apple.categories[0]._id;
|
|
903
|
+
await agent
|
|
904
|
+
.patch(`/food/${apple._id}/categories/${categoryId}`)
|
|
905
|
+
.send({categories: {addendums: [{text: "Existing"}]}})
|
|
906
|
+
.expect(200);
|
|
907
|
+
await agent
|
|
908
|
+
.patch(`/food/${apple._id}/categories/${categoryId}`)
|
|
909
|
+
.send({categories: {name: "Renamed"}})
|
|
910
|
+
.expect(200);
|
|
911
|
+
|
|
912
|
+
const stored = await FoodModel.findById(apple._id).lean();
|
|
913
|
+
expect(stored?.categories?.[0]?.name).toBe("Renamed");
|
|
914
|
+
expect(stored?.categories?.[0]?.addendums?.[0]?.text).toBe("Existing");
|
|
915
|
+
});
|
|
916
|
+
|
|
917
|
+
it("keeps nested subdocuments unchanged when preUpdate strips the array field", async () => {
|
|
918
|
+
await mountStrippingRouter((body) => {
|
|
919
|
+
const {categories: _categories, ...rest} = body;
|
|
920
|
+
return rest;
|
|
921
|
+
});
|
|
922
|
+
const categoryId = apple.categories[0]._id;
|
|
923
|
+
await FoodModel.updateOne(
|
|
924
|
+
{_id: apple._id},
|
|
925
|
+
{$set: {"categories.0.addendums": [{text: "Existing"}]}}
|
|
926
|
+
);
|
|
927
|
+
|
|
928
|
+
await agent
|
|
929
|
+
.patch(`/food/${apple._id}/categories/${categoryId}`)
|
|
930
|
+
.send({categories: {addendums: [{text: "Changed"}], name: "Renamed"}})
|
|
931
|
+
.expect(200);
|
|
932
|
+
|
|
933
|
+
const stored = await FoodModel.findById(apple._id).lean();
|
|
934
|
+
expect(stored?.categories?.[0]?.name).toBe("Fruit");
|
|
935
|
+
expect(stored?.categories?.[0]?.addendums?.[0]?.text).toBe("Existing");
|
|
936
|
+
});
|
|
937
|
+
|
|
938
|
+
it("applies nested subdocuments returned by preUpdate and keeps the item id", async () => {
|
|
939
|
+
await mountStrippingRouter((body) => ({...body}));
|
|
940
|
+
const categoryId = apple.categories[0]._id;
|
|
941
|
+
const res = await agent
|
|
942
|
+
.patch(`/food/${apple._id}/categories/${categoryId}`)
|
|
943
|
+
.send({categories: {addendums: [{text: "Applied"}]}})
|
|
944
|
+
.expect(200);
|
|
945
|
+
|
|
946
|
+
expect(res.body.data.categories[0]._id).toBe(String(categoryId));
|
|
947
|
+
expect(res.body.data.categories[0].addendums[0].text).toBe("Applied");
|
|
948
|
+
const stored = await FoodModel.findById(apple._id).lean();
|
|
949
|
+
expect(String(stored?.categories?.[0]?._id)).toBe(String(categoryId));
|
|
950
|
+
expect(stored?.categories?.[0]?.addendums?.[0]?.text).toBe("Applied");
|
|
951
|
+
});
|
|
872
952
|
});
|
package/src/api.hooks.test.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {beforeEach, describe, expect, it, type mock} from "bun:test";
|
|
2
2
|
import * as Sentry from "@sentry/bun";
|
|
3
3
|
import type express from "express";
|
|
4
|
+
import type {HydratedDocument} from "mongoose";
|
|
4
5
|
import supertest from "supertest";
|
|
5
6
|
import type TestAgent from "supertest/lib/agent";
|
|
6
7
|
|
|
@@ -48,8 +49,9 @@ describe("pre and post hooks", () => {
|
|
|
48
49
|
update: [Permissions.IsAny],
|
|
49
50
|
},
|
|
50
51
|
preCreate: (data) => {
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
const food = data as Partial<Food>;
|
|
53
|
+
food.calories = 14;
|
|
54
|
+
return food as Food;
|
|
53
55
|
},
|
|
54
56
|
preDelete: (data) => {
|
|
55
57
|
deleteCalled = true;
|
|
@@ -57,7 +59,7 @@ describe("pre and post hooks", () => {
|
|
|
57
59
|
},
|
|
58
60
|
preUpdate: (data) => {
|
|
59
61
|
data.calories = 15;
|
|
60
|
-
return data;
|
|
62
|
+
return data as Food;
|
|
61
63
|
},
|
|
62
64
|
})
|
|
63
65
|
);
|
|
@@ -158,18 +160,17 @@ describe("pre and post hooks", () => {
|
|
|
158
160
|
update: [Permissions.IsAny],
|
|
159
161
|
},
|
|
160
162
|
postCreate: async (data) => {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
163
|
+
const food = data as HydratedDocument<Food>;
|
|
164
|
+
food.calories = 14;
|
|
165
|
+
await food.save();
|
|
164
166
|
},
|
|
165
|
-
postDelete: (
|
|
167
|
+
postDelete: () => {
|
|
166
168
|
deleteCalled = true;
|
|
167
|
-
return data;
|
|
168
169
|
},
|
|
169
170
|
postUpdate: async (data) => {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
171
|
+
const food = data as HydratedDocument<Food>;
|
|
172
|
+
food.calories = 15;
|
|
173
|
+
await food.save();
|
|
173
174
|
},
|
|
174
175
|
})
|
|
175
176
|
);
|
package/src/api.query.test.ts
CHANGED
|
@@ -471,7 +471,7 @@ describe("query and list methods", () => {
|
|
|
471
471
|
.get(`/food?${qs.stringify({$or: [{name: "Carrots"}, {name: "Pizza"}]})}`)
|
|
472
472
|
.expect(200);
|
|
473
473
|
expect(res.body.data).toHaveLength(2);
|
|
474
|
-
const ids1 = res.body.data.map((d:
|
|
474
|
+
const ids1 = res.body.data.map((d: {id: string}) => d.id);
|
|
475
475
|
expect(ids1).toEqual(expect.arrayContaining([carrots?._id.toString(), pizza?._id.toString()]));
|
|
476
476
|
expect(ids1).toHaveLength(2);
|
|
477
477
|
});
|
|
@@ -486,7 +486,7 @@ describe("query and list methods", () => {
|
|
|
486
486
|
)
|
|
487
487
|
.expect(200);
|
|
488
488
|
expect(res.body.data).toHaveLength(2);
|
|
489
|
-
const ids2 = res.body.data.map((d:
|
|
489
|
+
const ids2 = res.body.data.map((d: {id: string}) => d.id);
|
|
490
490
|
expect(ids2).toEqual(
|
|
491
491
|
expect.arrayContaining([carrots?._id.toString(), spinach?._id.toString()])
|
|
492
492
|
);
|
package/src/api.test.ts
CHANGED
|
@@ -5,11 +5,12 @@ import mongoose, {type Model} from "mongoose";
|
|
|
5
5
|
import supertest from "supertest";
|
|
6
6
|
import type TestAgent from "supertest/lib/agent";
|
|
7
7
|
|
|
8
|
-
import {addPopulateToQuery, modelRouter} from "./api";
|
|
8
|
+
import {addPopulateToQuery, type JSONValue, modelRouter} from "./api";
|
|
9
9
|
import {type UserModel as AuthUserModel, addAuthRoutes, setupAuth} from "./auth";
|
|
10
10
|
import {APIError} from "./errors";
|
|
11
11
|
import {clearMCPRegistry, getMCPRegistry} from "./mcp/registry";
|
|
12
12
|
import {Permissions} from "./permissions";
|
|
13
|
+
import type {AnyTerrenoAccess} from "./rbac/types";
|
|
13
14
|
import {
|
|
14
15
|
authAsUser,
|
|
15
16
|
type Food,
|
|
@@ -403,7 +404,7 @@ describe("@terreno/api", () => {
|
|
|
403
404
|
read: [Permissions.IsAny],
|
|
404
405
|
update: [Permissions.IsAny],
|
|
405
406
|
},
|
|
406
|
-
responseHandler: async (_data, method) => {
|
|
407
|
+
responseHandler: async (_data, method): Promise<JSONValue> => {
|
|
407
408
|
if (method === "list") {
|
|
408
409
|
return {custom: "response"};
|
|
409
410
|
}
|
|
@@ -1443,7 +1444,7 @@ describe("@terreno/api", () => {
|
|
|
1443
1444
|
|
|
1444
1445
|
it("model.create validation error is handled", async () => {
|
|
1445
1446
|
// Use a model that has required fields
|
|
1446
|
-
const {RequiredModel} = await import("./tests");
|
|
1447
|
+
const {RequiredModel} = await import("./tests.js");
|
|
1447
1448
|
|
|
1448
1449
|
app.use(
|
|
1449
1450
|
"/required",
|
|
@@ -2234,8 +2235,8 @@ describe("@terreno/api", () => {
|
|
|
2234
2235
|
}),
|
|
2235
2236
|
queryFilter: async (): Promise<Record<string, unknown>> => ({}),
|
|
2236
2237
|
statements: {food: ["list", "read", "create", "update", "delete"]},
|
|
2237
|
-
};
|
|
2238
|
-
registration._buildWithContext({accessControl
|
|
2238
|
+
} as unknown as AnyTerrenoAccess;
|
|
2239
|
+
registration._buildWithContext({accessControl});
|
|
2239
2240
|
|
|
2240
2241
|
const after = getMCPRegistry()[0];
|
|
2241
2242
|
expect(after?.options.accessControl).toBe(accessControl);
|
|
@@ -2253,10 +2254,10 @@ describe("@terreno/api", () => {
|
|
|
2253
2254
|
}),
|
|
2254
2255
|
queryFilter: async (): Promise<Record<string, unknown>> => ({}),
|
|
2255
2256
|
statements: {food: ["list", "read", "create", "update", "delete"]},
|
|
2256
|
-
};
|
|
2257
|
+
} as unknown as AnyTerrenoAccess;
|
|
2257
2258
|
modelRouter(FoodModel, {
|
|
2258
2259
|
access: {resource: "food"},
|
|
2259
|
-
accessControl
|
|
2260
|
+
accessControl,
|
|
2260
2261
|
mcp: {methods: ["list"]},
|
|
2261
2262
|
permissions: {
|
|
2262
2263
|
create: [Permissions.IsAny],
|
package/src/api.ts
CHANGED
|
@@ -264,7 +264,7 @@ export interface ModelRouterOptions<T> {
|
|
|
264
264
|
preCreate?: (
|
|
265
265
|
value: Partial<T> | (Partial<T> | undefined)[] | null | undefined,
|
|
266
266
|
request: express.Request
|
|
267
|
-
) => T | Promise<T
|
|
267
|
+
) => T | null | Promise<T | null>;
|
|
268
268
|
/**
|
|
269
269
|
* Hook that runs after `transformer.transform` but before changes are made for update operations.
|
|
270
270
|
* Can update the body fields based on the request or the user.
|
|
@@ -274,7 +274,7 @@ export interface ModelRouterOptions<T> {
|
|
|
274
274
|
* @param value - The request body relative to the model update (type: Partial<T>). Note: this does not contain the entire document to be updated, only the fields being updated.
|
|
275
275
|
* @param request - The Express request object.
|
|
276
276
|
*/
|
|
277
|
-
preUpdate?: (value: Partial<T>, request: express.Request) => T | Promise<T
|
|
277
|
+
preUpdate?: (value: Partial<T>, request: express.Request) => T | null | Promise<T | null>;
|
|
278
278
|
/**
|
|
279
279
|
* Hook that runs after `transformer.transform` but before the object is deleted.
|
|
280
280
|
* Return null to return a generic 403 error.
|
|
@@ -283,7 +283,7 @@ export interface ModelRouterOptions<T> {
|
|
|
283
283
|
* @param value - The document to be deleted, before the soft update of deleted: true (type: T).
|
|
284
284
|
* @param request - The Express request object.
|
|
285
285
|
*/
|
|
286
|
-
preDelete?: (value: T, request: express.Request) => T | Promise<T
|
|
286
|
+
preDelete?: (value: T, request: express.Request) => T | null | Promise<T | null>;
|
|
287
287
|
/**
|
|
288
288
|
* Hook that runs after the object is created but before the responseHandler serializes and
|
|
289
289
|
* returned. This is a good spot to perform dependent changes to other models or performing async
|
|
@@ -1373,14 +1373,15 @@ const _buildModelRouter = <T>(
|
|
|
1373
1373
|
});
|
|
1374
1374
|
}
|
|
1375
1375
|
// For PATCHing an item by ID, we need to merge the objects so we don't override the _id or
|
|
1376
|
-
// other parts of the subdocument. Merge
|
|
1377
|
-
//
|
|
1378
|
-
//
|
|
1376
|
+
// other parts of the subdocument. Merge plain data from the detached copy: $clone() loses
|
|
1377
|
+
// the subdocument's parent, so assigning a nested document array to it can throw in
|
|
1378
|
+
// Mongoose's setters. The loaded document's live subdocuments must not change until only
|
|
1379
|
+
// the body that survives the hooks below is applied.
|
|
1379
1380
|
if (operation === "PATCH" && isValidObjectId(itemId)) {
|
|
1380
|
-
const current = array[index] as {
|
|
1381
|
+
const current = array[index] as {toObject?: () => object} | object;
|
|
1381
1382
|
const detached =
|
|
1382
|
-
typeof (current as {
|
|
1383
|
-
? (current as {
|
|
1383
|
+
typeof (current as {toObject?: unknown}).toObject === "function"
|
|
1384
|
+
? (current as {toObject: () => object}).toObject()
|
|
1384
1385
|
: {...current};
|
|
1385
1386
|
Object.assign(detached, req.body[field]);
|
|
1386
1387
|
array[index] = detached;
|
|
@@ -1429,7 +1430,37 @@ const _buildModelRouter = <T>(
|
|
|
1429
1430
|
// Using .save here runs the risk of a versioning error if you try to make two simultaneous
|
|
1430
1431
|
// updates. We won't wind up with corrupted data, just an API error.
|
|
1431
1432
|
try {
|
|
1432
|
-
|
|
1433
|
+
const bodyRecord = body as Record<string, unknown>;
|
|
1434
|
+
const finalArray = bodyRecord[field];
|
|
1435
|
+
const liveArray = (doc as unknown as Record<string, unknown>)[field];
|
|
1436
|
+
if (Array.isArray(finalArray) && Array.isArray(liveArray)) {
|
|
1437
|
+
const mergedArray = finalArray.map((item) => {
|
|
1438
|
+
if (!item || typeof item !== "object") {
|
|
1439
|
+
return item;
|
|
1440
|
+
}
|
|
1441
|
+
const itemIdValue = (item as {id?: unknown}).id ?? (item as {_id?: unknown})._id;
|
|
1442
|
+
if (itemIdValue == null) {
|
|
1443
|
+
return item;
|
|
1444
|
+
}
|
|
1445
|
+
const liveItem = liveArray.find((live) => {
|
|
1446
|
+
if (!live || typeof live !== "object") {
|
|
1447
|
+
return false;
|
|
1448
|
+
}
|
|
1449
|
+
const liveId = (live as {id?: unknown}).id ?? (live as {_id?: unknown})._id;
|
|
1450
|
+
return String(liveId) === String(itemIdValue);
|
|
1451
|
+
});
|
|
1452
|
+
if (liveItem && typeof liveItem === "object" && (liveItem as {$__?: unknown}).$__) {
|
|
1453
|
+
Object.assign(liveItem, item);
|
|
1454
|
+
return liveItem;
|
|
1455
|
+
}
|
|
1456
|
+
return item;
|
|
1457
|
+
});
|
|
1458
|
+
(doc as unknown as Record<string, unknown>)[field] = mergedArray;
|
|
1459
|
+
const {[field]: _arrayField, ...restBody} = bodyRecord;
|
|
1460
|
+
Object.assign(doc, restBody);
|
|
1461
|
+
} else {
|
|
1462
|
+
Object.assign(doc, body);
|
|
1463
|
+
}
|
|
1433
1464
|
await doc.save();
|
|
1434
1465
|
} catch (error: unknown) {
|
|
1435
1466
|
throw passthroughOrWrapWrite(error, {
|
|
@@ -48,7 +48,7 @@ describe("AuditApp", () => {
|
|
|
48
48
|
|
|
49
49
|
it("does not register AuditEvent on the default connection when @terreno/api is imported", async () => {
|
|
50
50
|
deleteAuditEventModel();
|
|
51
|
-
await import("../index");
|
|
51
|
+
await import("../index.js");
|
|
52
52
|
assert.isUndefined(mongoose.connection.models.TerrenoAuditEvent);
|
|
53
53
|
});
|
|
54
54
|
|
|
@@ -8,14 +8,17 @@ import {
|
|
|
8
8
|
findOneOrNone,
|
|
9
9
|
} from "../plugins";
|
|
10
10
|
|
|
11
|
-
export type AuditEventVerb = "created" | "deleted" | "updated";
|
|
12
|
-
|
|
11
|
+
export type AuditEventVerb = "created" | "deleted" | "updated" | "viewed";
|
|
12
|
+
/** Every framework surface that writes `AuditEvent` rows. `security` is AccountSecurityApp. */
|
|
13
|
+
export const AUDIT_EVENT_SOURCES = ["admin", "modelRouter", "rbac", "security"] as const;
|
|
14
|
+
export type AuditEventSource = (typeof AUDIT_EVENT_SOURCES)[number];
|
|
13
15
|
export type AuditEventOperation =
|
|
14
16
|
| "arrayPush"
|
|
15
17
|
| "arrayRemove"
|
|
16
18
|
| "arrayUpdate"
|
|
17
19
|
| "create"
|
|
18
20
|
| "delete"
|
|
21
|
+
| "read"
|
|
19
22
|
| "update";
|
|
20
23
|
|
|
21
24
|
type AuditEventMethods = Record<string, never>;
|
|
@@ -74,8 +77,8 @@ const auditEventSchema: AuditEventSchema = new mongoose.Schema<
|
|
|
74
77
|
type: String,
|
|
75
78
|
},
|
|
76
79
|
operation: {
|
|
77
|
-
description: "Fine-grained mutation kind",
|
|
78
|
-
enum: ["arrayPush", "arrayRemove", "arrayUpdate", "create", "delete", "update"],
|
|
80
|
+
description: "Fine-grained mutation kind, or read for an audited read",
|
|
81
|
+
enum: ["arrayPush", "arrayRemove", "arrayUpdate", "create", "delete", "read", "update"],
|
|
79
82
|
required: true,
|
|
80
83
|
type: String,
|
|
81
84
|
},
|
|
@@ -96,14 +99,14 @@ const auditEventSchema: AuditEventSchema = new mongoose.Schema<
|
|
|
96
99
|
},
|
|
97
100
|
source: {
|
|
98
101
|
description: "Which framework surface wrote this event",
|
|
99
|
-
enum: [
|
|
102
|
+
enum: [...AUDIT_EVENT_SOURCES],
|
|
100
103
|
index: true,
|
|
101
104
|
required: true,
|
|
102
105
|
type: String,
|
|
103
106
|
},
|
|
104
107
|
verb: {
|
|
105
|
-
description: "Widget-compatible mutation verb",
|
|
106
|
-
enum: ["created", "deleted", "updated"],
|
|
108
|
+
description: "Widget-compatible mutation verb, or viewed for an audited read",
|
|
109
|
+
enum: ["created", "deleted", "updated", "viewed"],
|
|
107
110
|
index: true,
|
|
108
111
|
required: true,
|
|
109
112
|
type: String,
|
|
@@ -2,6 +2,7 @@ import {describe, it} from "bun:test";
|
|
|
2
2
|
import {assert} from "chai";
|
|
3
3
|
|
|
4
4
|
import {APIError} from "../errors";
|
|
5
|
+
import {partialEnv} from "../tests/testEnv";
|
|
5
6
|
import {auditEnqueueFromEnv, createCloudTasksAuditEnqueue} from "./cloudTasksEnqueue";
|
|
6
7
|
import type {AuditEventWrite} from "./record";
|
|
7
8
|
|
|
@@ -102,23 +103,27 @@ describe("createCloudTasksAuditEnqueue", () => {
|
|
|
102
103
|
|
|
103
104
|
describe("auditEnqueueFromEnv", () => {
|
|
104
105
|
it("returns undefined until project, location, queue, url, and secret are set", () => {
|
|
105
|
-
assert.isUndefined(auditEnqueueFromEnv({}));
|
|
106
|
+
assert.isUndefined(auditEnqueueFromEnv(partialEnv({})));
|
|
106
107
|
assert.isUndefined(
|
|
107
|
-
auditEnqueueFromEnv(
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
108
|
+
auditEnqueueFromEnv(
|
|
109
|
+
partialEnv({
|
|
110
|
+
AUDIT_TASKS_SECRET: "s",
|
|
111
|
+
AUDIT_TASKS_URL: "https://example.com/internal/audit-events",
|
|
112
|
+
GCP_LOCATION: "us-central1",
|
|
113
|
+
GCP_PROJECT: "proj",
|
|
114
|
+
})
|
|
115
|
+
)
|
|
113
116
|
);
|
|
114
117
|
assert.isDefined(
|
|
115
|
-
auditEnqueueFromEnv(
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
118
|
+
auditEnqueueFromEnv(
|
|
119
|
+
partialEnv({
|
|
120
|
+
AUDIT_TASKS_SECRET: "s",
|
|
121
|
+
AUDIT_TASKS_URL: "https://example.com/internal/audit-events",
|
|
122
|
+
GCP_LOCATION: "us-central1",
|
|
123
|
+
GCP_PROJECT: "proj",
|
|
124
|
+
GCP_TASKS_AUDIT_QUEUE: "audit-q",
|
|
125
|
+
})
|
|
126
|
+
)
|
|
122
127
|
);
|
|
123
128
|
});
|
|
124
129
|
});
|
|
@@ -3,6 +3,7 @@ import {timingSafeEqual} from "node:crypto";
|
|
|
3
3
|
import type {Request, Response} from "express";
|
|
4
4
|
|
|
5
5
|
import {APIError} from "../errors";
|
|
6
|
+
import {AUDIT_EVENT_SOURCES} from "./auditEventModel";
|
|
6
7
|
import {AUDIT_SECRET_HEADER} from "./cloudTasksEnqueue";
|
|
7
8
|
import type {AuditEventWrite} from "./record";
|
|
8
9
|
import {persistEnqueuedAuditEvent} from "./record";
|
|
@@ -30,10 +31,11 @@ const isAuditEventWrite = (value: unknown): value is AuditEventWrite => {
|
|
|
30
31
|
"arrayUpdate",
|
|
31
32
|
"create",
|
|
32
33
|
"delete",
|
|
34
|
+
"read",
|
|
33
35
|
"update",
|
|
34
36
|
];
|
|
35
|
-
const validSources: AuditEventWrite["source"][] =
|
|
36
|
-
const validVerbs: AuditEventWrite["verb"][] = ["created", "deleted", "updated"];
|
|
37
|
+
const validSources: readonly AuditEventWrite["source"][] = AUDIT_EVENT_SOURCES;
|
|
38
|
+
const validVerbs: AuditEventWrite["verb"][] = ["created", "deleted", "updated", "viewed"];
|
|
37
39
|
return (
|
|
38
40
|
typeof record.modelName === "string" &&
|
|
39
41
|
record.modelName.length > 0 &&
|
package/src/audit/record.test.ts
CHANGED
|
@@ -96,6 +96,21 @@ describe("audit record helpers", () => {
|
|
|
96
96
|
assert.equal(events[0]?.source, "admin");
|
|
97
97
|
});
|
|
98
98
|
|
|
99
|
+
it("maps the viewed verb to the read operation", async () => {
|
|
100
|
+
registerAuditApp();
|
|
101
|
+
const req = {user: {id: new mongoose.Types.ObjectId().toString()}} as express.Request;
|
|
102
|
+
await maybeRecordAdminAudit({
|
|
103
|
+
after: {_id: "food-1", name: "Soup"},
|
|
104
|
+
modelName: "Food",
|
|
105
|
+
req,
|
|
106
|
+
verb: "viewed",
|
|
107
|
+
});
|
|
108
|
+
const events = await mongoose.connection.collection("auditevents").find({}).toArray();
|
|
109
|
+
assert.equal(events.length, 1);
|
|
110
|
+
assert.equal(events[0]?.operation, "read");
|
|
111
|
+
assert.equal(events[0]?.verb, "viewed");
|
|
112
|
+
});
|
|
113
|
+
|
|
99
114
|
it("omits hidden fields from admin diffs", async () => {
|
|
100
115
|
registerAuditApp();
|
|
101
116
|
await maybeRecordAdminAudit({
|
package/src/audit/record.ts
CHANGED
|
@@ -3,7 +3,12 @@ import mongoose from "mongoose";
|
|
|
3
3
|
|
|
4
4
|
import {logger} from "../logger";
|
|
5
5
|
import {getOrgContext, ORGANIZATION_ID_HEADER} from "../orgs/orgContext";
|
|
6
|
-
import type {
|
|
6
|
+
import type {
|
|
7
|
+
AuditEventModel,
|
|
8
|
+
AuditEventOperation,
|
|
9
|
+
AuditEventSource,
|
|
10
|
+
AuditEventVerb,
|
|
11
|
+
} from "./auditEventModel";
|
|
7
12
|
import {changedFieldDiff, recordLabelFromDoc, toAuditPlain} from "./diff";
|
|
8
13
|
|
|
9
14
|
export interface ModelRouterAuditOptions {
|
|
@@ -28,7 +33,7 @@ export interface AuditEventWrite {
|
|
|
28
33
|
organizationId?: string;
|
|
29
34
|
recordId?: string;
|
|
30
35
|
recordLabel?: string;
|
|
31
|
-
source:
|
|
36
|
+
source: AuditEventSource;
|
|
32
37
|
verb: AuditEventVerb;
|
|
33
38
|
}
|
|
34
39
|
|
|
@@ -69,6 +74,9 @@ const operationFromVerb = (verb: AuditEventVerb): AuditEventOperation => {
|
|
|
69
74
|
if (verb === "deleted") {
|
|
70
75
|
return "delete";
|
|
71
76
|
}
|
|
77
|
+
if (verb === "viewed") {
|
|
78
|
+
return "read";
|
|
79
|
+
}
|
|
72
80
|
return "update";
|
|
73
81
|
};
|
|
74
82
|
|
package/src/auth.test.ts
CHANGED
|
@@ -966,7 +966,7 @@ describe("addMeRoutes edge cases", () => {
|
|
|
966
966
|
it("PATCH /auth/me returns 404 when user is deleted after auth", async () => {
|
|
967
967
|
const [_admin, notAdmin] = await setupDb();
|
|
968
968
|
const jwtLib = (await import("jsonwebtoken")).default;
|
|
969
|
-
const notAdminId =
|
|
969
|
+
const notAdminId = notAdmin._id;
|
|
970
970
|
const token = jwtLib.sign({id: notAdminId.toString()}, process.env.TOKEN_SECRET as string, {
|
|
971
971
|
issuer: process.env.TOKEN_ISSUER,
|
|
972
972
|
});
|
|
@@ -1172,7 +1172,7 @@ describe("privileged user fields", () => {
|
|
|
1172
1172
|
it("does not let PATCH /auth/me escalate to admin or join organizations", async () => {
|
|
1173
1173
|
const [_admin, notAdmin] = await setupDb();
|
|
1174
1174
|
const jwtLib = (await import("jsonwebtoken")).default;
|
|
1175
|
-
const notAdminId =
|
|
1175
|
+
const notAdminId = notAdmin._id;
|
|
1176
1176
|
const token = jwtLib.sign({id: notAdminId.toString()}, process.env.TOKEN_SECRET as string, {
|
|
1177
1177
|
issuer: process.env.TOKEN_ISSUER,
|
|
1178
1178
|
});
|
|
@@ -1202,7 +1202,7 @@ describe("privileged user fields", () => {
|
|
|
1202
1202
|
it("clears email verification when PATCH /auth/me changes the mailbox", async () => {
|
|
1203
1203
|
const [_admin, notAdmin] = await setupDb();
|
|
1204
1204
|
const jwtLib = (await import("jsonwebtoken")).default;
|
|
1205
|
-
const notAdminId =
|
|
1205
|
+
const notAdminId = notAdmin._id;
|
|
1206
1206
|
const token = jwtLib.sign({id: notAdminId.toString()}, process.env.TOKEN_SECRET as string, {
|
|
1207
1207
|
issuer: process.env.TOKEN_ISSUER,
|
|
1208
1208
|
});
|
|
@@ -1221,7 +1221,7 @@ describe("privileged user fields", () => {
|
|
|
1221
1221
|
it("keeps email verification for a casing-only email update", async () => {
|
|
1222
1222
|
const [_admin, notAdmin] = await setupDb();
|
|
1223
1223
|
const jwtLib = (await import("jsonwebtoken")).default;
|
|
1224
|
-
const notAdminId =
|
|
1224
|
+
const notAdminId = notAdmin._id;
|
|
1225
1225
|
const token = jwtLib.sign({id: notAdminId.toString()}, process.env.TOKEN_SECRET as string, {
|
|
1226
1226
|
issuer: process.env.TOKEN_ISSUER,
|
|
1227
1227
|
});
|
|
@@ -1452,8 +1452,22 @@ describe("PATCH /auth/me email change step-up", () => {
|
|
|
1452
1452
|
});
|
|
1453
1453
|
});
|
|
1454
1454
|
|
|
1455
|
+
interface IdentityUser {
|
|
1456
|
+
admin: boolean;
|
|
1457
|
+
betterAuthId?: string;
|
|
1458
|
+
email?: string;
|
|
1459
|
+
githubId?: string;
|
|
1460
|
+
githubUsername?: string;
|
|
1461
|
+
/** passport-local-mongoose credential fields, selected explicitly with `+hash +salt`. */
|
|
1462
|
+
hash?: string;
|
|
1463
|
+
name?: string;
|
|
1464
|
+
oauthProvider?: string;
|
|
1465
|
+
salt?: string;
|
|
1466
|
+
tokenEpoch: number;
|
|
1467
|
+
}
|
|
1468
|
+
|
|
1455
1469
|
// A user schema that declares the identity-binding fields Better Auth and GitHub linking add.
|
|
1456
|
-
const identityUserSchema = new Schema({
|
|
1470
|
+
const identityUserSchema = new Schema<IdentityUser>({
|
|
1457
1471
|
admin: {default: false, description: "Admin flag", type: Boolean},
|
|
1458
1472
|
betterAuthId: {description: "Better Auth user id", type: String},
|
|
1459
1473
|
email: {description: "Email", type: String},
|
|
@@ -1467,8 +1481,8 @@ identityUserSchema.plugin(
|
|
|
1467
1481
|
);
|
|
1468
1482
|
identityUserSchema.plugin(githubUserPlugin);
|
|
1469
1483
|
const IdentityUserModel =
|
|
1470
|
-
(mongoose.models.AuthIdentityUser as mongoose.Model<
|
|
1471
|
-
mongoose.model("AuthIdentityUser", identityUserSchema);
|
|
1484
|
+
(mongoose.models.AuthIdentityUser as mongoose.Model<IdentityUser> | undefined) ??
|
|
1485
|
+
mongoose.model<IdentityUser>("AuthIdentityUser", identityUserSchema);
|
|
1472
1486
|
|
|
1473
1487
|
describe("self-service identity and credential fields", () => {
|
|
1474
1488
|
const identityFields = {
|