Skip to content

Commit 4891649

Browse files
committed
fix(google-meet): fall back to manual OAuth paste when callback port is occupied
The Google Meet OAuth login binds a fixed localhost:8085 callback listener and let listener failures propagate, so meet auth login aborted entirely when port 8085 was already in use. The sibling Gemini CLI OAuth path already recovers by switching to the manual copy/paste flow on EADDRINUSE/listen errors; bring Google Meet to parity by catching listener errors and reusing the existing manual-paste handler instead of failing the login.
1 parent 3217165 commit 4891649

2 files changed

Lines changed: 117 additions & 19 deletions

File tree

‎extensions/google-meet/src/oauth.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
// Google Meet tests cover oauth plugin behavior.
2+
import { createServer, type Server } from "node:http";
23
import { afterEach, describe, expect, it, vi } from "vitest";
34
import {
45
buildGoogleMeetAuthUrl,
56
refreshGoogleMeetAccessToken,
67
resolveGoogleMeetAccessToken,
8+
waitForGoogleMeetAuthCode,
79
} from "./oauth.js";
810

11+
async function occupyPort(port: number): Promise<Server | null> {
12+
const server = createServer();
13+
try {
14+
await new Promise<void>((resolve, reject) => {
15+
server.once("error", reject);
16+
server.listen(port, "localhost", () => {
17+
resolve();
18+
});
19+
});
20+
} catch (error) {
21+
if (error instanceof Error && error.message.includes("EADDRINUSE")) {
22+
return null;
23+
}
24+
throw error;
25+
}
26+
return server;
27+
}
28+
29+
async function closeServer(server: Server): Promise<void> {
30+
await new Promise<void>((resolve) => {
31+
server.close(() => {
32+
resolve();
33+
});
34+
});
35+
}
36+
937
describe("Google Meet OAuth", () => {
1038
afterEach(() => {
1139
vi.restoreAllMocks();
@@ -161,4 +189,42 @@ describe("Google Meet OAuth", () => {
161189

162190
expect(tokens.expiresAt).toBe(Date.now());
163191
});
192+
193+
it("falls back to manual paste when the local callback port is occupied", async () => {
194+
const blocker = await occupyPort(8085);
195+
try {
196+
const state = "state-token";
197+
const lines: string[] = [];
198+
const code = await waitForGoogleMeetAuthCode({
199+
state,
200+
manual: false,
201+
timeoutMs: 60_000,
202+
authUrl: "https://accounts.google.com/o/oauth2/v2/auth?x=1",
203+
promptInput: async () =>
204+
`http://localhost:8085/oauth2callback?code=auth-code-123&state=${state}`,
205+
writeLine: (message) => lines.push(message),
206+
});
207+
expect(code).toBe("auth-code-123");
208+
expect(lines.some((line) => line.includes("Switching to manual mode"))).toBe(true);
209+
} finally {
210+
if (blocker) {
211+
await closeServer(blocker);
212+
}
213+
}
214+
});
215+
216+
it("propagates non-listener callback failures without manual fallback", async () => {
217+
const promptInput = vi.fn(async () => "unused");
218+
await expect(
219+
waitForGoogleMeetAuthCode({
220+
state: "state-token",
221+
manual: false,
222+
timeoutMs: 1,
223+
authUrl: "https://accounts.google.com/o/oauth2/v2/auth?x=1",
224+
promptInput,
225+
writeLine: () => {},
226+
}),
227+
).rejects.toThrow(/timeout/i);
228+
expect(promptInput).not.toHaveBeenCalled();
229+
});
164230
});

‎extensions/google-meet/src/oauth.ts‎

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,35 @@ export function createGoogleMeetOAuthState(): string {
213213
return generateOAuthState();
214214
}
215215

216+
function isLocalCallbackListenerError(error: unknown): boolean {
217+
if (!(error instanceof Error)) {
218+
return false;
219+
}
220+
return (
221+
error.message.includes("EADDRINUSE") ||
222+
error.message.includes("port") ||
223+
error.message.includes("listen")
224+
);
225+
}
226+
227+
async function readManualGoogleMeetAuthCode(params: {
228+
state: string;
229+
promptInput: (message: string) => Promise<string>;
230+
}): Promise<string> {
231+
const input = await params.promptInput("Paste the full redirect URL here: ");
232+
const parsed = parseOAuthCallbackInput(input, {
233+
missingState: "Missing 'state' parameter. Paste the full redirect URL.",
234+
invalidInput: "Paste the full redirect URL, not just the code.",
235+
});
236+
if ("error" in parsed) {
237+
throw new Error(parsed.error);
238+
}
239+
if (parsed.state !== params.state) {
240+
throw new Error("OAuth state mismatch - please try again");
241+
}
242+
return parsed.code;
243+
}
244+
216245
export async function waitForGoogleMeetAuthCode(params: {
217246
state: string;
218247
manual: boolean;
@@ -223,26 +252,29 @@ export async function waitForGoogleMeetAuthCode(params: {
223252
}): Promise<string> {
224253
params.writeLine(`Open this URL in your browser:\n\n${params.authUrl}\n`);
225254
if (params.manual) {
226-
const input = await params.promptInput("Paste the full redirect URL here: ");
227-
const parsed = parseOAuthCallbackInput(input, {
228-
missingState: "Missing 'state' parameter. Paste the full redirect URL.",
229-
invalidInput: "Paste the full redirect URL, not just the code.",
255+
return await readManualGoogleMeetAuthCode({
256+
state: params.state,
257+
promptInput: params.promptInput,
230258
});
231-
if ("error" in parsed) {
232-
throw new Error(parsed.error);
233-
}
234-
if (parsed.state !== params.state) {
235-
throw new Error("OAuth state mismatch - please try again");
259+
}
260+
try {
261+
const callback = await waitForLocalOAuthCallback({
262+
expectedState: params.state,
263+
timeoutMs: params.timeoutMs,
264+
port: 8085,
265+
callbackPath: "/oauth2callback",
266+
redirectUri: GOOGLE_MEET_REDIRECT_URI,
267+
successTitle: "Google Meet OAuth complete",
268+
});
269+
return callback.code;
270+
} catch (error) {
271+
if (!isLocalCallbackListenerError(error)) {
272+
throw error;
236273
}
237-
return parsed.code;
274+
params.writeLine("Local callback server failed. Switching to manual mode...");
275+
return await readManualGoogleMeetAuthCode({
276+
state: params.state,
277+
promptInput: params.promptInput,
278+
});
238279
}
239-
const callback = await waitForLocalOAuthCallback({
240-
expectedState: params.state,
241-
timeoutMs: params.timeoutMs,
242-
port: 8085,
243-
callbackPath: "/oauth2callback",
244-
redirectUri: GOOGLE_MEET_REDIRECT_URI,
245-
successTitle: "Google Meet OAuth complete",
246-
});
247-
return callback.code;
248280
}

0 commit comments

Comments
 (0)