From aa5690b182eda522b72e5dc61f0c7b3380708426 Mon Sep 17 00:00:00 2001 From: Allen Zhou <46854522+allenzhou101@users.noreply.github.com> Date: Mon, 24 Feb 2025 20:17:08 -0800 Subject: [PATCH] Zod parsing for registration response --- src/server/auth/proxyProvider.test.ts | 34 ++++++++++----------------- src/server/auth/proxyProvider.ts | 4 +++- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/server/auth/proxyProvider.test.ts b/src/server/auth/proxyProvider.test.ts index 8c6106ff..ab73d7b4 100644 --- a/src/server/auth/proxyProvider.test.ts +++ b/src/server/auth/proxyProvider.test.ts @@ -64,6 +64,16 @@ describe("Proxy OAuth Server Provider", () => { }); }); + // Add helper function for failed responses + const mockFailedResponse = () => { + (global.fetch as jest.Mock).mockImplementation(() => + Promise.resolve({ + ok: false, + status: 400, + }) + ); + }; + afterEach(() => { global.fetch = originalFetch; jest.clearAllMocks(); @@ -178,13 +188,7 @@ describe("Proxy OAuth Server Provider", () => { }); it("handles token exchange failure", async () => { - (global.fetch as jest.Mock).mockImplementation(() => - Promise.resolve({ - ok: false, - status: 400, - }) - ); - + mockFailedResponse(); await expect( provider.exchangeAuthorizationCode(validClient, "invalid-code") ).rejects.toThrow(ServerError); @@ -221,13 +225,7 @@ describe("Proxy OAuth Server Provider", () => { }); it("handles registration failure", async () => { - (global.fetch as jest.Mock).mockImplementation(() => - Promise.resolve({ - ok: false, - status: 400, - }) - ); - + mockFailedResponse(); const newClient: OAuthClientInformationFull = { client_id: "new-client", redirect_uris: ["https://new-client.com/callback"], @@ -265,13 +263,7 @@ describe("Proxy OAuth Server Provider", () => { }); it("handles revocation failure", async () => { - (global.fetch as jest.Mock).mockImplementation(() => - Promise.resolve({ - ok: false, - status: 400, - }) - ); - + mockFailedResponse(); await expect( provider.revokeToken!(validClient, { token: "invalid-token", diff --git a/src/server/auth/proxyProvider.ts b/src/server/auth/proxyProvider.ts index 81727548..2b5ad3f2 100644 --- a/src/server/auth/proxyProvider.ts +++ b/src/server/auth/proxyProvider.ts @@ -2,6 +2,7 @@ import { Response } from "express"; import { OAuthRegisteredClientsStore } from "./clients.js"; import { OAuthClientInformationFull, + OAuthClientInformationFullSchema, OAuthTokenRevocationRequest, OAuthTokens, OAuthTokensSchema, @@ -106,7 +107,8 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider { throw new ServerError(`Client registration failed: ${response.status}`); } - return response.json(); + const data = await response.json(); + return OAuthClientInformationFullSchema.parse(data); } }) }