@@ -118,6 +118,11 @@ interface TokenResponse {
118118 expires_in ?: number
119119}
120120
121+ interface CodexAuthPluginOptions {
122+ issuer ?: string
123+ codexApiEndpoint ?: string
124+ }
125+
121126async function exchangeCodeForTokens ( code : string , redirectUri : string , pkce : PkceCodes ) : Promise < TokenResponse > {
122127 const response = await fetch ( `${ ISSUER } /oauth/token` , {
123128 method : "POST" ,
@@ -136,8 +141,8 @@ async function exchangeCodeForTokens(code: string, redirectUri: string, pkce: Pk
136141 return response . json ( )
137142}
138143
139- async function refreshAccessToken ( refreshToken : string ) : Promise < TokenResponse > {
140- const response = await fetch ( `${ ISSUER } /oauth/token` , {
144+ async function refreshAccessToken ( refreshToken : string , issuer = ISSUER ) : Promise < TokenResponse > {
145+ const response = await fetch ( `${ issuer } /oauth/token` , {
141146 method : "POST" ,
142147 headers : { "Content-Type" : "application/x-www-form-urlencoded" } ,
143148 body : new URLSearchParams ( {
@@ -364,7 +369,10 @@ function waitForOAuthCallback(pkce: PkceCodes, state: string): Promise<TokenResp
364369 } )
365370}
366371
367- export async function CodexAuthPlugin ( input : PluginInput ) : Promise < Hooks > {
372+ export async function CodexAuthPlugin ( input : PluginInput , options : CodexAuthPluginOptions = { } ) : Promise < Hooks > {
373+ const issuer = options . issuer ?? ISSUER
374+ const codexApiEndpoint = options . codexApiEndpoint ?? CODEX_API_ENDPOINT
375+
368376 return {
369377 provider : {
370378 id : "openai" ,
@@ -405,6 +413,13 @@ export async function CodexAuthPlugin(input: PluginInput): Promise<Hooks> {
405413 const auth = await getAuth ( )
406414 if ( auth . type !== "oauth" ) return { }
407415
416+ let refreshPromise :
417+ | Promise < {
418+ access : string
419+ accountId : string | undefined
420+ } >
421+ | undefined
422+
408423 return {
409424 apiKey : OAUTH_DUMMY_KEY ,
410425 async fetch ( requestInput : RequestInfo | URL , init ?: RequestInit ) {
@@ -429,21 +444,34 @@ export async function CodexAuthPlugin(input: PluginInput): Promise<Hooks> {
429444
430445 // Check if token needs refresh
431446 if ( ! currentAuth . access || currentAuth . expires < Date . now ( ) ) {
432- log . info ( "refreshing codex access token" )
433- const tokens = await refreshAccessToken ( currentAuth . refresh )
434- const newAccountId = extractAccountId ( tokens ) || authWithAccount . accountId
435- await input . client . auth . set ( {
436- path : { id : "openai" } ,
437- body : {
438- type : "oauth" ,
439- refresh : tokens . refresh_token ,
440- access : tokens . access_token ,
441- expires : Date . now ( ) + ( tokens . expires_in ?? 3600 ) * 1000 ,
442- ...( newAccountId && { accountId : newAccountId } ) ,
443- } ,
444- } )
445- currentAuth . access = tokens . access_token
446- authWithAccount . accountId = newAccountId
447+ if ( ! refreshPromise ) {
448+ log . info ( "refreshing codex access token" )
449+ refreshPromise = refreshAccessToken ( currentAuth . refresh , issuer )
450+ . then ( async ( tokens ) => {
451+ const accountId = extractAccountId ( tokens ) || authWithAccount . accountId
452+ await input . client . auth . set ( {
453+ path : { id : "openai" } ,
454+ body : {
455+ type : "oauth" ,
456+ refresh : tokens . refresh_token ,
457+ access : tokens . access_token ,
458+ expires : Date . now ( ) + ( tokens . expires_in ?? 3600 ) * 1000 ,
459+ ...( accountId && { accountId } ) ,
460+ } ,
461+ } )
462+ return {
463+ access : tokens . access_token ,
464+ accountId,
465+ }
466+ } )
467+ . finally ( ( ) => {
468+ refreshPromise = undefined
469+ } )
470+ }
471+
472+ const refreshed = await refreshPromise
473+ currentAuth . access = refreshed . access
474+ authWithAccount . accountId = refreshed . accountId
447475 }
448476
449477 // Build headers
@@ -477,7 +505,7 @@ export async function CodexAuthPlugin(input: PluginInput): Promise<Hooks> {
477505 : new URL ( typeof requestInput === "string" ? requestInput : requestInput . url )
478506 const url =
479507 parsed . pathname . includes ( "/v1/responses" ) || parsed . pathname . includes ( "/chat/completions" )
480- ? new URL ( CODEX_API_ENDPOINT )
508+ ? new URL ( codexApiEndpoint )
481509 : parsed
482510
483511 return fetch ( url , {
0 commit comments