@terreno/api 57.2.0 → 57.3.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/README.md +17 -7
- package/dist/actions.js +19 -7
- package/dist/api.d.ts +4 -0
- package/dist/api.js +12 -24
- package/dist/auth.d.ts +23 -10
- package/dist/auth.js +97 -32
- package/dist/authRecovery.d.ts +5 -0
- package/dist/authRecovery.js +350 -0
- package/dist/authTokens.d.ts +8 -0
- package/dist/authTokens.js +162 -0
- package/dist/betterAuth.d.ts +33 -0
- package/dist/betterAuthApp.d.ts +4 -0
- package/dist/betterAuthApp.js +26 -8
- package/dist/betterAuthSetup.d.ts +44 -2
- package/dist/betterAuthSetup.js +288 -18
- package/dist/collectionRegistry.d.ts +38 -0
- package/dist/collectionRegistry.js +72 -0
- package/dist/configurationApp.d.ts +11 -2
- package/dist/configurationApp.js +2 -4
- package/dist/docLoader.js +35 -6
- package/dist/expressServer.d.ts +26 -3
- package/dist/expressServer.js +79 -11
- package/dist/index.d.ts +16 -0
- package/dist/index.js +38 -2
- package/dist/mcp/auth.d.ts +1 -0
- package/dist/mcp/auth.js +47 -25
- package/dist/mcp/handlers.d.ts +1 -1
- package/dist/mcp/handlers.js +67 -233
- package/dist/mcp/query.d.ts +2 -2
- package/dist/mcp/registry.d.ts +1 -5
- package/dist/mcp/registry.js +31 -20
- package/dist/mcp/schemaGenerator.d.ts +2 -7
- package/dist/mcp/schemaGenerator.js +41 -116
- package/dist/mcp/server.d.ts +1 -0
- package/dist/mcp/server.js +1 -0
- package/dist/mcp/serviceTokens.d.ts +16 -0
- package/dist/mcp/serviceTokens.js +345 -0
- package/dist/mcp/types.d.ts +2 -2
- package/dist/models/mcpServiceToken.d.ts +3 -0
- package/dist/models/mcpServiceToken.js +208 -0
- package/dist/openApiBuilder.d.ts +5 -4
- package/dist/openApiBuilder.js +61 -8
- package/dist/openApiCompat.js +1 -1
- package/dist/openApiValidator.js +0 -2
- package/dist/plugins.d.ts +14 -1
- package/dist/plugins.js +17 -6
- package/dist/populate.d.ts +2 -2
- package/dist/populate.js +47 -42
- package/dist/rateLimit/applyTrustProxy.d.ts +3 -0
- package/dist/rateLimit/applyTrustProxy.js +17 -0
- package/dist/rateLimit/createStore.d.ts +2 -0
- package/dist/rateLimit/createStore.js +28 -0
- package/dist/rateLimit/memoryStore.d.ts +2 -0
- package/dist/rateLimit/memoryStore.js +108 -0
- package/dist/rateLimit/middleware.d.ts +11 -0
- package/dist/rateLimit/middleware.js +110 -0
- package/dist/rateLimit/mongoStore.d.ts +3 -0
- package/dist/rateLimit/mongoStore.js +122 -0
- package/dist/rateLimit/policies.d.ts +14 -0
- package/dist/rateLimit/policies.js +151 -0
- package/dist/rateLimit/redisStore.d.ts +3 -0
- package/dist/rateLimit/redisStore.js +155 -0
- package/dist/rateLimit/types.d.ts +54 -0
- package/dist/rateLimit/types.js +9 -0
- package/dist/rbac/access.js +5 -1
- package/dist/rbac/assertAllowed.d.ts +9 -0
- package/dist/rbac/assertAllowed.js +182 -0
- package/dist/rbac/modelRouterAccess.d.ts +1 -0
- package/dist/rbac/modelRouterAccess.js +8 -4
- package/dist/rbac/permissionUtils.js +3 -2
- package/dist/rbac/resolve.js +5 -4
- package/dist/rbac/routes.js +15 -6
- package/dist/realtime/deprecation.d.ts +12 -0
- package/dist/realtime/deprecation.js +32 -0
- package/dist/realtime/realtimeApp.d.ts +1 -1
- package/dist/realtime/realtimeApp.js +10 -11
- package/dist/realtime/registry.d.ts +0 -31
- package/dist/realtime/registry.js +44 -31
- package/dist/realtime/socketAuth.d.ts +5 -5
- package/dist/realtime/socketAuth.js +11 -1
- package/dist/realtime/socketUser.d.ts +5 -1
- package/dist/realtime/types.d.ts +14 -4
- package/dist/schemaMetadata.d.ts +56 -0
- package/dist/schemaMetadata.js +513 -0
- package/dist/seedRunner.d.ts +88 -0
- package/dist/seedRunner.js +627 -0
- package/dist/sync/executors.d.ts +12 -2
- package/dist/sync/executors.js +8 -10
- package/dist/sync/loadHarness.js +28 -19
- package/dist/sync/models.js +26 -24
- package/dist/sync/mutationHandler.d.ts +9 -0
- package/dist/sync/mutationHandler.js +11 -6
- package/dist/sync/registrationSideEffects.d.ts +35 -0
- package/dist/sync/registrationSideEffects.js +151 -0
- package/dist/sync/registry.d.ts +2 -52
- package/dist/sync/registry.js +38 -187
- package/dist/sync/routes.js +3 -2
- package/dist/sync/scripts/compactTombstones.js +0 -2
- package/dist/sync/syncApp.d.ts +3 -3
- package/dist/sync/syncApp.js +4 -4
- package/dist/sync/syncBatch.js +3 -2
- package/dist/sync/syncSeqPlugin.d.ts +1 -1
- package/dist/sync/syncSeqPlugin.js +20 -16
- package/dist/terrenoApp.d.ts +19 -5
- package/dist/terrenoApp.js +66 -18
- package/dist/tests/models.d.ts +2 -0
- package/dist/tests/models.js +6 -0
- package/dist/types/authToken.d.ts +33 -0
- package/dist/types/authToken.js +2 -0
- package/dist/types/mcpServiceToken.d.ts +41 -0
- package/dist/types/mcpServiceToken.js +2 -0
- package/dist/webhooks/headerValue.d.ts +2 -0
- package/dist/webhooks/headerValue.js +14 -0
- package/dist/webhooks/idempotency/memoryStore.d.ts +10 -0
- package/dist/webhooks/idempotency/memoryStore.js +65 -0
- package/dist/webhooks/idempotency/mongoStore.d.ts +4 -0
- package/dist/webhooks/idempotency/mongoStore.js +150 -0
- package/dist/webhooks/idempotency/webhookReceipt.d.ts +62 -0
- package/dist/webhooks/idempotency/webhookReceipt.js +39 -0
- package/dist/webhooks/rawBody.d.ts +14 -0
- package/dist/webhooks/rawBody.js +22 -0
- package/dist/webhooks/timingSafeEqual.d.ts +4 -0
- package/dist/webhooks/timingSafeEqual.js +17 -0
- package/dist/webhooks/verifiers/hmac.d.ts +15 -0
- package/dist/webhooks/verifiers/hmac.js +49 -0
- package/dist/webhooks/verifiers/sendgrid.d.ts +10 -0
- package/dist/webhooks/verifiers/sendgrid.js +56 -0
- package/dist/webhooks/verifiers/stripe.d.ts +9 -0
- package/dist/webhooks/verifiers/stripe.js +99 -0
- package/dist/webhooks/verifiers/twilio.d.ts +9 -0
- package/dist/webhooks/verifiers/twilio.js +112 -0
- package/dist/webhooks/webhooksApp.d.ts +33 -0
- package/dist/webhooks/webhooksApp.js +195 -0
- package/package.json +4 -5
- package/src/__snapshots__/openApi.test.ts.snap +270 -0
- package/src/__snapshots__/openApiBuilder.test.ts.snap +30 -12
- package/src/__snapshots__/schemaMetadata.test.ts.snap +197 -0
- package/src/actions.ts +20 -13
- package/src/adminModelRouter.test.ts +133 -0
- package/src/api.arrayOperations.test.ts +43 -31
- package/src/api.hooks.test.ts +38 -29
- package/src/api.query.test.ts +31 -31
- package/src/api.test.ts +95 -96
- package/src/api.ts +16 -27
- package/src/auth.test.ts +107 -51
- package/src/auth.ts +98 -29
- package/src/authRecovery.test.ts +281 -0
- package/src/authRecovery.ts +279 -0
- package/src/authTokens.test.ts +94 -0
- package/src/authTokens.ts +118 -0
- package/src/authVerification.test.ts +293 -0
- package/src/betterAuth.ts +24 -0
- package/src/betterAuthApp.test.ts +1 -0
- package/src/betterAuthApp.ts +27 -9
- package/src/betterAuthSetup.test.ts +568 -38
- package/src/betterAuthSetup.ts +260 -5
- package/src/collectionRegistry.test.ts +165 -0
- package/src/collectionRegistry.ts +115 -0
- package/src/configurationApp.ts +13 -7
- package/src/docLoader.test.ts +45 -1
- package/src/docLoader.ts +37 -6
- package/src/express.d.ts +1 -0
- package/src/expressServer.test.ts +130 -66
- package/src/expressServer.ts +75 -19
- package/src/githubAuth.test.ts +60 -55
- package/src/index.ts +58 -0
- package/src/mcp/auth.test.ts +47 -0
- package/src/mcp/auth.ts +32 -11
- package/src/mcp/handlers.ts +45 -172
- package/src/mcp/integration.test.ts +59 -9
- package/src/mcp/query.ts +2 -4
- package/src/mcp/registry.ts +29 -20
- package/src/mcp/schemaGenerator.test.ts +8 -0
- package/src/mcp/schemaGenerator.ts +49 -131
- package/src/mcp/server.ts +2 -0
- package/src/mcp/serviceTokens.test.ts +330 -0
- package/src/mcp/serviceTokens.ts +307 -0
- package/src/mcp/toolGenerator.test.ts +1 -1
- package/src/mcp/types.ts +2 -6
- package/src/models/mcpServiceToken.test.ts +129 -0
- package/src/models/mcpServiceToken.ts +158 -0
- package/src/openApi.test.ts +80 -64
- package/src/openApiBuilder.test.ts +95 -7
- package/src/openApiBuilder.ts +46 -9
- package/src/openApiCompat.ts +1 -1
- package/src/openApiValidator.test.ts +49 -54
- package/src/openApiValidator.ts +1 -3
- package/src/plugins.test.ts +14 -0
- package/src/plugins.ts +34 -9
- package/src/populate.test.ts +34 -0
- package/src/populate.ts +40 -29
- package/src/rateLimit/applyTrustProxy.ts +17 -0
- package/src/rateLimit/createStore.ts +25 -0
- package/src/rateLimit/memoryStore.ts +40 -0
- package/src/rateLimit/middleware.ts +79 -0
- package/src/rateLimit/mongoStore.test.ts +80 -0
- package/src/rateLimit/mongoStore.ts +76 -0
- package/src/rateLimit/policies.ts +130 -0
- package/src/rateLimit/rateLimit.test.ts +328 -0
- package/src/rateLimit/redisStore.ioredis.test.ts +29 -0
- package/src/rateLimit/redisStore.test.ts +114 -0
- package/src/rateLimit/redisStore.ts +85 -0
- package/src/rateLimit/types.ts +64 -0
- package/src/rbac/access.ts +5 -1
- package/src/rbac/assertAllowed.test.ts +325 -0
- package/src/rbac/assertAllowed.ts +157 -0
- package/src/rbac/backfillAdmins.test.ts +124 -0
- package/src/rbac/fieldViews.test.ts +89 -0
- package/src/rbac/modelRouterAccess.test.ts +126 -0
- package/src/rbac/modelRouterAccess.ts +5 -2
- package/src/rbac/permissionUtils.ts +3 -2
- package/src/rbac/realtimeAccess.test.ts +109 -1
- package/src/rbac/resolve.ts +5 -4
- package/src/rbac/roleManager.test.ts +169 -0
- package/src/rbac/routes.test.ts +233 -0
- package/src/rbac/routes.ts +16 -6
- package/src/realtime/deprecation.test.ts +60 -0
- package/src/realtime/deprecation.ts +32 -0
- package/src/realtime/realtime.test.ts +8 -4
- package/src/realtime/realtimeApp.ts +10 -11
- package/src/realtime/realtimeAppSetup.test.ts +32 -5
- package/src/realtime/registry.ts +48 -42
- package/src/realtime/sessionRevalidation.test.ts +57 -44
- package/src/realtime/socketAuth.test.ts +75 -23
- package/src/realtime/socketAuth.ts +17 -6
- package/src/realtime/socketUser.ts +5 -3
- package/src/realtime/types.ts +14 -4
- package/src/schemaMetadata.test.ts +405 -0
- package/src/schemaMetadata.ts +593 -0
- package/src/scrubAdminFields.test.ts +105 -0
- package/src/seedRunner.test.ts +591 -0
- package/src/seedRunner.ts +464 -0
- package/src/sync/docMatchesMutationData.test.ts +16 -0
- package/src/sync/ensureSyncIndexes.test.ts +54 -15
- package/src/sync/executors.test.ts +40 -0
- package/src/sync/executors.ts +24 -12
- package/src/sync/integration.test.ts +15 -7
- package/src/sync/loadHarness.ts +28 -19
- package/src/sync/models.ts +32 -18
- package/src/sync/mutationHandler.test.ts +16 -16
- package/src/sync/mutationHandler.ts +9 -7
- package/src/sync/registrationSideEffects.test.ts +126 -0
- package/src/sync/registrationSideEffects.ts +131 -0
- package/src/sync/registry.ts +45 -171
- package/src/sync/routes.ts +3 -2
- package/src/sync/scripts/compactTombstones.ts +0 -2
- package/src/sync/sessionRevalidationIntegration.test.ts +12 -11
- package/src/sync/sync.test.ts +25 -23
- package/src/sync/syncApp.ts +4 -4
- package/src/sync/syncBatch.ts +3 -2
- package/src/sync/syncFrontier.test.ts +17 -15
- package/src/sync/syncPhaseC.test.ts +54 -43
- package/src/sync/syncRoutes.test.ts +51 -36
- package/src/sync/syncSeqPlugin.ts +66 -44
- package/src/sync/syncSocket.test.ts +112 -81
- package/src/terrenoApp.mcpServiceTokens.test.ts +173 -0
- package/src/terrenoApp.ts +87 -15
- package/src/tests/models.ts +14 -1
- package/src/types/authToken.ts +43 -0
- package/src/types/mcpServiceToken.ts +54 -0
- package/src/webhooks/headerValue.test.ts +22 -0
- package/src/webhooks/headerValue.ts +12 -0
- package/src/webhooks/idempotency/memoryStore.ts +34 -0
- package/src/webhooks/idempotency/mongoStore.test.ts +274 -0
- package/src/webhooks/idempotency/mongoStore.ts +83 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +70 -0
- package/src/webhooks/rawBody.test.ts +74 -0
- package/src/webhooks/rawBody.ts +23 -0
- package/src/webhooks/timingSafeEqual.ts +16 -0
- package/src/webhooks/verifiers/hmac.test.ts +110 -0
- package/src/webhooks/verifiers/hmac.ts +54 -0
- package/src/webhooks/verifiers/providerVerifiers.test.ts +388 -0
- package/src/webhooks/verifiers/sendgrid.ts +59 -0
- package/src/webhooks/verifiers/stripe.ts +67 -0
- package/src/webhooks/verifiers/twilio.ts +69 -0
- package/src/webhooks/webhooksApp.test.ts +221 -0
- package/src/webhooks/webhooksApp.ts +144 -0
package/src/githubAuth.test.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
// noExplicitAny: test mock typing
|
|
2
|
-
// biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
|
|
3
1
|
import {afterEach, beforeEach, describe, expect, it, setSystemTime} from "bun:test";
|
|
4
2
|
import type express from "express";
|
|
5
|
-
import mongoose, {model, Schema} from "mongoose";
|
|
3
|
+
import mongoose, {type Model, model, Schema} from "mongoose";
|
|
6
4
|
import passport from "passport";
|
|
7
5
|
import passportLocalMongoose from "passport-local-mongoose";
|
|
8
6
|
import supertest from "supertest";
|
|
@@ -72,22 +70,26 @@ const testUserSchema = new Schema<TestUser>({
|
|
|
72
70
|
username: {description: "The user's username", type: String},
|
|
73
71
|
});
|
|
74
72
|
|
|
75
|
-
testUserSchema.plugin(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
73
|
+
testUserSchema.plugin(
|
|
74
|
+
passportLocalMongoose as unknown as (schema: Schema, options?: Record<string, unknown>) => void,
|
|
75
|
+
{
|
|
76
|
+
attemptsField: "attempts",
|
|
77
|
+
interval: 1,
|
|
78
|
+
limitAttempts: true,
|
|
79
|
+
maxAttempts: 3,
|
|
80
|
+
maxInterval: 1,
|
|
81
|
+
usernameCaseInsensitive: true,
|
|
82
|
+
usernameField: "email",
|
|
83
|
+
}
|
|
84
|
+
);
|
|
84
85
|
testUserSchema.plugin(createdUpdatedPlugin);
|
|
85
86
|
testUserSchema.plugin(isDisabledPlugin);
|
|
86
87
|
testUserSchema.plugin(githubUserPlugin);
|
|
87
88
|
|
|
88
89
|
// Get or create model to avoid model redefinition errors
|
|
89
90
|
const GitHubTestUserModel =
|
|
90
|
-
mongoose.models.GitHubTestUser
|
|
91
|
+
(mongoose.models.GitHubTestUser as Model<TestUser> | undefined) ??
|
|
92
|
+
model<TestUser>("GitHubTestUser", testUserSchema);
|
|
91
93
|
|
|
92
94
|
// Connect to database before tests
|
|
93
95
|
const connectDb = async () => {
|
|
@@ -114,9 +116,9 @@ describe("githubUserPlugin", () => {
|
|
|
114
116
|
|
|
115
117
|
it("githubId is indexed and sparse", () => {
|
|
116
118
|
const githubIdPath = testUserSchema.path("githubId");
|
|
117
|
-
expect(
|
|
118
|
-
expect(
|
|
119
|
-
expect(
|
|
119
|
+
expect(githubIdPath.options.index).toBe(true);
|
|
120
|
+
expect(githubIdPath.options.sparse).toBe(true);
|
|
121
|
+
expect(githubIdPath.options.unique).toBe(true);
|
|
120
122
|
});
|
|
121
123
|
});
|
|
122
124
|
|
|
@@ -136,7 +138,7 @@ describe("GitHub auth routes", () => {
|
|
|
136
138
|
email: "test@example.com",
|
|
137
139
|
name: "Test User",
|
|
138
140
|
});
|
|
139
|
-
await (testUser as
|
|
141
|
+
await (testUser as unknown as PasswordedDocument).setPassword("password123");
|
|
140
142
|
await testUser.save();
|
|
141
143
|
|
|
142
144
|
function addRoutes(router: express.Router): void {
|
|
@@ -152,7 +154,7 @@ describe("GitHub auth routes", () => {
|
|
|
152
154
|
clientSecret: "test-client-secret",
|
|
153
155
|
},
|
|
154
156
|
skipListen: true,
|
|
155
|
-
userModel: GitHubTestUserModel as
|
|
157
|
+
userModel: GitHubTestUserModel as unknown as UserModel,
|
|
156
158
|
}).build();
|
|
157
159
|
agent = supertest.agent(app);
|
|
158
160
|
});
|
|
@@ -187,8 +189,8 @@ describe("GitHub auth routes", () => {
|
|
|
187
189
|
// Link github to this user
|
|
188
190
|
const user = await GitHubTestUserModel.findOne({email: "test@example.com"});
|
|
189
191
|
if (user) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
+
user.githubId = "99999";
|
|
193
|
+
user.githubUsername = "testghuser";
|
|
192
194
|
await user.save();
|
|
193
195
|
}
|
|
194
196
|
|
|
@@ -202,8 +204,8 @@ describe("GitHub auth routes", () => {
|
|
|
202
204
|
|
|
203
205
|
// Verify github fields are cleared
|
|
204
206
|
const updatedUser = await GitHubTestUserModel.findOne({email: "test@example.com"});
|
|
205
|
-
expect(
|
|
206
|
-
expect(
|
|
207
|
+
expect(updatedUser?.githubId).toBeUndefined();
|
|
208
|
+
expect(updatedUser?.githubUsername).toBeUndefined();
|
|
207
209
|
});
|
|
208
210
|
|
|
209
211
|
it("user can have both password and GitHub auth", async () => {
|
|
@@ -214,8 +216,8 @@ describe("GitHub auth routes", () => {
|
|
|
214
216
|
}
|
|
215
217
|
|
|
216
218
|
// Link GitHub
|
|
217
|
-
|
|
218
|
-
|
|
219
|
+
user.githubId = "88888";
|
|
220
|
+
user.githubUsername = "linkeduser";
|
|
219
221
|
await user.save();
|
|
220
222
|
|
|
221
223
|
// Can still login with password
|
|
@@ -230,8 +232,8 @@ describe("GitHub auth routes", () => {
|
|
|
230
232
|
// and we verify GitHub fields are set
|
|
231
233
|
const updatedUser = await GitHubTestUserModel.findOne({email: "test@example.com"});
|
|
232
234
|
expect(updatedUser).toBeDefined();
|
|
233
|
-
expect(
|
|
234
|
-
expect(
|
|
235
|
+
expect(updatedUser?.githubId).toBe("88888");
|
|
236
|
+
expect(updatedUser?.githubUsername).toBe("linkeduser");
|
|
235
237
|
});
|
|
236
238
|
});
|
|
237
239
|
|
|
@@ -253,7 +255,7 @@ describe("GitHub auth disabled", () => {
|
|
|
253
255
|
app = new TerrenoApp({
|
|
254
256
|
configureApp: addRoutes,
|
|
255
257
|
skipListen: true,
|
|
256
|
-
userModel: GitHubTestUserModel as
|
|
258
|
+
userModel: GitHubTestUserModel as unknown as UserModel,
|
|
257
259
|
}).build();
|
|
258
260
|
agent = supertest.agent(app);
|
|
259
261
|
});
|
|
@@ -282,7 +284,10 @@ interface VerifyError {
|
|
|
282
284
|
message?: string;
|
|
283
285
|
}
|
|
284
286
|
|
|
285
|
-
type VerifiedUser =
|
|
287
|
+
type VerifiedUser = {
|
|
288
|
+
_id: mongoose.Types.ObjectId | string;
|
|
289
|
+
email?: string;
|
|
290
|
+
} & Partial<GitHubUserFields>;
|
|
286
291
|
|
|
287
292
|
interface VerifyStrategy {
|
|
288
293
|
_verify: (
|
|
@@ -316,7 +321,7 @@ const invokeGitHubVerify = (
|
|
|
316
321
|
};
|
|
317
322
|
|
|
318
323
|
describe("GitHub strategy verify callback", () => {
|
|
319
|
-
const testApp = {get: () => {}, use: () => {}} as
|
|
324
|
+
const testApp = {get: () => {}, use: () => {}} as unknown as express.Application;
|
|
320
325
|
|
|
321
326
|
beforeEach(async () => {
|
|
322
327
|
await connectDb();
|
|
@@ -325,7 +330,7 @@ describe("GitHub strategy verify callback", () => {
|
|
|
325
330
|
|
|
326
331
|
it("uses custom findOrCreateUser when provided", async () => {
|
|
327
332
|
const customUser = {_id: "custom-user-id", email: "custom@example.com"};
|
|
328
|
-
setupGitHubAuth(testApp, GitHubTestUserModel as
|
|
333
|
+
setupGitHubAuth(testApp, GitHubTestUserModel as unknown as UserModel, {
|
|
329
334
|
callbackURL: "http://localhost:9000/auth/github/callback",
|
|
330
335
|
clientId: "id",
|
|
331
336
|
clientSecret: "secret",
|
|
@@ -338,7 +343,7 @@ describe("GitHub strategy verify callback", () => {
|
|
|
338
343
|
});
|
|
339
344
|
|
|
340
345
|
it("creates a new user when no existing GitHub or email user", async () => {
|
|
341
|
-
setupGitHubAuth(testApp, GitHubTestUserModel as
|
|
346
|
+
setupGitHubAuth(testApp, GitHubTestUserModel as unknown as UserModel, {
|
|
342
347
|
callbackURL: "http://localhost:9000/auth/github/callback",
|
|
343
348
|
clientId: "id",
|
|
344
349
|
clientSecret: "secret",
|
|
@@ -366,9 +371,9 @@ describe("GitHub strategy verify callback", () => {
|
|
|
366
371
|
githubId: "gh-existing-1",
|
|
367
372
|
githubUsername: "ghuser",
|
|
368
373
|
name: "GH User",
|
|
369
|
-
}
|
|
374
|
+
});
|
|
370
375
|
|
|
371
|
-
setupGitHubAuth(testApp, GitHubTestUserModel as
|
|
376
|
+
setupGitHubAuth(testApp, GitHubTestUserModel as unknown as UserModel, {
|
|
372
377
|
callbackURL: "http://localhost:9000/auth/github/callback",
|
|
373
378
|
clientId: "id",
|
|
374
379
|
clientSecret: "secret",
|
|
@@ -379,16 +384,16 @@ describe("GitHub strategy verify callback", () => {
|
|
|
379
384
|
username: "ghuser",
|
|
380
385
|
});
|
|
381
386
|
expect(result.err).toBeNull();
|
|
382
|
-
expect(result.user._id.toString()).toBe(
|
|
387
|
+
expect(result.user._id.toString()).toBe(existingUser._id.toString());
|
|
383
388
|
});
|
|
384
389
|
|
|
385
390
|
it("links GitHub to authenticated user when allowAccountLinking=true", async () => {
|
|
386
391
|
const existingUser = await GitHubTestUserModel.create({
|
|
387
392
|
email: "link@example.com",
|
|
388
393
|
name: "Link User",
|
|
389
|
-
}
|
|
394
|
+
});
|
|
390
395
|
|
|
391
|
-
setupGitHubAuth(testApp, GitHubTestUserModel as
|
|
396
|
+
setupGitHubAuth(testApp, GitHubTestUserModel as unknown as UserModel, {
|
|
392
397
|
allowAccountLinking: true,
|
|
393
398
|
callbackURL: "http://localhost:9000/auth/github/callback",
|
|
394
399
|
clientId: "id",
|
|
@@ -410,9 +415,9 @@ describe("GitHub strategy verify callback", () => {
|
|
|
410
415
|
it("rejects linking when allowAccountLinking=false", async () => {
|
|
411
416
|
const existingUser = await GitHubTestUserModel.create({
|
|
412
417
|
email: "nolink@example.com",
|
|
413
|
-
}
|
|
418
|
+
});
|
|
414
419
|
|
|
415
|
-
setupGitHubAuth(testApp, GitHubTestUserModel as
|
|
420
|
+
setupGitHubAuth(testApp, GitHubTestUserModel as unknown as UserModel, {
|
|
416
421
|
allowAccountLinking: false,
|
|
417
422
|
callbackURL: "http://localhost:9000/auth/github/callback",
|
|
418
423
|
clientId: "id",
|
|
@@ -422,19 +427,19 @@ describe("GitHub strategy verify callback", () => {
|
|
|
422
427
|
const req = {user: existingUser};
|
|
423
428
|
const result = await invokeGitHubVerify(req, "access", "refresh", {id: "gh-nolink-1"});
|
|
424
429
|
expect(result.err).toBeDefined();
|
|
425
|
-
expect(
|
|
430
|
+
expect(result.err?.status).toBe(400);
|
|
426
431
|
});
|
|
427
432
|
|
|
428
433
|
it("rejects linking when GitHub account belongs to another user", async () => {
|
|
429
434
|
const userA = await GitHubTestUserModel.create({
|
|
430
435
|
email: "a@example.com",
|
|
431
|
-
}
|
|
436
|
+
});
|
|
432
437
|
await GitHubTestUserModel.create({
|
|
433
438
|
email: "b@example.com",
|
|
434
439
|
githubId: "gh-other-1",
|
|
435
|
-
}
|
|
440
|
+
});
|
|
436
441
|
|
|
437
|
-
setupGitHubAuth(testApp, GitHubTestUserModel as
|
|
442
|
+
setupGitHubAuth(testApp, GitHubTestUserModel as unknown as UserModel, {
|
|
438
443
|
allowAccountLinking: true,
|
|
439
444
|
callbackURL: "http://localhost:9000/auth/github/callback",
|
|
440
445
|
clientId: "id",
|
|
@@ -444,15 +449,15 @@ describe("GitHub strategy verify callback", () => {
|
|
|
444
449
|
const req = {user: userA};
|
|
445
450
|
const result = await invokeGitHubVerify(req, "access", "refresh", {id: "gh-other-1"});
|
|
446
451
|
expect(result.err).toBeDefined();
|
|
447
|
-
expect(
|
|
452
|
+
expect(result.err?.status).toBe(400);
|
|
448
453
|
});
|
|
449
454
|
|
|
450
455
|
it("links GitHub to existing email user when allowAccountLinking is not false", async () => {
|
|
451
456
|
await GitHubTestUserModel.create({
|
|
452
457
|
email: "emailuser@example.com",
|
|
453
|
-
}
|
|
458
|
+
});
|
|
454
459
|
|
|
455
|
-
setupGitHubAuth(testApp, GitHubTestUserModel as
|
|
460
|
+
setupGitHubAuth(testApp, GitHubTestUserModel as unknown as UserModel, {
|
|
456
461
|
callbackURL: "http://localhost:9000/auth/github/callback",
|
|
457
462
|
clientId: "id",
|
|
458
463
|
clientSecret: "secret",
|
|
@@ -471,9 +476,9 @@ describe("GitHub strategy verify callback", () => {
|
|
|
471
476
|
it("rejects email-link when allowAccountLinking=false", async () => {
|
|
472
477
|
await GitHubTestUserModel.create({
|
|
473
478
|
email: "emailnolink@example.com",
|
|
474
|
-
}
|
|
479
|
+
});
|
|
475
480
|
|
|
476
|
-
setupGitHubAuth(testApp, GitHubTestUserModel as
|
|
481
|
+
setupGitHubAuth(testApp, GitHubTestUserModel as unknown as UserModel, {
|
|
477
482
|
allowAccountLinking: false,
|
|
478
483
|
callbackURL: "http://localhost:9000/auth/github/callback",
|
|
479
484
|
clientId: "id",
|
|
@@ -485,12 +490,12 @@ describe("GitHub strategy verify callback", () => {
|
|
|
485
490
|
id: "gh-email-nolink-1",
|
|
486
491
|
});
|
|
487
492
|
expect(result.err).toBeDefined();
|
|
488
|
-
expect(
|
|
493
|
+
expect(result.err?.status).toBe(400);
|
|
489
494
|
});
|
|
490
495
|
|
|
491
496
|
it("returns error when thrown during lookup", async () => {
|
|
492
497
|
// Set up strategy with findOrCreateUser that throws
|
|
493
|
-
setupGitHubAuth(testApp, GitHubTestUserModel as
|
|
498
|
+
setupGitHubAuth(testApp, GitHubTestUserModel as unknown as UserModel, {
|
|
494
499
|
callbackURL: "http://localhost:9000/auth/github/callback",
|
|
495
500
|
clientId: "id",
|
|
496
501
|
clientSecret: "secret",
|
|
@@ -527,7 +532,7 @@ describe("addGitHubAuthRoutes link endpoints", () => {
|
|
|
527
532
|
clientSecret: "test-client-secret",
|
|
528
533
|
},
|
|
529
534
|
skipListen: true,
|
|
530
|
-
userModel: GitHubTestUserModel as
|
|
535
|
+
userModel: GitHubTestUserModel as unknown as UserModel,
|
|
531
536
|
}).build();
|
|
532
537
|
agent = supertest.agent(app);
|
|
533
538
|
});
|
|
@@ -553,8 +558,8 @@ describe("addGitHubAuthRoutes link endpoints", () => {
|
|
|
553
558
|
githubId: "77777",
|
|
554
559
|
githubProfileUrl: "http://profile",
|
|
555
560
|
githubUsername: "ghunlink",
|
|
556
|
-
}
|
|
557
|
-
await (user as
|
|
561
|
+
});
|
|
562
|
+
await (user as unknown as PasswordedDocument).setPassword("password123");
|
|
558
563
|
await user.save();
|
|
559
564
|
|
|
560
565
|
const loginRes = await agent
|
|
@@ -568,9 +573,9 @@ describe("addGitHubAuthRoutes link endpoints", () => {
|
|
|
568
573
|
.expect(200);
|
|
569
574
|
|
|
570
575
|
const updatedUser = await GitHubTestUserModel.findOne({email: "unlinkme@example.com"});
|
|
571
|
-
expect(
|
|
572
|
-
expect(
|
|
573
|
-
expect(
|
|
576
|
+
expect(updatedUser?.githubId).toBeUndefined();
|
|
577
|
+
expect(updatedUser?.githubAvatarUrl).toBeUndefined();
|
|
578
|
+
expect(updatedUser?.githubProfileUrl).toBeUndefined();
|
|
574
579
|
});
|
|
575
580
|
});
|
|
576
581
|
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,8 @@ export * from "./actions";
|
|
|
2
2
|
export * from "./adminTypes";
|
|
3
3
|
export * from "./api";
|
|
4
4
|
export * from "./auth";
|
|
5
|
+
export * from "./authRecovery";
|
|
6
|
+
export * from "./authTokens";
|
|
5
7
|
export * from "./betterAuth";
|
|
6
8
|
export * from "./betterAuthApp";
|
|
7
9
|
export * from "./betterAuthSetup";
|
|
@@ -26,6 +28,13 @@ export {
|
|
|
26
28
|
} from "./mcp/registry";
|
|
27
29
|
export {generateInputSchema, generateToolDescription} from "./mcp/schemaGenerator";
|
|
28
30
|
export {type MCPServerOptions, mountMCPServer} from "./mcp/server";
|
|
31
|
+
export {
|
|
32
|
+
addMcpServiceTokenRoutes,
|
|
33
|
+
MAX_ACTIVE_MCP_SERVICE_TOKENS,
|
|
34
|
+
type McpServiceTokenRoutesOptions,
|
|
35
|
+
type McpServiceTokensAppOption,
|
|
36
|
+
resolveMcpServiceTokensOption,
|
|
37
|
+
} from "./mcp/serviceTokens";
|
|
29
38
|
export {
|
|
30
39
|
generateAllTools,
|
|
31
40
|
generateToolsForEntry,
|
|
@@ -43,6 +52,7 @@ export type {
|
|
|
43
52
|
export * from "./middleware";
|
|
44
53
|
export * from "./models/consentForm";
|
|
45
54
|
export * from "./models/consentResponse";
|
|
55
|
+
export * from "./models/mcpServiceToken";
|
|
46
56
|
export * from "./models/versionConfig";
|
|
47
57
|
export * from "./notifiers/googleChatNotifier";
|
|
48
58
|
export * from "./notifiers/slackNotifier";
|
|
@@ -54,7 +64,19 @@ export * from "./openApiValidator";
|
|
|
54
64
|
export * from "./permissions";
|
|
55
65
|
export * from "./plugins";
|
|
56
66
|
export * from "./populate";
|
|
67
|
+
export type {
|
|
68
|
+
RateLimitLimits,
|
|
69
|
+
RateLimitOptions,
|
|
70
|
+
RateLimitRedisClient,
|
|
71
|
+
RateLimitStore,
|
|
72
|
+
} from "./rateLimit/types";
|
|
73
|
+
export {
|
|
74
|
+
DEFAULT_API_MAX,
|
|
75
|
+
DEFAULT_AUTH_MAX,
|
|
76
|
+
DEFAULT_WINDOW_MS,
|
|
77
|
+
} from "./rateLimit/types";
|
|
57
78
|
export * from "./rbac/access";
|
|
79
|
+
export {assertAllowed} from "./rbac/assertAllowed";
|
|
58
80
|
export {
|
|
59
81
|
createRbacAuditModel,
|
|
60
82
|
type RbacAuditDocument,
|
|
@@ -150,9 +172,26 @@ export type {
|
|
|
150
172
|
RealtimeEvent,
|
|
151
173
|
} from "./realtime/types";
|
|
152
174
|
export * from "./requestContext";
|
|
175
|
+
export {
|
|
176
|
+
type DescribeModelForRouterOptions,
|
|
177
|
+
type DescribeModelOptions,
|
|
178
|
+
describeModel,
|
|
179
|
+
describeModelForRouter,
|
|
180
|
+
type FieldDescription,
|
|
181
|
+
type FieldKind,
|
|
182
|
+
fieldDescriptionToAdminMeta,
|
|
183
|
+
fieldDescriptionToOpenApiProperty,
|
|
184
|
+
fieldDescriptionToZodType,
|
|
185
|
+
type ModelDescription,
|
|
186
|
+
modelDescriptionToAdminFields,
|
|
187
|
+
modelDescriptionToOpenApiSpec,
|
|
188
|
+
nestDottedFieldDescriptions,
|
|
189
|
+
SYSTEM_FIELD_PATHS,
|
|
190
|
+
} from "./schemaMetadata";
|
|
153
191
|
export * from "./scriptRunner";
|
|
154
192
|
export {adminBodyFieldsToStrip, scrubAdminFields, stripAdminBodyFields} from "./scrubAdminFields";
|
|
155
193
|
export * from "./secretProviders";
|
|
194
|
+
export * from "./seedRunner";
|
|
156
195
|
export * from "./sync/executors";
|
|
157
196
|
export * from "./sync/models";
|
|
158
197
|
export * from "./sync/mutationHandler";
|
|
@@ -174,4 +213,23 @@ export * from "./types/consentForm";
|
|
|
174
213
|
export * from "./types/consentResponse";
|
|
175
214
|
export * from "./utils";
|
|
176
215
|
export * from "./versionCheckPlugin";
|
|
216
|
+
export type {
|
|
217
|
+
WebhookClaimArgs,
|
|
218
|
+
WebhookClaimResult,
|
|
219
|
+
WebhookIdempotencyStore,
|
|
220
|
+
} from "./webhooks/idempotency/memoryStore";
|
|
221
|
+
export {createMemoryIdempotencyStore} from "./webhooks/idempotency/memoryStore";
|
|
222
|
+
export {type HmacSignatureOptions, hmacSignature} from "./webhooks/verifiers/hmac";
|
|
223
|
+
export {
|
|
224
|
+
type SendgridEventSignatureOptions,
|
|
225
|
+
sendgridEventSignature,
|
|
226
|
+
} from "./webhooks/verifiers/sendgrid";
|
|
227
|
+
export {type StripeSignatureOptions, stripeSignature} from "./webhooks/verifiers/stripe";
|
|
228
|
+
export {type TwilioSignatureOptions, twilioSignature} from "./webhooks/verifiers/twilio";
|
|
229
|
+
export {
|
|
230
|
+
type WebhookHandlerContext,
|
|
231
|
+
type WebhookRouteOptions,
|
|
232
|
+
WebhooksApp,
|
|
233
|
+
type WebhooksAppOptions,
|
|
234
|
+
} from "./webhooks/webhooksApp";
|
|
177
235
|
export {z} from "./zodOpenApi";
|
package/src/mcp/auth.test.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import {beforeEach, describe, expect, it} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
2
3
|
import jwt from "jsonwebtoken";
|
|
3
4
|
import mongoose, {Schema} from "mongoose";
|
|
4
5
|
|
|
5
6
|
import type {UserModel} from "../auth";
|
|
6
7
|
import type {BetterAuthInstance} from "../betterAuthSetup";
|
|
8
|
+
import {McpServiceToken} from "../models/mcpServiceToken";
|
|
7
9
|
import {findOneOrNone} from "../plugins";
|
|
8
10
|
import {extractUserFromHeaders} from "./auth";
|
|
9
11
|
|
|
@@ -46,6 +48,7 @@ const signToken = (payload: Record<string, unknown>, secret = "test-token-secret
|
|
|
46
48
|
describe("extractUserFromHeaders", () => {
|
|
47
49
|
beforeEach(async () => {
|
|
48
50
|
await UserTestModel.deleteMany({});
|
|
51
|
+
await McpServiceToken.deleteMany({});
|
|
49
52
|
process.env.TOKEN_SECRET = "test-token-secret";
|
|
50
53
|
});
|
|
51
54
|
|
|
@@ -70,6 +73,50 @@ describe("extractUserFromHeaders", () => {
|
|
|
70
73
|
expect(String((user as unknown as MCPAuthUserFields)?._id)).toBe(created._id.toString());
|
|
71
74
|
});
|
|
72
75
|
|
|
76
|
+
it("resolves the owner of an active MCP service token before JWT verification", async () => {
|
|
77
|
+
const created = await UserTestModel.create({email: "mcp-token@example.com"});
|
|
78
|
+
const issued = await McpServiceToken.issueFor({_id: created._id}, {name: "Perplexity"});
|
|
79
|
+
|
|
80
|
+
const user = await extractUserFromHeaders(
|
|
81
|
+
{authorization: `Bearer ${issued.token}`},
|
|
82
|
+
{mcpServiceTokens: true, userModel}
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
assert.equal(String((user as unknown as MCPAuthUserFields)?._id), created._id.toString());
|
|
86
|
+
|
|
87
|
+
await Bun.sleep(10);
|
|
88
|
+
const usedToken = await McpServiceToken.findById(issued.mcpServiceToken._id);
|
|
89
|
+
assert.isDefined(usedToken?.lastUsedAt);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("rejects revoked MCP service tokens", async () => {
|
|
93
|
+
const created = await UserTestModel.create({email: "revoked-mcp-token@example.com"});
|
|
94
|
+
const issued = await McpServiceToken.issueFor({_id: created._id}, {name: "Revoked"});
|
|
95
|
+
await McpServiceToken.revokeForUser({_id: created._id}, issued.mcpServiceToken._id);
|
|
96
|
+
|
|
97
|
+
const user = await extractUserFromHeaders(
|
|
98
|
+
{authorization: `Bearer ${issued.token}`},
|
|
99
|
+
{mcpServiceTokens: true, userModel}
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
assert.isUndefined(user);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("rejects MCP service tokens belonging to disabled users", async () => {
|
|
106
|
+
const created = await UserTestModel.create({
|
|
107
|
+
disabled: true,
|
|
108
|
+
email: "disabled-mcp-token@example.com",
|
|
109
|
+
});
|
|
110
|
+
const issued = await McpServiceToken.issueFor({_id: created._id}, {name: "Disabled"});
|
|
111
|
+
|
|
112
|
+
const user = await extractUserFromHeaders(
|
|
113
|
+
{authorization: `Bearer ${issued.token}`},
|
|
114
|
+
{mcpServiceTokens: true, userModel}
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
assert.isUndefined(user);
|
|
118
|
+
});
|
|
119
|
+
|
|
73
120
|
it("accepts a raw token without the Bearer prefix", async () => {
|
|
74
121
|
const created = await UserTestModel.create({email: "raw@example.com"});
|
|
75
122
|
const token = signToken({sub: created._id.toString()});
|
package/src/mcp/auth.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import jwt from "jsonwebtoken";
|
|
2
|
+
import {DateTime} from "luxon";
|
|
2
3
|
|
|
3
4
|
import type {User, UserModel} from "../auth";
|
|
4
5
|
import type {BetterAuthInstance} from "../betterAuthSetup";
|
|
5
6
|
import {logger} from "../logger";
|
|
7
|
+
import {McpServiceToken} from "../models/mcpServiceToken";
|
|
6
8
|
import {findOneOrNoneFor} from "../plugins";
|
|
7
9
|
|
|
8
10
|
export interface MCPAuthContext {
|
|
9
11
|
userModel: UserModel;
|
|
10
12
|
betterAuth?: BetterAuthInstance;
|
|
13
|
+
mcpServiceTokens?: boolean;
|
|
11
14
|
}
|
|
12
15
|
|
|
13
16
|
/**
|
|
@@ -34,9 +37,36 @@ export const extractUserFromHeaders = async (
|
|
|
34
37
|
headers: Record<string, string | string[] | undefined>,
|
|
35
38
|
context: MCPAuthContext
|
|
36
39
|
): Promise<User | undefined> => {
|
|
37
|
-
const {userModel, betterAuth} = context;
|
|
40
|
+
const {userModel, betterAuth, mcpServiceTokens} = context;
|
|
41
|
+
const authorization =
|
|
42
|
+
typeof headers.authorization === "string"
|
|
43
|
+
? headers.authorization
|
|
44
|
+
: Array.isArray(headers.authorization)
|
|
45
|
+
? headers.authorization[0]
|
|
46
|
+
: undefined;
|
|
47
|
+
const token = authorization?.startsWith("Bearer ") ? authorization.slice(7) : authorization;
|
|
48
|
+
|
|
49
|
+
// Resolve MCP service tokens before session/JWT auth because mcp_ is a distinct credential type.
|
|
50
|
+
if (mcpServiceTokens && token?.startsWith("mcp_")) {
|
|
51
|
+
const serviceToken = await McpServiceToken.verify(token);
|
|
52
|
+
if (!serviceToken) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
const user = await userModel.findById(serviceToken.userId);
|
|
56
|
+
const activeUser = rejectIfDisabled(user as unknown as User | undefined, "MCP service token");
|
|
57
|
+
if (!activeUser) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
void McpServiceToken.updateOne(
|
|
61
|
+
{_id: serviceToken._id},
|
|
62
|
+
{$set: {lastUsedAt: DateTime.now().toJSDate()}}
|
|
63
|
+
).catch((error) => {
|
|
64
|
+
logger.debug(`MCP service token last-used update failed: ${error}`);
|
|
65
|
+
});
|
|
66
|
+
return activeUser;
|
|
67
|
+
}
|
|
38
68
|
|
|
39
|
-
// Try Better Auth first if configured
|
|
69
|
+
// Try Better Auth first if configured.
|
|
40
70
|
if (betterAuth) {
|
|
41
71
|
try {
|
|
42
72
|
const session = await betterAuth.api.getSession({
|
|
@@ -58,19 +88,10 @@ export const extractUserFromHeaders = async (
|
|
|
58
88
|
}
|
|
59
89
|
|
|
60
90
|
// Try JWT auth
|
|
61
|
-
const authorization =
|
|
62
|
-
typeof headers.authorization === "string"
|
|
63
|
-
? headers.authorization
|
|
64
|
-
: Array.isArray(headers.authorization)
|
|
65
|
-
? headers.authorization[0]
|
|
66
|
-
: undefined;
|
|
67
|
-
|
|
68
91
|
if (!authorization) {
|
|
69
92
|
return undefined;
|
|
70
93
|
}
|
|
71
94
|
|
|
72
|
-
const token = authorization.startsWith("Bearer ") ? authorization.slice(7) : authorization;
|
|
73
|
-
|
|
74
95
|
if (!token) {
|
|
75
96
|
return undefined;
|
|
76
97
|
}
|