Skip to content

Commit 19bf109

Browse files
committed
fix: multi-account auth flow always runs regardless of inputs parameter
Fixes #12: authorize() was conditionally checking 'inputs' before entering the multi-account while loop. When 'inputs' was undefined (which happens when opencode auth login is called), only single-account flow would run. Changes: - Remove conditional check on inputs in authorize() - Remove duplicate single-account else branch - Update test expectations for multiAccount: true
1 parent 269654b commit 19bf109

4 files changed

Lines changed: 26 additions & 88 deletions

File tree

index.ts

Lines changed: 7 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -827,13 +827,13 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
827827
* @returns Authorization flow configuration
828828
*/
829829
authorize: async (inputs?: Record<string, string>) => {
830-
console.log(`[DEBUG] authorize called, inputs:`, JSON.stringify(inputs));
831-
if (inputs && Object.keys(inputs).length > 0) {
832-
const accounts: TokenSuccess[] = [];
833-
const noBrowser =
834-
inputs.noBrowser === "true" ||
835-
inputs["no-browser"] === "true";
836-
const useManualMode = noBrowser;
830+
// Always use the multi-account flow regardless of inputs
831+
// The inputs parameter is only used for noBrowser flag, not for flow selection
832+
const accounts: TokenSuccess[] = [];
833+
const noBrowser =
834+
inputs?.noBrowser === "true" ||
835+
inputs?.["no-browser"] === "true";
836+
const useManualMode = noBrowser;
837837

838838
let startFresh = true;
839839
const existingStorage = await hydrateEmails(await loadAccounts());
@@ -974,71 +974,6 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
974974
method: "auto",
975975
callback: async () => primary,
976976
};
977-
}
978-
979-
let startFresh = true;
980-
const existingStorage = await hydrateEmails(await loadAccounts());
981-
if (existingStorage && existingStorage.accounts.length > 0) {
982-
const existingAccounts = existingStorage.accounts.map((account, index) => ({
983-
accountId: account.accountId,
984-
email: account.email,
985-
index,
986-
}));
987-
const loginMode = await promptLoginMode(existingAccounts);
988-
startFresh = loginMode === "fresh";
989-
if (startFresh) {
990-
console.log("\nStarting fresh - existing accounts will be replaced.\n");
991-
} else {
992-
console.log("\nAdding to existing accounts.\n");
993-
}
994-
}
995-
996-
const { pkce, state, url } = await createAuthorizationFlow();
997-
let serverInfo: Awaited<ReturnType<typeof startLocalOAuthServer>> | null =
998-
null;
999-
try {
1000-
serverInfo = await startLocalOAuthServer({ state });
1001-
} catch (err) {
1002-
logDebug(`[${PLUGIN_NAME}] Failed to start OAuth server for add flow: ${(err as Error)?.message ?? String(err)}`);
1003-
serverInfo = null;
1004-
}
1005-
1006-
openBrowserUrl(url);
1007-
1008-
if (!serverInfo || !serverInfo.ready) {
1009-
serverInfo?.close();
1010-
return buildManualOAuthFlow(pkce, url, async (tokens) => {
1011-
await persistAccountPool([tokens], startFresh);
1012-
});
1013-
}
1014-
1015-
return {
1016-
url,
1017-
method: "auto" as const,
1018-
instructions: AUTH_LABELS.INSTRUCTIONS,
1019-
callback: async () => {
1020-
const result = await serverInfo.waitForCode(state);
1021-
serverInfo.close();
1022-
1023-
if (!result) {
1024-
return { type: "failed" as const };
1025-
}
1026-
1027-
const tokens = await exchangeAuthorizationCode(
1028-
result.code,
1029-
pkce.verifier,
1030-
REDIRECT_URI,
1031-
);
1032-
1033-
if (tokens?.type === "success") {
1034-
await persistAccountPool([tokens], startFresh);
1035-
}
1036-
1037-
return tokens?.type === "success"
1038-
? tokens
1039-
: { type: "failed" as const };
1040-
},
1041-
};
1042977
},
1043978
},
1044979
{

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "oc-chatgpt-multi-auth",
3-
"version": "4.9.0",
3+
"version": "4.9.1",
44
"description": "Multi-account rotation plugin for ChatGPT Plus/Pro (OAuth / Codex backend)",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

test/auth.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,14 @@ describe('Auth Module', () => {
172172

173173
try {
174174
const result = await refreshAccessToken('existing-refresh');
175-
expect(result).toEqual({
176-
type: 'success',
177-
access: 'new-access',
178-
refresh: 'existing-refresh',
179-
expires: 61_000,
180-
});
175+
expect(result).toEqual({
176+
type: 'success',
177+
access: 'new-access',
178+
refresh: 'existing-refresh',
179+
expires: 61_000,
180+
idToken: undefined,
181+
multiAccount: true,
182+
});
181183
} finally {
182184
globalThis.fetch = originalFetch;
183185
vi.restoreAllMocks();

test/fetch-helpers.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,16 @@ describe('Fetch Helpers Module', () => {
8282

8383
const updated = await refreshAndUpdateToken(auth, client);
8484

85-
expect(client.auth.set).toHaveBeenCalledWith({
86-
path: { id: 'openai' },
87-
body: {
88-
type: 'oauth',
89-
access: 'new',
90-
refresh: 'newr',
91-
expires: 123,
92-
},
93-
});
85+
expect(client.auth.set).toHaveBeenCalledWith({
86+
path: { id: 'openai' },
87+
body: {
88+
type: 'oauth',
89+
access: 'new',
90+
refresh: 'newr',
91+
expires: 123,
92+
multiAccount: true,
93+
},
94+
});
9495
expect(updated.access).toBe('new');
9596
expect(updated.refresh).toBe('newr');
9697
expect(updated.expires).toBe(123);

0 commit comments

Comments
 (0)