|
| 1 | +/** |
| 2 | + * Tests for McpAuth (src/mcp/auth.ts) — credential persistence, URL validation, |
| 3 | + * and token expiry logic. |
| 4 | + * |
| 5 | + * These functions are the security gate for MCP OAuth: getForUrl() prevents |
| 6 | + * credentials from being sent to the wrong server when a user reconfigures |
| 7 | + * their MCP server URL, and isTokenExpired() controls token refresh decisions. |
| 8 | + * |
| 9 | + * Also tests McpOAuthProvider.clientInformation() expiry handling from |
| 10 | + * src/mcp/oauth-provider.ts. |
| 11 | + */ |
| 12 | + |
| 13 | +import { describe, test, expect, afterEach } from "bun:test" |
| 14 | +import { tmpdir } from "../fixture/fixture" |
| 15 | + |
| 16 | +const { McpAuth } = await import("../../src/mcp/auth") |
| 17 | +const { McpOAuthProvider } = await import("../../src/mcp/oauth-provider") |
| 18 | +const { Instance } = await import("../../src/project/instance") |
| 19 | + |
| 20 | +// --------------------------------------------------------------------------- |
| 21 | +// McpAuth.getForUrl — URL validation for credential safety |
| 22 | +// --------------------------------------------------------------------------- |
| 23 | + |
| 24 | +describe("McpAuth.getForUrl", () => { |
| 25 | + test("returns undefined when no entry exists", async () => { |
| 26 | + await using tmp = await tmpdir() |
| 27 | + await Instance.provide({ |
| 28 | + directory: tmp.path, |
| 29 | + fn: async () => { |
| 30 | + const result = await McpAuth.getForUrl("nonexistent-server", "https://example.com/mcp") |
| 31 | + expect(result).toBeUndefined() |
| 32 | + }, |
| 33 | + }) |
| 34 | + }) |
| 35 | + |
| 36 | + test("returns undefined when entry has no serverUrl (old version migration)", async () => { |
| 37 | + await using tmp = await tmpdir() |
| 38 | + await Instance.provide({ |
| 39 | + directory: tmp.path, |
| 40 | + fn: async () => { |
| 41 | + // Write an entry without serverUrl, simulating data from an older version |
| 42 | + await McpAuth.set("legacy-server", { |
| 43 | + tokens: { accessToken: "old-token" }, |
| 44 | + }) |
| 45 | + |
| 46 | + const result = await McpAuth.getForUrl("legacy-server", "https://example.com/mcp") |
| 47 | + expect(result).toBeUndefined() |
| 48 | + }, |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + test("returns undefined when URL has changed (credential safety)", async () => { |
| 53 | + await using tmp = await tmpdir() |
| 54 | + await Instance.provide({ |
| 55 | + directory: tmp.path, |
| 56 | + fn: async () => { |
| 57 | + // Store credentials for server A |
| 58 | + await McpAuth.set( |
| 59 | + "my-mcp", |
| 60 | + { |
| 61 | + tokens: { accessToken: "token-for-server-a" }, |
| 62 | + serverUrl: "https://server-a.example.com/mcp", |
| 63 | + }, |
| 64 | + "https://server-a.example.com/mcp", |
| 65 | + ) |
| 66 | + |
| 67 | + // Try to get credentials for server B — must return undefined |
| 68 | + const result = await McpAuth.getForUrl("my-mcp", "https://server-b.example.com/mcp") |
| 69 | + expect(result).toBeUndefined() |
| 70 | + }, |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + test("returns the entry when URL matches exactly", async () => { |
| 75 | + await using tmp = await tmpdir() |
| 76 | + await Instance.provide({ |
| 77 | + directory: tmp.path, |
| 78 | + fn: async () => { |
| 79 | + const serverUrl = "https://my-server.example.com/mcp" |
| 80 | + await McpAuth.set( |
| 81 | + "my-mcp", |
| 82 | + { |
| 83 | + tokens: { accessToken: "valid-token", refreshToken: "refresh" }, |
| 84 | + serverUrl, |
| 85 | + }, |
| 86 | + serverUrl, |
| 87 | + ) |
| 88 | + |
| 89 | + const result = await McpAuth.getForUrl("my-mcp", serverUrl) |
| 90 | + expect(result).toBeDefined() |
| 91 | + expect(result!.tokens!.accessToken).toBe("valid-token") |
| 92 | + expect(result!.serverUrl).toBe(serverUrl) |
| 93 | + }, |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + test("CRUD lifecycle: set → getForUrl → updateTokens → getForUrl", async () => { |
| 98 | + await using tmp = await tmpdir() |
| 99 | + await Instance.provide({ |
| 100 | + directory: tmp.path, |
| 101 | + fn: async () => { |
| 102 | + const name = "lifecycle-test" |
| 103 | + const url = "https://lifecycle.example.com/mcp" |
| 104 | + |
| 105 | + // Set initial tokens |
| 106 | + await McpAuth.updateTokens(name, { accessToken: "first-token" }, url) |
| 107 | + |
| 108 | + // Verify retrieval |
| 109 | + const entry1 = await McpAuth.getForUrl(name, url) |
| 110 | + expect(entry1).toBeDefined() |
| 111 | + expect(entry1!.tokens!.accessToken).toBe("first-token") |
| 112 | + |
| 113 | + // Update tokens (same URL) |
| 114 | + await McpAuth.updateTokens(name, { accessToken: "second-token" }, url) |
| 115 | + const entry2 = await McpAuth.getForUrl(name, url) |
| 116 | + expect(entry2!.tokens!.accessToken).toBe("second-token") |
| 117 | + |
| 118 | + // Different URL should not return the entry |
| 119 | + const entry3 = await McpAuth.getForUrl(name, "https://different.example.com/mcp") |
| 120 | + expect(entry3).toBeUndefined() |
| 121 | + }, |
| 122 | + }) |
| 123 | + }) |
| 124 | +}) |
| 125 | + |
| 126 | +// --------------------------------------------------------------------------- |
| 127 | +// McpAuth.isTokenExpired — token expiry checking |
| 128 | +// --------------------------------------------------------------------------- |
| 129 | + |
| 130 | +describe("McpAuth.isTokenExpired", () => { |
| 131 | + const originalDateNow = Date.now |
| 132 | + |
| 133 | + afterEach(() => { |
| 134 | + Date.now = originalDateNow |
| 135 | + }) |
| 136 | + |
| 137 | + test("returns null when no tokens exist", async () => { |
| 138 | + await using tmp = await tmpdir() |
| 139 | + await Instance.provide({ |
| 140 | + directory: tmp.path, |
| 141 | + fn: async () => { |
| 142 | + const result = await McpAuth.isTokenExpired("no-such-server") |
| 143 | + expect(result).toBeNull() |
| 144 | + }, |
| 145 | + }) |
| 146 | + }) |
| 147 | + |
| 148 | + test("returns false when tokens have no expiry (never expires)", async () => { |
| 149 | + await using tmp = await tmpdir() |
| 150 | + await Instance.provide({ |
| 151 | + directory: tmp.path, |
| 152 | + fn: async () => { |
| 153 | + await McpAuth.updateTokens("no-expiry", { accessToken: "forever-token" }) |
| 154 | + const result = await McpAuth.isTokenExpired("no-expiry") |
| 155 | + expect(result).toBe(false) |
| 156 | + }, |
| 157 | + }) |
| 158 | + }) |
| 159 | + |
| 160 | + test("returns false when token expiry is in the future", async () => { |
| 161 | + await using tmp = await tmpdir() |
| 162 | + await Instance.provide({ |
| 163 | + directory: tmp.path, |
| 164 | + fn: async () => { |
| 165 | + // expiresAt is in Unix seconds; set to 1 hour from now |
| 166 | + const futureExpiry = Date.now() / 1000 + 3600 |
| 167 | + await McpAuth.updateTokens("valid-token", { |
| 168 | + accessToken: "not-expired", |
| 169 | + expiresAt: futureExpiry, |
| 170 | + }) |
| 171 | + const result = await McpAuth.isTokenExpired("valid-token") |
| 172 | + expect(result).toBe(false) |
| 173 | + }, |
| 174 | + }) |
| 175 | + }) |
| 176 | + |
| 177 | + test("returns true when token has expired", async () => { |
| 178 | + await using tmp = await tmpdir() |
| 179 | + await Instance.provide({ |
| 180 | + directory: tmp.path, |
| 181 | + fn: async () => { |
| 182 | + // expiresAt is in Unix seconds; set to 1 hour ago |
| 183 | + const pastExpiry = Date.now() / 1000 - 3600 |
| 184 | + await McpAuth.updateTokens("expired-token", { |
| 185 | + accessToken: "old-token", |
| 186 | + expiresAt: pastExpiry, |
| 187 | + }) |
| 188 | + const result = await McpAuth.isTokenExpired("expired-token") |
| 189 | + expect(result).toBe(true) |
| 190 | + }, |
| 191 | + }) |
| 192 | + }) |
| 193 | +}) |
| 194 | + |
| 195 | +// --------------------------------------------------------------------------- |
| 196 | +// McpOAuthProvider.clientInformation() — client secret expiry detection |
| 197 | +// --------------------------------------------------------------------------- |
| 198 | + |
| 199 | +describe("McpOAuthProvider.clientInformation", () => { |
| 200 | + const originalDateNow = Date.now |
| 201 | + |
| 202 | + afterEach(() => { |
| 203 | + Date.now = originalDateNow |
| 204 | + }) |
| 205 | + |
| 206 | + test("returns config-based client info when clientId is set", async () => { |
| 207 | + await using tmp = await tmpdir() |
| 208 | + await Instance.provide({ |
| 209 | + directory: tmp.path, |
| 210 | + fn: async () => { |
| 211 | + const provider = new McpOAuthProvider( |
| 212 | + "config-client", |
| 213 | + "https://example.com/mcp", |
| 214 | + { clientId: "my-client-id", clientSecret: "my-secret" }, |
| 215 | + { onRedirect: async () => {} }, |
| 216 | + ) |
| 217 | + |
| 218 | + const info = await provider.clientInformation() |
| 219 | + expect(info).toBeDefined() |
| 220 | + expect(info!.client_id).toBe("my-client-id") |
| 221 | + expect(info!.client_secret).toBe("my-secret") |
| 222 | + }, |
| 223 | + }) |
| 224 | + }) |
| 225 | + |
| 226 | + test("returns stored client info when not expired", async () => { |
| 227 | + await using tmp = await tmpdir() |
| 228 | + await Instance.provide({ |
| 229 | + directory: tmp.path, |
| 230 | + fn: async () => { |
| 231 | + const serverUrl = "https://example.com/mcp" |
| 232 | + |
| 233 | + // Store client info with a future expiry, using the same serverUrl |
| 234 | + await McpAuth.updateClientInfo( |
| 235 | + "stored-client", |
| 236 | + { |
| 237 | + clientId: "dynamic-client-id", |
| 238 | + clientSecret: "dynamic-secret", |
| 239 | + clientSecretExpiresAt: Date.now() / 1000 + 86400, // expires in 24h |
| 240 | + }, |
| 241 | + serverUrl, |
| 242 | + ) |
| 243 | + |
| 244 | + const provider = new McpOAuthProvider( |
| 245 | + "stored-client", |
| 246 | + serverUrl, |
| 247 | + {}, // no config clientId — forces lookup from store |
| 248 | + { onRedirect: async () => {} }, |
| 249 | + ) |
| 250 | + |
| 251 | + const info = await provider.clientInformation() |
| 252 | + expect(info).toBeDefined() |
| 253 | + expect(info!.client_id).toBe("dynamic-client-id") |
| 254 | + expect(info!.client_secret).toBe("dynamic-secret") |
| 255 | + }, |
| 256 | + }) |
| 257 | + }) |
| 258 | + |
| 259 | + test("returns undefined when stored client secret has expired (triggers re-registration)", async () => { |
| 260 | + await using tmp = await tmpdir() |
| 261 | + await Instance.provide({ |
| 262 | + directory: tmp.path, |
| 263 | + fn: async () => { |
| 264 | + const serverUrl = "https://example.com/mcp" |
| 265 | + |
| 266 | + // Store client info with an expiry in the past, using the same serverUrl |
| 267 | + await McpAuth.updateClientInfo( |
| 268 | + "expired-client", |
| 269 | + { |
| 270 | + clientId: "old-client-id", |
| 271 | + clientSecret: "old-secret", |
| 272 | + clientSecretExpiresAt: Date.now() / 1000 - 3600, // expired 1 hour ago |
| 273 | + }, |
| 274 | + serverUrl, |
| 275 | + ) |
| 276 | + |
| 277 | + const provider = new McpOAuthProvider( |
| 278 | + "expired-client", |
| 279 | + serverUrl, |
| 280 | + {}, // no config clientId |
| 281 | + { onRedirect: async () => {} }, |
| 282 | + ) |
| 283 | + |
| 284 | + // Must return undefined so the SDK triggers dynamic registration |
| 285 | + const info = await provider.clientInformation() |
| 286 | + expect(info).toBeUndefined() |
| 287 | + }, |
| 288 | + }) |
| 289 | + }) |
| 290 | + |
| 291 | + test("returns undefined when no stored credentials and no config (dynamic registration)", async () => { |
| 292 | + await using tmp = await tmpdir() |
| 293 | + await Instance.provide({ |
| 294 | + directory: tmp.path, |
| 295 | + fn: async () => { |
| 296 | + const provider = new McpOAuthProvider( |
| 297 | + "brand-new-server", |
| 298 | + "https://brand-new.example.com/mcp", |
| 299 | + {}, // no config |
| 300 | + { onRedirect: async () => {} }, |
| 301 | + ) |
| 302 | + |
| 303 | + const info = await provider.clientInformation() |
| 304 | + expect(info).toBeUndefined() |
| 305 | + }, |
| 306 | + }) |
| 307 | + }) |
| 308 | +}) |
0 commit comments