Skip to content

Commit 2a2392b

Browse files
committed
Correctly pass redirect_uri to tokens call
1 parent 54aa316 commit 2a2392b

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

Diff for: src/client/auth.test.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ describe("OAuth Authorization", () => {
4646
it("returns metadata when first fetch fails but second without MCP header succeeds", async () => {
4747
// Set up a counter to control behavior
4848
let callCount = 0;
49-
49+
5050
// Mock implementation that changes behavior based on call count
5151
mockFetch.mockImplementation((_url, _options) => {
5252
callCount++;
53-
53+
5454
if (callCount === 1) {
5555
// First call with MCP header - fail with TypeError (simulating CORS error)
5656
// We need to use TypeError specifically because that's what the implementation checks for
@@ -68,22 +68,22 @@ describe("OAuth Authorization", () => {
6868
// Should succeed with the second call
6969
const metadata = await discoverOAuthMetadata("https://auth.example.com");
7070
expect(metadata).toEqual(validMetadata);
71-
71+
7272
// Verify both calls were made
7373
expect(mockFetch).toHaveBeenCalledTimes(2);
74-
74+
7575
// Verify first call had MCP header
7676
expect(mockFetch.mock.calls[0][1]?.headers).toHaveProperty("MCP-Protocol-Version");
7777
});
7878

7979
it("throws an error when all fetch attempts fail", async () => {
8080
// Set up a counter to control behavior
8181
let callCount = 0;
82-
82+
8383
// Mock implementation that changes behavior based on call count
8484
mockFetch.mockImplementation((_url, _options) => {
8585
callCount++;
86-
86+
8787
if (callCount === 1) {
8888
// First call - fail with TypeError
8989
return Promise.reject(new TypeError("First failure"));
@@ -96,7 +96,7 @@ describe("OAuth Authorization", () => {
9696
// Should fail with the second error
9797
await expect(discoverOAuthMetadata("https://auth.example.com"))
9898
.rejects.toThrow("Second failure");
99-
99+
100100
// Verify both calls were made
101101
expect(mockFetch).toHaveBeenCalledTimes(2);
102102
});
@@ -250,6 +250,7 @@ describe("OAuth Authorization", () => {
250250
clientInformation: validClientInfo,
251251
authorizationCode: "code123",
252252
codeVerifier: "verifier123",
253+
redirectUri: "http://localhost:3000/callback",
253254
});
254255

255256
expect(tokens).toEqual(validTokens);
@@ -271,6 +272,7 @@ describe("OAuth Authorization", () => {
271272
expect(body.get("code_verifier")).toBe("verifier123");
272273
expect(body.get("client_id")).toBe("client123");
273274
expect(body.get("client_secret")).toBe("secret123");
275+
expect(body.get("redirect_uri")).toBe("http://localhost:3000/callback");
274276
});
275277

276278
it("validates token response schema", async () => {
@@ -288,6 +290,7 @@ describe("OAuth Authorization", () => {
288290
clientInformation: validClientInfo,
289291
authorizationCode: "code123",
290292
codeVerifier: "verifier123",
293+
redirectUri: "http://localhost:3000/callback",
291294
})
292295
).rejects.toThrow();
293296
});
@@ -303,6 +306,7 @@ describe("OAuth Authorization", () => {
303306
clientInformation: validClientInfo,
304307
authorizationCode: "code123",
305308
codeVerifier: "verifier123",
309+
redirectUri: "http://localhost:3000/callback",
306310
})
307311
).rejects.toThrow("Token exchange failed");
308312
});

Diff for: src/client/auth.ts

+4
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export async function auth(
115115
clientInformation,
116116
authorizationCode,
117117
codeVerifier,
118+
redirectUri: provider.redirectUrl,
118119
});
119120

120121
await provider.saveTokens(tokens);
@@ -259,11 +260,13 @@ export async function exchangeAuthorization(
259260
clientInformation,
260261
authorizationCode,
261262
codeVerifier,
263+
redirectUri,
262264
}: {
263265
metadata?: OAuthMetadata;
264266
clientInformation: OAuthClientInformation;
265267
authorizationCode: string;
266268
codeVerifier: string;
269+
redirectUri: string | URL;
267270
},
268271
): Promise<OAuthTokens> {
269272
const grantType = "authorization_code";
@@ -290,6 +293,7 @@ export async function exchangeAuthorization(
290293
client_id: clientInformation.client_id,
291294
code: authorizationCode,
292295
code_verifier: codeVerifier,
296+
redirect_uri: String(redirectUri),
293297
});
294298

295299
if (clientInformation.client_secret) {

0 commit comments

Comments
 (0)