@terreno/api 57.19.2 → 57.20.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 +393 -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.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/index.d.ts +14 -2
- package/dist/index.js +23 -3
- package/dist/mcp/oauth.js +151 -11
- package/dist/mcp/oauthMetadata.d.ts +2 -1
- package/dist/mcp/oauthMetadata.js +7 -4
- package/dist/models/consentResponse.js +51 -0
- package/dist/models/mcpOAuthClient.js +78 -2
- package/dist/models/mcpOAuthToken.js +28 -4
- 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/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 +4 -0
- package/dist/terrenoApp.js +15 -0
- package/dist/tests/models.d.ts +5 -0
- package/dist/tests/models.js +8 -0
- package/dist/types/consentResponse.d.ts +8 -0
- package/dist/types/mcpOAuth.d.ts +35 -2
- package/package.json +6 -3
- 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/api.arrayOperations.test.ts +71 -0
- package/src/api.ts +38 -7
- package/src/audit/auditEventModel.ts +10 -7
- package/src/audit/processQueue.ts +4 -2
- package/src/audit/record.test.ts +15 -0
- package/src/audit/record.ts +10 -2
- package/src/authRecovery.test.ts +23 -0
- package/src/authRecovery.ts +34 -1
- package/src/authRecoveryBetterAuth.test.ts +169 -0
- 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 +489 -0
- package/src/betterAuthFeatures.ts +283 -0
- package/src/betterAuthLinking.test.ts +844 -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 +39 -7
- package/src/betterAuthSetup.ts +457 -38
- 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/index.ts +30 -0
- package/src/mcp/oauth.test.ts +463 -2
- package/src/mcp/oauth.ts +184 -13
- package/src/mcp/oauthMetadata.ts +5 -2
- package/src/models/consentResponse.ts +53 -0
- package/src/models/mcpOAuthClient.ts +100 -3
- package/src/models/mcpOAuthToken.ts +30 -5
- package/src/orgs/membershipGuards.ts +39 -0
- package/src/orgs/orgRbac.test.ts +1 -1
- package/src/orgs/orgsApp.test.ts +101 -2
- package/src/orgs/orgsApp.ts +69 -28
- package/src/rateLimit/policies.test.ts +53 -0
- package/src/rateLimit/policies.ts +8 -0
- package/src/rbac/access.ts +4 -1
- 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 +48 -0
- package/src/rbac/userWriteGuards.ts +5 -2
- package/src/schemaMetadata.test.ts +39 -0
- package/src/schemaMetadata.ts +11 -1
- package/src/seedRunner.test.ts +56 -0
- package/src/seedRunner.ts +7 -2
- package/src/terrenoApp.ts +20 -0
- package/src/tests/accountSecurityTestApp.ts +290 -0
- package/src/tests/betterAuthStubs.ts +188 -0
- package/src/tests/mockOidcProvider.ts +105 -0
- package/src/tests/models.ts +13 -0
- package/src/tests/softwareAuthenticator.ts +183 -0
- package/src/types/consentResponse.ts +8 -0
- package/src/types/mcpOAuth.ts +39 -3
package/src/mcp/oauth.test.ts
CHANGED
|
@@ -204,13 +204,17 @@ describe("MCP OAuth authorization server", () => {
|
|
|
204
204
|
expect(res.body).toMatchObject({
|
|
205
205
|
authorization_endpoint: `${ISSUER}/oauth/authorize`,
|
|
206
206
|
code_challenge_methods_supported: ["S256"],
|
|
207
|
-
grant_types_supported: ["authorization_code", "refresh_token"],
|
|
207
|
+
grant_types_supported: ["authorization_code", "refresh_token", "client_credentials"],
|
|
208
208
|
issuer: ISSUER,
|
|
209
209
|
registration_endpoint: `${ISSUER}/oauth/register`,
|
|
210
210
|
response_types_supported: ["code"],
|
|
211
211
|
revocation_endpoint: `${ISSUER}/oauth/revoke`,
|
|
212
212
|
token_endpoint: `${ISSUER}/oauth/token`,
|
|
213
|
-
token_endpoint_auth_methods_supported: [
|
|
213
|
+
token_endpoint_auth_methods_supported: [
|
|
214
|
+
"none",
|
|
215
|
+
"client_secret_basic",
|
|
216
|
+
"client_secret_post",
|
|
217
|
+
],
|
|
214
218
|
});
|
|
215
219
|
});
|
|
216
220
|
});
|
|
@@ -345,6 +349,24 @@ describe("MCP OAuth authorization server", () => {
|
|
|
345
349
|
expect(redirect.searchParams.get("state")).toBe("s");
|
|
346
350
|
});
|
|
347
351
|
|
|
352
|
+
it("rejects authorization for a revoked client without redirecting", async () => {
|
|
353
|
+
const clientId = await registerClient(app);
|
|
354
|
+
await McpOAuthClient.revoke(clientId);
|
|
355
|
+
const {challenge} = createPkce();
|
|
356
|
+
|
|
357
|
+
const res = await startAuthorization(app, {
|
|
358
|
+
client_id: clientId,
|
|
359
|
+
code_challenge: challenge,
|
|
360
|
+
code_challenge_method: "S256",
|
|
361
|
+
redirect_uri: REDIRECT_URI,
|
|
362
|
+
response_type: "code",
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
expect(res.status).toBe(400);
|
|
366
|
+
expect(res.body.error).toBe("invalid_request");
|
|
367
|
+
expect(res.headers.location).toBeUndefined();
|
|
368
|
+
});
|
|
369
|
+
|
|
348
370
|
it("answers in place, without redirecting, for an unregistered redirect_uri", async () => {
|
|
349
371
|
const clientId = await registerClient(app);
|
|
350
372
|
const {challenge} = createPkce();
|
|
@@ -361,6 +383,97 @@ describe("MCP OAuth authorization server", () => {
|
|
|
361
383
|
expect(res.body.error).toBe("invalid_request");
|
|
362
384
|
});
|
|
363
385
|
|
|
386
|
+
it("sends the authorization code to the requested loopback port", async () => {
|
|
387
|
+
await McpOAuthClient.create({
|
|
388
|
+
clientId: "claude-code",
|
|
389
|
+
clientName: "Claude Code",
|
|
390
|
+
grantTypes: ["authorization_code", "refresh_token"],
|
|
391
|
+
redirectUris: ["http://127.0.0.1:33418/callback"],
|
|
392
|
+
responseTypes: ["code"],
|
|
393
|
+
});
|
|
394
|
+
const {challenge} = createPkce();
|
|
395
|
+
|
|
396
|
+
const authorize = await startAuthorization(app, {
|
|
397
|
+
client_id: "claude-code",
|
|
398
|
+
code_challenge: challenge,
|
|
399
|
+
code_challenge_method: "S256",
|
|
400
|
+
redirect_uri: "http://127.0.0.1:54321/callback",
|
|
401
|
+
response_type: "code",
|
|
402
|
+
state: "loopback-state",
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
expect(authorize.status).toBe(302);
|
|
406
|
+
const approve = await agent.post(
|
|
407
|
+
`/oauth/authorize/${requestIdFrom(authorize.headers.location)}/approve`
|
|
408
|
+
);
|
|
409
|
+
expect(approve.status).toBe(200);
|
|
410
|
+
const redirect = new URL(approve.body.data.redirectUrl);
|
|
411
|
+
expect(redirect.origin).toBe("http://127.0.0.1:54321");
|
|
412
|
+
expect(redirect.pathname).toBe("/callback");
|
|
413
|
+
expect(redirect.searchParams.get("code")).toBeString();
|
|
414
|
+
expect(redirect.searchParams.get("state")).toBe("loopback-state");
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
it("accepts any port for localhost and [::1] loopback redirects", async () => {
|
|
418
|
+
await McpOAuthClient.create({
|
|
419
|
+
clientId: "claude-code-localhost",
|
|
420
|
+
grantTypes: ["authorization_code"],
|
|
421
|
+
redirectUris: ["http://localhost:33418/callback", "http://[::1]:33418/callback"],
|
|
422
|
+
responseTypes: ["code"],
|
|
423
|
+
});
|
|
424
|
+
const {challenge} = createPkce();
|
|
425
|
+
|
|
426
|
+
const localhost = await startAuthorization(app, {
|
|
427
|
+
client_id: "claude-code-localhost",
|
|
428
|
+
code_challenge: challenge,
|
|
429
|
+
code_challenge_method: "S256",
|
|
430
|
+
redirect_uri: "http://localhost:9/callback",
|
|
431
|
+
response_type: "code",
|
|
432
|
+
});
|
|
433
|
+
const ipv6 = await startAuthorization(app, {
|
|
434
|
+
client_id: "claude-code-localhost",
|
|
435
|
+
code_challenge: challenge,
|
|
436
|
+
code_challenge_method: "S256",
|
|
437
|
+
redirect_uri: "http://[::1]:9/callback",
|
|
438
|
+
response_type: "code",
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
expect(localhost.status).toBe(302);
|
|
442
|
+
expect(ipv6.status).toBe(302);
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it("keeps loopback matching exact for path, query, host, and non-loopback ports", async () => {
|
|
446
|
+
await McpOAuthClient.create({
|
|
447
|
+
clientId: "claude-code-strict",
|
|
448
|
+
grantTypes: ["authorization_code"],
|
|
449
|
+
redirectUris: [
|
|
450
|
+
"http://localhost:33418/callback",
|
|
451
|
+
"https://claude.ai/api/mcp/auth_callback",
|
|
452
|
+
],
|
|
453
|
+
responseTypes: ["code"],
|
|
454
|
+
});
|
|
455
|
+
const {challenge} = createPkce();
|
|
456
|
+
const attempt = (redirectUri: string) =>
|
|
457
|
+
startAuthorization(app, {
|
|
458
|
+
client_id: "claude-code-strict",
|
|
459
|
+
code_challenge: challenge,
|
|
460
|
+
code_challenge_method: "S256",
|
|
461
|
+
redirect_uri: redirectUri,
|
|
462
|
+
response_type: "code",
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
const otherPath = await attempt("http://localhost:9/other");
|
|
466
|
+
const otherQuery = await attempt("http://localhost:9/callback?extra=1");
|
|
467
|
+
const otherHost = await attempt("http://127.0.0.1:9/callback");
|
|
468
|
+
const otherPublicPort = await attempt("https://claude.ai:8443/api/mcp/auth_callback");
|
|
469
|
+
|
|
470
|
+
expect(otherPath.status).toBe(400);
|
|
471
|
+
expect(otherQuery.status).toBe(400);
|
|
472
|
+
expect(otherHost.status).toBe(400);
|
|
473
|
+
expect(otherPublicPort.status).toBe(400);
|
|
474
|
+
expect(otherPath.body.error).toBe("invalid_request");
|
|
475
|
+
});
|
|
476
|
+
|
|
364
477
|
it("rejects a wrong code_verifier and burns the code", async () => {
|
|
365
478
|
const flow = await approvedCode(app, agent);
|
|
366
479
|
|
|
@@ -508,6 +621,27 @@ describe("MCP OAuth authorization server", () => {
|
|
|
508
621
|
|
|
509
622
|
expect(res.status).toBe(401);
|
|
510
623
|
});
|
|
624
|
+
|
|
625
|
+
it("discards an authorization-code exchange when the client is revoked during issuance", async () => {
|
|
626
|
+
const flow = await approvedCode(app, agent);
|
|
627
|
+
const realIssue = McpOAuthToken.issue.bind(McpOAuthToken);
|
|
628
|
+
const issueSpy = spyOn(McpOAuthToken, "issue").mockImplementation(async (options) => {
|
|
629
|
+
await McpOAuthClient.revoke(options.clientId);
|
|
630
|
+
return realIssue(options);
|
|
631
|
+
});
|
|
632
|
+
let token: supertest.Response;
|
|
633
|
+
try {
|
|
634
|
+
token = await exchangeCode(app, flow);
|
|
635
|
+
} finally {
|
|
636
|
+
issueSpy.mockRestore();
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
expect(token.status).toBe(401);
|
|
640
|
+
expect(token.body.error).toBe("invalid_client");
|
|
641
|
+
expect(
|
|
642
|
+
await McpOAuthToken.countDocuments({clientId: flow.clientId, revokedAt: {$exists: false}})
|
|
643
|
+
).toBe(0);
|
|
644
|
+
});
|
|
511
645
|
});
|
|
512
646
|
|
|
513
647
|
describe("refresh tokens", () => {
|
|
@@ -620,6 +754,30 @@ describe("MCP OAuth authorization server", () => {
|
|
|
620
754
|
expect(responses.map((res) => res.body.error)).toEqual(["invalid_grant", "invalid_grant"]);
|
|
621
755
|
expect(await McpOAuthToken.countDocuments({revokedAt: {$exists: false}})).toBe(0);
|
|
622
756
|
});
|
|
757
|
+
|
|
758
|
+
it("discards a refresh when the client is revoked during issuance", async () => {
|
|
759
|
+
const flow = await approvedCode(app, agent);
|
|
760
|
+
const first = await exchangeCode(app, flow);
|
|
761
|
+
expect(first.status).toBe(200);
|
|
762
|
+
const realIssue = McpOAuthToken.issue.bind(McpOAuthToken);
|
|
763
|
+
const issueSpy = spyOn(McpOAuthToken, "issue").mockImplementation(async (options) => {
|
|
764
|
+
await McpOAuthClient.revoke(options.clientId);
|
|
765
|
+
return realIssue(options);
|
|
766
|
+
});
|
|
767
|
+
let refreshed: supertest.Response;
|
|
768
|
+
try {
|
|
769
|
+
refreshed = await refresh(app, flow.clientId, first.body.refresh_token as string);
|
|
770
|
+
} finally {
|
|
771
|
+
issueSpy.mockRestore();
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
expect(refreshed.status).toBe(401);
|
|
775
|
+
expect(refreshed.body.error).toBe("invalid_client");
|
|
776
|
+
expect((await callMcp(app, first.body.access_token as string)).status).toBe(401);
|
|
777
|
+
expect(
|
|
778
|
+
await McpOAuthToken.countDocuments({clientId: flow.clientId, revokedAt: {$exists: false}})
|
|
779
|
+
).toBe(0);
|
|
780
|
+
});
|
|
623
781
|
});
|
|
624
782
|
|
|
625
783
|
describe("revocation", () => {
|
|
@@ -782,6 +940,53 @@ describe("MCP OAuth authorization server", () => {
|
|
|
782
940
|
});
|
|
783
941
|
});
|
|
784
942
|
|
|
943
|
+
it("approves consent when middleware already set req.user", async () => {
|
|
944
|
+
const user = await UserModel.findById(notAdminId);
|
|
945
|
+
const consentApp = new TerrenoApp({
|
|
946
|
+
mcpOAuth: oauthOptions,
|
|
947
|
+
skipListen: true,
|
|
948
|
+
userModel: typedUserModel,
|
|
949
|
+
})
|
|
950
|
+
.addMiddleware((req, _res, next) => {
|
|
951
|
+
if (req.path.startsWith("/oauth/authorize/")) {
|
|
952
|
+
req.user = user ?? undefined;
|
|
953
|
+
}
|
|
954
|
+
next();
|
|
955
|
+
})
|
|
956
|
+
.register(noteRouter())
|
|
957
|
+
.build();
|
|
958
|
+
const clientId = await registerClient(consentApp);
|
|
959
|
+
const {challenge, verifier} = createPkce();
|
|
960
|
+
const authorize = await startAuthorization(consentApp, {
|
|
961
|
+
client_id: clientId,
|
|
962
|
+
code_challenge: challenge,
|
|
963
|
+
code_challenge_method: "S256",
|
|
964
|
+
redirect_uri: REDIRECT_URI,
|
|
965
|
+
response_type: "code",
|
|
966
|
+
state: "consent-user",
|
|
967
|
+
});
|
|
968
|
+
const requestId = requestIdFrom(authorize.headers.location);
|
|
969
|
+
const unsigned = await supertest(app).post(`/oauth/authorize/${requestId}/approve`);
|
|
970
|
+
const pending = await supertest(consentApp).get(`/oauth/authorize/${requestId}`);
|
|
971
|
+
const approve = await supertest(consentApp).post(`/oauth/authorize/${requestId}/approve`);
|
|
972
|
+
|
|
973
|
+
expect(unsigned.status).toBe(401);
|
|
974
|
+
expect(pending.status).toBe(200);
|
|
975
|
+
expect(pending.body.data.clientName).toBe("Claude");
|
|
976
|
+
expect(approve.status).toBe(200);
|
|
977
|
+
const redirect = new URL(approve.body.data.redirectUrl);
|
|
978
|
+
const token = await exchangeCode(consentApp, {
|
|
979
|
+
clientId,
|
|
980
|
+
code: redirect.searchParams.get("code") as string,
|
|
981
|
+
verifier,
|
|
982
|
+
});
|
|
983
|
+
expect(token.status).toBe(200);
|
|
984
|
+
expect(toolPayload((await callMcp(consentApp, token.body.access_token)).text)).toEqual({
|
|
985
|
+
credential: "oauth",
|
|
986
|
+
userId: String(notAdminId),
|
|
987
|
+
});
|
|
988
|
+
});
|
|
989
|
+
|
|
785
990
|
it("documents the consent endpoints in OpenAPI", async () => {
|
|
786
991
|
const res = await supertest(app).get("/openapi.json");
|
|
787
992
|
|
|
@@ -789,6 +994,262 @@ describe("MCP OAuth authorization server", () => {
|
|
|
789
994
|
"approveMcpOAuthRequest"
|
|
790
995
|
);
|
|
791
996
|
});
|
|
997
|
+
describe("client credentials", () => {
|
|
998
|
+
const basicAuthorization = (clientId: string, clientSecret: string): string => {
|
|
999
|
+
const encoded = Buffer.from(
|
|
1000
|
+
`${encodeURIComponent(clientId)}:${encodeURIComponent(clientSecret)}`
|
|
1001
|
+
).toString("base64");
|
|
1002
|
+
return `Basic ${encoded}`;
|
|
1003
|
+
};
|
|
1004
|
+
|
|
1005
|
+
it("issues an access token for client_credentials and no refresh token", async () => {
|
|
1006
|
+
const created = await McpOAuthClient.issueConfidential({
|
|
1007
|
+
clientName: "Sandbox agent",
|
|
1008
|
+
tokenEndpointAuthMethod: "client_secret_basic",
|
|
1009
|
+
userId: notAdminId,
|
|
1010
|
+
});
|
|
1011
|
+
|
|
1012
|
+
const token = await supertest(app)
|
|
1013
|
+
.post("/oauth/token")
|
|
1014
|
+
.set("Authorization", basicAuthorization(created.client.clientId, created.clientSecret))
|
|
1015
|
+
.type("form")
|
|
1016
|
+
.send({grant_type: "client_credentials"});
|
|
1017
|
+
|
|
1018
|
+
expect(token.status).toBe(200);
|
|
1019
|
+
expect(token.body.access_token.startsWith("mcpat_")).toBe(true);
|
|
1020
|
+
expect(token.body.refresh_token).toBeUndefined();
|
|
1021
|
+
expect(token.body.token_type).toBe("Bearer");
|
|
1022
|
+
expect(token.headers["cache-control"]).toBe("no-store");
|
|
1023
|
+
expect(created.client.clientSecretHash).not.toBe(created.clientSecret);
|
|
1024
|
+
const call = await callMcp(app, token.body.access_token as string);
|
|
1025
|
+
expect(call.status).toBe(200);
|
|
1026
|
+
expect(toolPayload(call.text)).toMatchObject({
|
|
1027
|
+
credential: "oauth",
|
|
1028
|
+
userId: String(notAdminId),
|
|
1029
|
+
});
|
|
1030
|
+
});
|
|
1031
|
+
|
|
1032
|
+
it("accepts client_secret_post and rejects the other method, a bad secret, and a public client", async () => {
|
|
1033
|
+
const created = await McpOAuthClient.issueConfidential({
|
|
1034
|
+
clientName: "Reader",
|
|
1035
|
+
tokenEndpointAuthMethod: "client_secret_post",
|
|
1036
|
+
userId: notAdminId,
|
|
1037
|
+
});
|
|
1038
|
+
const posted = await supertest(app).post("/oauth/token").type("form").send({
|
|
1039
|
+
client_id: created.client.clientId,
|
|
1040
|
+
client_secret: created.clientSecret,
|
|
1041
|
+
grant_type: "client_credentials",
|
|
1042
|
+
});
|
|
1043
|
+
const asBasic = await supertest(app)
|
|
1044
|
+
.post("/oauth/token")
|
|
1045
|
+
.set("Authorization", basicAuthorization(created.client.clientId, created.clientSecret))
|
|
1046
|
+
.type("form")
|
|
1047
|
+
.send({grant_type: "client_credentials"});
|
|
1048
|
+
const badSecret = await supertest(app).post("/oauth/token").type("form").send({
|
|
1049
|
+
client_id: created.client.clientId,
|
|
1050
|
+
client_secret: "not-the-secret",
|
|
1051
|
+
grant_type: "client_credentials",
|
|
1052
|
+
});
|
|
1053
|
+
const publicClient = await registerClient(app);
|
|
1054
|
+
const publicGrant = await supertest(app).post("/oauth/token").type("form").send({
|
|
1055
|
+
client_id: publicClient,
|
|
1056
|
+
grant_type: "client_credentials",
|
|
1057
|
+
});
|
|
1058
|
+
|
|
1059
|
+
expect(posted.status).toBe(200);
|
|
1060
|
+
expect(posted.body.refresh_token).toBeUndefined();
|
|
1061
|
+
expect(asBasic.status).toBe(401);
|
|
1062
|
+
expect(asBasic.body.error).toBe("invalid_client");
|
|
1063
|
+
expect(badSecret.status).toBe(401);
|
|
1064
|
+
expect(badSecret.body.error).toBe("invalid_client");
|
|
1065
|
+
expect(publicGrant.status).toBe(400);
|
|
1066
|
+
expect(publicGrant.body.error).toBe("unsupported_grant_type");
|
|
1067
|
+
});
|
|
1068
|
+
|
|
1069
|
+
it("refuses a disabled user and ends the token after a password reset", async () => {
|
|
1070
|
+
const created = await McpOAuthClient.issueConfidential({
|
|
1071
|
+
clientName: "Sandbox agent",
|
|
1072
|
+
tokenEndpointAuthMethod: "client_secret_basic",
|
|
1073
|
+
userId: notAdminId,
|
|
1074
|
+
});
|
|
1075
|
+
const authorization = basicAuthorization(created.client.clientId, created.clientSecret);
|
|
1076
|
+
const issued = await supertest(app)
|
|
1077
|
+
.post("/oauth/token")
|
|
1078
|
+
.set("Authorization", authorization)
|
|
1079
|
+
.type("form")
|
|
1080
|
+
.send({grant_type: "client_credentials"});
|
|
1081
|
+
expect(issued.status).toBe(200);
|
|
1082
|
+
|
|
1083
|
+
const reset = await AuthToken.issueFor({_id: String(notAdminId)}, "passwordReset");
|
|
1084
|
+
const passwordReset = await supertest(app)
|
|
1085
|
+
.post("/auth/resetPassword")
|
|
1086
|
+
.send({password: "reset-password-123", token: reset.token});
|
|
1087
|
+
expect(passwordReset.status).toBe(200);
|
|
1088
|
+
expect((await callMcp(app, issued.body.access_token as string)).status).toBe(401);
|
|
1089
|
+
|
|
1090
|
+
await UserModel.updateOne({_id: notAdminId}, {$set: {disabled: true}});
|
|
1091
|
+
const again = await supertest(app)
|
|
1092
|
+
.post("/oauth/token")
|
|
1093
|
+
.set("Authorization", authorization)
|
|
1094
|
+
.type("form")
|
|
1095
|
+
.send({grant_type: "client_credentials"});
|
|
1096
|
+
expect(again.status).toBe(400);
|
|
1097
|
+
expect(again.body.error).toBe("invalid_grant");
|
|
1098
|
+
});
|
|
1099
|
+
|
|
1100
|
+
it("ends every token when the client is revoked or deleted", async () => {
|
|
1101
|
+
const revokedClient = await McpOAuthClient.issueConfidential({
|
|
1102
|
+
clientName: "Revoked",
|
|
1103
|
+
tokenEndpointAuthMethod: "client_secret_basic",
|
|
1104
|
+
userId: notAdminId,
|
|
1105
|
+
});
|
|
1106
|
+
const deletedClient = await McpOAuthClient.issueConfidential({
|
|
1107
|
+
clientName: "Deleted",
|
|
1108
|
+
tokenEndpointAuthMethod: "client_secret_post",
|
|
1109
|
+
userId: notAdminId,
|
|
1110
|
+
});
|
|
1111
|
+
const revokedToken = await supertest(app)
|
|
1112
|
+
.post("/oauth/token")
|
|
1113
|
+
.set(
|
|
1114
|
+
"Authorization",
|
|
1115
|
+
basicAuthorization(revokedClient.client.clientId, revokedClient.clientSecret)
|
|
1116
|
+
)
|
|
1117
|
+
.type("form")
|
|
1118
|
+
.send({grant_type: "client_credentials"});
|
|
1119
|
+
const deletedToken = await supertest(app).post("/oauth/token").type("form").send({
|
|
1120
|
+
client_id: deletedClient.client.clientId,
|
|
1121
|
+
client_secret: deletedClient.clientSecret,
|
|
1122
|
+
grant_type: "client_credentials",
|
|
1123
|
+
});
|
|
1124
|
+
expect(revokedToken.status).toBe(200);
|
|
1125
|
+
expect(deletedToken.status).toBe(200);
|
|
1126
|
+
|
|
1127
|
+
await McpOAuthClient.revoke(revokedClient.client.clientId);
|
|
1128
|
+
await McpOAuthClient.deleteOne({clientId: deletedClient.client.clientId});
|
|
1129
|
+
|
|
1130
|
+
expect((await callMcp(app, revokedToken.body.access_token as string)).status).toBe(401);
|
|
1131
|
+
expect((await callMcp(app, deletedToken.body.access_token as string)).status).toBe(401);
|
|
1132
|
+
const afterRevoke = await supertest(app)
|
|
1133
|
+
.post("/oauth/token")
|
|
1134
|
+
.set(
|
|
1135
|
+
"Authorization",
|
|
1136
|
+
basicAuthorization(revokedClient.client.clientId, revokedClient.clientSecret)
|
|
1137
|
+
)
|
|
1138
|
+
.type("form")
|
|
1139
|
+
.send({grant_type: "client_credentials"});
|
|
1140
|
+
expect(afterRevoke.status).toBe(401);
|
|
1141
|
+
expect(afterRevoke.body.error).toBe("invalid_client");
|
|
1142
|
+
});
|
|
1143
|
+
|
|
1144
|
+
it("ends tokens when the client is removed with deleteMany", async () => {
|
|
1145
|
+
const created = await McpOAuthClient.issueConfidential({
|
|
1146
|
+
clientName: "Bulk",
|
|
1147
|
+
tokenEndpointAuthMethod: "client_secret_basic",
|
|
1148
|
+
userId: notAdminId,
|
|
1149
|
+
});
|
|
1150
|
+
const issued = await supertest(app)
|
|
1151
|
+
.post("/oauth/token")
|
|
1152
|
+
.set("Authorization", basicAuthorization(created.client.clientId, created.clientSecret))
|
|
1153
|
+
.type("form")
|
|
1154
|
+
.send({grant_type: "client_credentials"});
|
|
1155
|
+
expect(issued.status).toBe(200);
|
|
1156
|
+
|
|
1157
|
+
await McpOAuthClient.deleteMany({clientId: created.client.clientId});
|
|
1158
|
+
|
|
1159
|
+
expect((await callMcp(app, issued.body.access_token as string)).status).toBe(401);
|
|
1160
|
+
const again = await supertest(app)
|
|
1161
|
+
.post("/oauth/token")
|
|
1162
|
+
.set("Authorization", basicAuthorization(created.client.clientId, created.clientSecret))
|
|
1163
|
+
.type("form")
|
|
1164
|
+
.send({grant_type: "client_credentials"});
|
|
1165
|
+
expect(again.status).toBe(401);
|
|
1166
|
+
expect(again.body.error).toBe("invalid_client");
|
|
1167
|
+
});
|
|
1168
|
+
|
|
1169
|
+
it("revokes only the client a multi-match deleteOne removes", async () => {
|
|
1170
|
+
const kept = await McpOAuthClient.issueConfidential({
|
|
1171
|
+
clientName: "Kept",
|
|
1172
|
+
tokenEndpointAuthMethod: "client_secret_basic",
|
|
1173
|
+
userId: notAdminId,
|
|
1174
|
+
});
|
|
1175
|
+
const removed = await McpOAuthClient.issueConfidential({
|
|
1176
|
+
clientName: "Removed",
|
|
1177
|
+
tokenEndpointAuthMethod: "client_secret_basic",
|
|
1178
|
+
userId: notAdminId,
|
|
1179
|
+
});
|
|
1180
|
+
const issue = (clientId: string, clientSecret: string) =>
|
|
1181
|
+
supertest(app)
|
|
1182
|
+
.post("/oauth/token")
|
|
1183
|
+
.set("Authorization", basicAuthorization(clientId, clientSecret))
|
|
1184
|
+
.type("form")
|
|
1185
|
+
.send({grant_type: "client_credentials"});
|
|
1186
|
+
const keptToken = await issue(kept.client.clientId, kept.clientSecret);
|
|
1187
|
+
const removedToken = await issue(removed.client.clientId, removed.clientSecret);
|
|
1188
|
+
expect(keptToken.status).toBe(200);
|
|
1189
|
+
expect(removedToken.status).toBe(200);
|
|
1190
|
+
|
|
1191
|
+
await McpOAuthClient.deleteOne({tokenEndpointAuthMethod: "client_secret_basic"});
|
|
1192
|
+
|
|
1193
|
+
const remaining = await McpOAuthClient.find({
|
|
1194
|
+
tokenEndpointAuthMethod: "client_secret_basic",
|
|
1195
|
+
}).select({clientId: 1});
|
|
1196
|
+
expect(remaining).toHaveLength(1);
|
|
1197
|
+
const survivorStillWorks =
|
|
1198
|
+
remaining[0]?.clientId === kept.client.clientId
|
|
1199
|
+
? keptToken.body.access_token
|
|
1200
|
+
: removedToken.body.access_token;
|
|
1201
|
+
const removedAccessToken =
|
|
1202
|
+
remaining[0]?.clientId === kept.client.clientId
|
|
1203
|
+
? removedToken.body.access_token
|
|
1204
|
+
: keptToken.body.access_token;
|
|
1205
|
+
expect((await callMcp(app, survivorStillWorks as string)).status).toBe(200);
|
|
1206
|
+
expect((await callMcp(app, removedAccessToken as string)).status).toBe(401);
|
|
1207
|
+
});
|
|
1208
|
+
|
|
1209
|
+
it("discards a client_credentials token when the client is revoked during issuance", async () => {
|
|
1210
|
+
const created = await McpOAuthClient.issueConfidential({
|
|
1211
|
+
clientName: "Racy",
|
|
1212
|
+
tokenEndpointAuthMethod: "client_secret_basic",
|
|
1213
|
+
userId: notAdminId,
|
|
1214
|
+
});
|
|
1215
|
+
const realIssue = McpOAuthToken.issue.bind(McpOAuthToken);
|
|
1216
|
+
const issueSpy = spyOn(McpOAuthToken, "issue").mockImplementation(async (options) => {
|
|
1217
|
+
await McpOAuthClient.revoke(options.clientId);
|
|
1218
|
+
return realIssue(options);
|
|
1219
|
+
});
|
|
1220
|
+
let token: supertest.Response;
|
|
1221
|
+
try {
|
|
1222
|
+
token = await supertest(app)
|
|
1223
|
+
.post("/oauth/token")
|
|
1224
|
+
.set("Authorization", basicAuthorization(created.client.clientId, created.clientSecret))
|
|
1225
|
+
.type("form")
|
|
1226
|
+
.send({grant_type: "client_credentials"});
|
|
1227
|
+
} finally {
|
|
1228
|
+
issueSpy.mockRestore();
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
expect(token.status).toBe(401);
|
|
1232
|
+
expect(token.body.error).toBe("invalid_client");
|
|
1233
|
+
expect(
|
|
1234
|
+
await McpOAuthToken.countDocuments({
|
|
1235
|
+
clientId: created.client.clientId,
|
|
1236
|
+
revokedAt: {$exists: false},
|
|
1237
|
+
})
|
|
1238
|
+
).toBe(0);
|
|
1239
|
+
});
|
|
1240
|
+
|
|
1241
|
+
it("rejects client_credentials during dynamic client registration", async () => {
|
|
1242
|
+
const res = await supertest(app)
|
|
1243
|
+
.post("/oauth/register")
|
|
1244
|
+
.send({
|
|
1245
|
+
grant_types: ["authorization_code", "client_credentials"],
|
|
1246
|
+
redirect_uris: [REDIRECT_URI],
|
|
1247
|
+
});
|
|
1248
|
+
|
|
1249
|
+
expect(res.status).toBe(400);
|
|
1250
|
+
expect(res.body.error).toBe("invalid_client_metadata");
|
|
1251
|
+
});
|
|
1252
|
+
});
|
|
792
1253
|
});
|
|
793
1254
|
|
|
794
1255
|
describe("MCP OAuth granted scopes", () => {
|