|
1 | | -import { discoverScopes } from "../auth"; |
| 1 | +import { discoverScopes, revokeTokens } from "../auth"; |
2 | 2 | import { discoverAuthorizationServerMetadata } from "@modelcontextprotocol/sdk/client/auth.js"; |
| 3 | +import { SESSION_KEYS, getServerSpecificKey } from "../constants"; |
3 | 4 |
|
4 | 5 | jest.mock("@modelcontextprotocol/sdk/client/auth.js", () => ({ |
5 | 6 | discoverAuthorizationServerMetadata: jest.fn(), |
@@ -156,3 +157,183 @@ describe("discoverScopes", () => { |
156 | 157 | }, |
157 | 158 | ); |
158 | 159 | }); |
| 160 | + |
| 161 | +describe("revokeTokens", () => { |
| 162 | + const serverUrl = "https://example.com"; |
| 163 | + const revocationEndpoint = "https://test.com/revoke"; |
| 164 | + const metadataWithRevocation = { |
| 165 | + ...baseMetadata, |
| 166 | + revocation_endpoint: revocationEndpoint, |
| 167 | + }; |
| 168 | + |
| 169 | + const seedTokens = (tokens: { |
| 170 | + access_token: string; |
| 171 | + token_type?: string; |
| 172 | + refresh_token?: string; |
| 173 | + }) => { |
| 174 | + sessionStorage.setItem( |
| 175 | + getServerSpecificKey(SESSION_KEYS.TOKENS, serverUrl), |
| 176 | + JSON.stringify({ token_type: "Bearer", ...tokens }), |
| 177 | + ); |
| 178 | + }; |
| 179 | + |
| 180 | + const seedClientInfo = ( |
| 181 | + client_id: string, |
| 182 | + { isPreregistered = false } = {}, |
| 183 | + ) => { |
| 184 | + const key = getServerSpecificKey( |
| 185 | + isPreregistered |
| 186 | + ? SESSION_KEYS.PREREGISTERED_CLIENT_INFORMATION |
| 187 | + : SESSION_KEYS.CLIENT_INFORMATION, |
| 188 | + serverUrl, |
| 189 | + ); |
| 190 | + sessionStorage.setItem(key, JSON.stringify({ client_id })); |
| 191 | + }; |
| 192 | + |
| 193 | + const parseRevokeBody = (call: [unknown, RequestInit | undefined]) => { |
| 194 | + const init = call[1]; |
| 195 | + return new URLSearchParams(init?.body as string); |
| 196 | + }; |
| 197 | + |
| 198 | + let warnSpy: jest.SpyInstance; |
| 199 | + let debugSpy: jest.SpyInstance; |
| 200 | + |
| 201 | + beforeEach(() => { |
| 202 | + jest.clearAllMocks(); |
| 203 | + sessionStorage.clear(); |
| 204 | + warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); |
| 205 | + debugSpy = jest.spyOn(console, "debug").mockImplementation(() => {}); |
| 206 | + }); |
| 207 | + |
| 208 | + afterEach(() => { |
| 209 | + warnSpy.mockRestore(); |
| 210 | + debugSpy.mockRestore(); |
| 211 | + }); |
| 212 | + |
| 213 | + it("posts refresh_token to revocation_endpoint when available, includes client_id", async () => { |
| 214 | + mockDiscoverAuth.mockResolvedValue(metadataWithRevocation); |
| 215 | + seedTokens({ access_token: "at-123", refresh_token: "rt-456" }); |
| 216 | + seedClientInfo("client-xyz"); |
| 217 | + const fetchFn = jest |
| 218 | + .fn<Promise<Response>, [RequestInfo | URL, RequestInit?]>() |
| 219 | + .mockResolvedValue(new Response(null, { status: 200 })); |
| 220 | + |
| 221 | + await revokeTokens({ serverUrl, fetchFn }); |
| 222 | + |
| 223 | + expect(fetchFn).toHaveBeenCalledTimes(1); |
| 224 | + const [url, init] = fetchFn.mock.calls[0]; |
| 225 | + expect(url).toBe(revocationEndpoint); |
| 226 | + expect(init?.method).toBe("POST"); |
| 227 | + expect((init?.headers as Record<string, string>)["Content-Type"]).toBe( |
| 228 | + "application/x-www-form-urlencoded", |
| 229 | + ); |
| 230 | + const body = parseRevokeBody(fetchFn.mock.calls[0]); |
| 231 | + expect(body.get("token")).toBe("rt-456"); |
| 232 | + expect(body.get("token_type_hint")).toBe("refresh_token"); |
| 233 | + expect(body.get("client_id")).toBe("client-xyz"); |
| 234 | + }); |
| 235 | + |
| 236 | + it("prefers preregistered client_id over dynamic", async () => { |
| 237 | + mockDiscoverAuth.mockResolvedValue(metadataWithRevocation); |
| 238 | + seedTokens({ access_token: "at-123", refresh_token: "rt-456" }); |
| 239 | + seedClientInfo("dynamic-client"); |
| 240 | + seedClientInfo("preregistered-client", { isPreregistered: true }); |
| 241 | + const fetchFn = jest |
| 242 | + .fn<Promise<Response>, [RequestInfo | URL, RequestInit?]>() |
| 243 | + .mockResolvedValue(new Response(null, { status: 200 })); |
| 244 | + |
| 245 | + await revokeTokens({ serverUrl, fetchFn }); |
| 246 | + |
| 247 | + const body = parseRevokeBody(fetchFn.mock.calls[0]); |
| 248 | + expect(body.get("client_id")).toBe("preregistered-client"); |
| 249 | + }); |
| 250 | + |
| 251 | + it("falls back to access_token when no refresh_token is present", async () => { |
| 252 | + mockDiscoverAuth.mockResolvedValue(metadataWithRevocation); |
| 253 | + seedTokens({ access_token: "at-only" }); |
| 254 | + const fetchFn = jest |
| 255 | + .fn<Promise<Response>, [RequestInfo | URL, RequestInit?]>() |
| 256 | + .mockResolvedValue(new Response(null, { status: 200 })); |
| 257 | + |
| 258 | + await revokeTokens({ serverUrl, fetchFn }); |
| 259 | + |
| 260 | + expect(fetchFn).toHaveBeenCalledTimes(1); |
| 261 | + const body = parseRevokeBody(fetchFn.mock.calls[0]); |
| 262 | + expect(body.get("token")).toBe("at-only"); |
| 263 | + expect(body.get("token_type_hint")).toBe("access_token"); |
| 264 | + expect(body.get("client_id")).toBeNull(); |
| 265 | + }); |
| 266 | + |
| 267 | + it("no-ops when no tokens are stored", async () => { |
| 268 | + const fetchFn = jest.fn< |
| 269 | + Promise<Response>, |
| 270 | + [RequestInfo | URL, RequestInit?] |
| 271 | + >(); |
| 272 | + |
| 273 | + await revokeTokens({ serverUrl, fetchFn }); |
| 274 | + |
| 275 | + expect(fetchFn).not.toHaveBeenCalled(); |
| 276 | + expect(mockDiscoverAuth).not.toHaveBeenCalled(); |
| 277 | + }); |
| 278 | + |
| 279 | + it("no-ops when AS metadata has no revocation_endpoint", async () => { |
| 280 | + mockDiscoverAuth.mockResolvedValue(baseMetadata); |
| 281 | + seedTokens({ access_token: "at-123", refresh_token: "rt-456" }); |
| 282 | + const fetchFn = jest.fn< |
| 283 | + Promise<Response>, |
| 284 | + [RequestInfo | URL, RequestInit?] |
| 285 | + >(); |
| 286 | + |
| 287 | + await revokeTokens({ serverUrl, fetchFn }); |
| 288 | + |
| 289 | + expect(fetchFn).not.toHaveBeenCalled(); |
| 290 | + }); |
| 291 | + |
| 292 | + it("swallows fetch rejection and logs a warning", async () => { |
| 293 | + mockDiscoverAuth.mockResolvedValue(metadataWithRevocation); |
| 294 | + seedTokens({ access_token: "at-123", refresh_token: "rt-456" }); |
| 295 | + const fetchFn = jest |
| 296 | + .fn<Promise<Response>, [RequestInfo | URL, RequestInit?]>() |
| 297 | + .mockRejectedValue(new Error("network down")); |
| 298 | + |
| 299 | + await expect(revokeTokens({ serverUrl, fetchFn })).resolves.toBeUndefined(); |
| 300 | + expect(warnSpy).toHaveBeenCalledWith( |
| 301 | + "Token revocation failed (best-effort):", |
| 302 | + expect.any(Error), |
| 303 | + ); |
| 304 | + }); |
| 305 | + |
| 306 | + it("treats non-2xx response as a soft failure without throwing", async () => { |
| 307 | + mockDiscoverAuth.mockResolvedValue(metadataWithRevocation); |
| 308 | + seedTokens({ access_token: "at-123", refresh_token: "rt-456" }); |
| 309 | + const fetchFn = jest |
| 310 | + .fn<Promise<Response>, [RequestInfo | URL, RequestInit?]>() |
| 311 | + .mockResolvedValue( |
| 312 | + new Response("nope", { status: 400, statusText: "Bad Request" }), |
| 313 | + ); |
| 314 | + |
| 315 | + await expect(revokeTokens({ serverUrl, fetchFn })).resolves.toBeUndefined(); |
| 316 | + expect(warnSpy).toHaveBeenCalledWith( |
| 317 | + expect.stringContaining("Token revocation responded 400"), |
| 318 | + ); |
| 319 | + }); |
| 320 | + |
| 321 | + it("uses the provided fetchFn, not the global fetch", async () => { |
| 322 | + mockDiscoverAuth.mockResolvedValue(metadataWithRevocation); |
| 323 | + seedTokens({ access_token: "at-123", refresh_token: "rt-456" }); |
| 324 | + const globalFetchSpy = jest |
| 325 | + .spyOn(globalThis, "fetch") |
| 326 | + .mockResolvedValue(new Response(null, { status: 200 })); |
| 327 | + const fetchFn = jest |
| 328 | + .fn<Promise<Response>, [RequestInfo | URL, RequestInit?]>() |
| 329 | + .mockResolvedValue(new Response(null, { status: 200 })); |
| 330 | + |
| 331 | + try { |
| 332 | + await revokeTokens({ serverUrl, fetchFn }); |
| 333 | + expect(fetchFn).toHaveBeenCalledTimes(1); |
| 334 | + expect(globalFetchSpy).not.toHaveBeenCalled(); |
| 335 | + } finally { |
| 336 | + globalFetchSpy.mockRestore(); |
| 337 | + } |
| 338 | + }); |
| 339 | +}); |
0 commit comments