|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { mkdtemp } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { loadAuthState, saveAuthState } from "./auth-store.js"; |
| 6 | +import { createOAuthProvider } from "./oauth-provider.js"; |
| 7 | + |
| 8 | +async function tempHome(): Promise<string> { |
| 9 | + return mkdtemp(join(tmpdir(), "mcp-oauth-")); |
| 10 | +} |
| 11 | + |
| 12 | +const clientInfo = (port: number) => ({ |
| 13 | + client_id: `client-on-${String(port)}`, |
| 14 | + redirect_uris: [`http://127.0.0.1:${String(port)}/callback`], |
| 15 | + client_id_issued_at: 1, |
| 16 | + token_endpoint_auth_method: "none" as const, |
| 17 | + grant_types: ["authorization_code", "refresh_token"], |
| 18 | + response_types: ["code"], |
| 19 | + client_name: "interchange-code", |
| 20 | +}); |
| 21 | + |
| 22 | +async function syncValue<T>(value: T | Promise<T>): Promise<T> { |
| 23 | + return await value; |
| 24 | +} |
| 25 | + |
| 26 | +describe("createOAuthProvider", () => { |
| 27 | + test("drops stale DCR client when redirect port changed and no tokens exist", async () => { |
| 28 | + const home = await tempHome(); |
| 29 | + await saveAuthState("linear", { |
| 30 | + clientInformation: clientInfo(60435), |
| 31 | + codeVerifier: "old-verifier", |
| 32 | + }, home); |
| 33 | + |
| 34 | + const provider = await createOAuthProvider({ |
| 35 | + serverName: "linear", |
| 36 | + redirectUrl: "http://127.0.0.1:62000/callback", |
| 37 | + onAuthURL: () => undefined, |
| 38 | + home, |
| 39 | + }); |
| 40 | + |
| 41 | + expect(await syncValue(provider.clientInformation())).toBeUndefined(); |
| 42 | + const disk = await loadAuthState("linear", home); |
| 43 | + expect(disk.clientInformation).toBeUndefined(); |
| 44 | + expect(disk.codeVerifier).toBeUndefined(); |
| 45 | + }); |
| 46 | + |
| 47 | + test("keeps registered client and tokens when only the loopback port changed", async () => { |
| 48 | + const home = await tempHome(); |
| 49 | + await saveAuthState("linear", { |
| 50 | + clientInformation: clientInfo(60435), |
| 51 | + tokens: { |
| 52 | + access_token: "live", |
| 53 | + token_type: "bearer", |
| 54 | + expires_in: 3600, |
| 55 | + refresh_token: "refresh", |
| 56 | + }, |
| 57 | + }, home); |
| 58 | + |
| 59 | + const provider = await createOAuthProvider({ |
| 60 | + serverName: "linear", |
| 61 | + redirectUrl: "http://127.0.0.1:62000/callback", |
| 62 | + onAuthURL: () => undefined, |
| 63 | + home, |
| 64 | + }); |
| 65 | + |
| 66 | + expect((await syncValue(provider.clientInformation()))?.client_id).toBe("client-on-60435"); |
| 67 | + expect((await syncValue(provider.tokens()))?.access_token).toBe("live"); |
| 68 | + }); |
| 69 | + |
| 70 | + test("concurrent saveTokens and saveCodeVerifier from two providers keep both fields", async () => { |
| 71 | + const home = await tempHome(); |
| 72 | + await saveAuthState("linear", { clientInformation: clientInfo(1) }, home); |
| 73 | + |
| 74 | + const a = await createOAuthProvider({ |
| 75 | + serverName: "linear", |
| 76 | + redirectUrl: "http://127.0.0.1:1/callback", |
| 77 | + onAuthURL: () => undefined, |
| 78 | + home, |
| 79 | + }); |
| 80 | + const b = await createOAuthProvider({ |
| 81 | + serverName: "linear", |
| 82 | + redirectUrl: "http://127.0.0.1:1/callback", |
| 83 | + onAuthURL: () => undefined, |
| 84 | + home, |
| 85 | + }); |
| 86 | + |
| 87 | + await Promise.all([ |
| 88 | + a.saveTokens({ |
| 89 | + access_token: "tok-a", |
| 90 | + token_type: "bearer", |
| 91 | + expires_in: 60, |
| 92 | + refresh_token: "ref-a", |
| 93 | + }), |
| 94 | + b.saveCodeVerifier("verifier-b"), |
| 95 | + ]); |
| 96 | + |
| 97 | + const disk = await loadAuthState("linear", home); |
| 98 | + expect(disk.tokens?.access_token).toBe("tok-a"); |
| 99 | + expect(disk.codeVerifier).toBe("verifier-b"); |
| 100 | + }); |
| 101 | + |
| 102 | + test("resetAuthorization clears client when redirect no longer matches registration", async () => { |
| 103 | + const home = await tempHome(); |
| 104 | + await saveAuthState("linear", { |
| 105 | + clientInformation: clientInfo(60435), |
| 106 | + tokens: { |
| 107 | + access_token: "live", |
| 108 | + token_type: "bearer", |
| 109 | + expires_in: 1, |
| 110 | + refresh_token: "r", |
| 111 | + }, |
| 112 | + codeVerifier: "v", |
| 113 | + }, home); |
| 114 | + |
| 115 | + const provider = await createOAuthProvider({ |
| 116 | + serverName: "linear", |
| 117 | + redirectUrl: "http://127.0.0.1:62000/callback", |
| 118 | + onAuthURL: () => undefined, |
| 119 | + home, |
| 120 | + }); |
| 121 | + |
| 122 | + // Tokens present → client kept at create. Reset simulates failed refresh. |
| 123 | + await provider.resetAuthorization(); |
| 124 | + expect(await syncValue(provider.tokens())).toBeUndefined(); |
| 125 | + expect(await syncValue(provider.clientInformation())).toBeUndefined(); |
| 126 | + const disk = await loadAuthState("linear", home); |
| 127 | + expect(disk.clientInformation).toBeUndefined(); |
| 128 | + expect(disk.tokens).toBeUndefined(); |
| 129 | + }); |
| 130 | +}); |
0 commit comments