@@ -21,7 +21,9 @@ const { getOAuthApiKeyMock } = vi.hoisted(() => ({
2121} ) ) ;
2222
2323const { readCodexCliCredentialsCachedMock } = vi . hoisted ( ( ) => ( {
24- readCodexCliCredentialsCachedMock : vi . fn < ( ) => OAuthCredential | null > ( ( ) => null ) ,
24+ readCodexCliCredentialsCachedMock : vi . fn < ( _options ?: unknown ) => OAuthCredential | null > (
25+ ( ) => null ,
26+ ) ,
2527} ) ) ;
2628
2729const {
@@ -462,6 +464,183 @@ describe("resolveApiKeyForProfile openai-codex refresh fallback", () => {
462464 expect ( persisted . profiles [ profileId ] ) . not . toHaveProperty ( "refresh" ) ;
463465 } ) ;
464466
467+ it ( "uses same-account Codex CLI credentials after forced local refresh fails" , async ( ) => {
468+ const profileId = "openai-codex:default" ;
469+ saveAuthProfileStore (
470+ {
471+ version : 1 ,
472+ profiles : {
473+ [ profileId ] : {
474+ type : "oauth" ,
475+ provider : "openai-codex" ,
476+ access : "local-access-token" ,
477+ refresh : "local-refresh-token" ,
478+ expires : Date . now ( ) + 86_400_000 ,
479+ accountId : "acct-shared" ,
480+ } ,
481+ } ,
482+ } ,
483+ agentDir ,
484+ ) ;
485+ readCodexCliCredentialsCachedMock . mockReturnValue ( {
486+ type : "oauth" ,
487+ provider : "openai-codex" ,
488+ access : "codex-cli-access-token" ,
489+ refresh : "codex-cli-refresh-token" ,
490+ expires : Date . now ( ) + 86_400_000 ,
491+ accountId : "acct-shared" ,
492+ } ) ;
493+ refreshProviderOAuthCredentialWithPluginMock . mockImplementationOnce ( async ( ) => {
494+ throw new Error (
495+ '401 {"error":{"message":"Your refresh token is expired.","code":"refresh_token_expired"}}' ,
496+ ) ;
497+ } ) ;
498+
499+ await expect (
500+ resolveApiKeyForProfile ( {
501+ store : ensureAuthProfileStore ( agentDir ) ,
502+ profileId,
503+ agentDir,
504+ forceRefresh : true ,
505+ } ) ,
506+ ) . resolves . toEqual ( {
507+ apiKey : "codex-cli-access-token" ,
508+ provider : "openai-codex" ,
509+ email : undefined ,
510+ } ) ;
511+
512+ expect ( readCodexCliCredentialsCachedMock ) . toHaveBeenCalledWith ( {
513+ ttlMs : expect . any ( Number ) ,
514+ allowKeychainPrompt : false ,
515+ } ) ;
516+ const persisted = await readPersistedStore ( agentDir ) ;
517+ const persistedProfile = requireOAuthProfile ( persisted , profileId ) ;
518+ expect ( persistedProfile . accountId ) . toBe ( "acct-shared" ) ;
519+ expect ( persistedProfile ) . not . toHaveProperty ( "access" ) ;
520+ expect ( persistedProfile ) . not . toHaveProperty ( "refresh" ) ;
521+ expect ( JSON . stringify ( persisted ) ) . not . toContain ( "codex-cli-access-token" ) ;
522+ expect ( JSON . stringify ( persisted ) ) . not . toContain ( "codex-cli-refresh-token" ) ;
523+ } ) ;
524+
525+ it ( "rejects mismatched Codex CLI fallback after forced local refresh fails" , async ( ) => {
526+ const profileId = "openai-codex:default" ;
527+ saveAuthProfileStore (
528+ {
529+ version : 1 ,
530+ profiles : {
531+ [ profileId ] : {
532+ type : "oauth" ,
533+ provider : "openai-codex" ,
534+ access : "local-access-token" ,
535+ refresh : "local-refresh-token" ,
536+ expires : Date . now ( ) + 86_400_000 ,
537+ accountId : "acct-local" ,
538+ } ,
539+ } ,
540+ } ,
541+ agentDir ,
542+ ) ;
543+ readCodexCliCredentialsCachedMock . mockReturnValue ( {
544+ type : "oauth" ,
545+ provider : "openai-codex" ,
546+ access : "codex-cli-access-token" ,
547+ refresh : "codex-cli-refresh-token" ,
548+ expires : Date . now ( ) + 86_400_000 ,
549+ accountId : "acct-other" ,
550+ } ) ;
551+ refreshProviderOAuthCredentialWithPluginMock . mockImplementationOnce ( async ( ) => {
552+ throw new Error (
553+ '401 {"error":{"message":"Your refresh token is expired.","code":"refresh_token_expired"}}' ,
554+ ) ;
555+ } ) ;
556+
557+ await expect (
558+ resolveApiKeyForProfile ( {
559+ store : ensureAuthProfileStore ( agentDir ) ,
560+ profileId,
561+ agentDir,
562+ forceRefresh : true ,
563+ } ) ,
564+ ) . rejects . toThrow ( / O A u t h t o k e n r e f r e s h f a i l e d f o r o p e n a i - c o d e x / ) ;
565+ } ) ;
566+
567+ it ( "rejects identity-less Codex CLI fallback after forced local refresh fails" , async ( ) => {
568+ const profileId = "openai-codex:default" ;
569+ saveAuthProfileStore (
570+ {
571+ version : 1 ,
572+ profiles : {
573+ [ profileId ] : {
574+ type : "oauth" ,
575+ provider : "openai-codex" ,
576+ access : "local-access-token" ,
577+ refresh : "local-refresh-token" ,
578+ expires : Date . now ( ) + 86_400_000 ,
579+ } ,
580+ } ,
581+ } ,
582+ agentDir ,
583+ ) ;
584+ readCodexCliCredentialsCachedMock . mockReturnValue ( {
585+ type : "oauth" ,
586+ provider : "openai-codex" ,
587+ access : "codex-cli-access-token" ,
588+ refresh : "codex-cli-refresh-token" ,
589+ expires : Date . now ( ) + 86_400_000 ,
590+ accountId : "acct-cli" ,
591+ } ) ;
592+ refreshProviderOAuthCredentialWithPluginMock . mockImplementationOnce ( async ( ) => {
593+ throw new Error (
594+ '401 {"error":{"message":"Your refresh token is expired.","code":"refresh_token_expired"}}' ,
595+ ) ;
596+ } ) ;
597+
598+ await expect (
599+ resolveApiKeyForProfile ( {
600+ store : ensureAuthProfileStore ( agentDir ) ,
601+ profileId,
602+ agentDir,
603+ forceRefresh : true ,
604+ } ) ,
605+ ) . rejects . toThrow ( / O A u t h t o k e n r e f r e s h f a i l e d f o r o p e n a i - c o d e x / ) ;
606+ } ) ;
607+
608+ it ( "rejects unchanged Codex CLI fallback during forced refresh" , async ( ) => {
609+ const profileId = "openai-codex:default" ;
610+ const credential : OAuthCredential = {
611+ type : "oauth" ,
612+ provider : "openai-codex" ,
613+ access : "shared-access-token" ,
614+ refresh : "shared-refresh-token" ,
615+ expires : Date . now ( ) + 86_400_000 ,
616+ accountId : "acct-shared" ,
617+ } ;
618+ saveAuthProfileStore (
619+ {
620+ version : 1 ,
621+ profiles : {
622+ [ profileId ] : credential ,
623+ } ,
624+ } ,
625+ agentDir ,
626+ ) ;
627+ readCodexCliCredentialsCachedMock . mockReturnValue ( { ...credential } ) ;
628+ refreshProviderOAuthCredentialWithPluginMock . mockImplementationOnce ( async ( ) => {
629+ throw new Error (
630+ '401 {"error":{"message":"Your refresh token is expired.","code":"refresh_token_expired"}}' ,
631+ ) ;
632+ } ) ;
633+
634+ await expect (
635+ resolveApiKeyForProfile ( {
636+ store : ensureAuthProfileStore ( agentDir ) ,
637+ profileId,
638+ agentDir,
639+ forceRefresh : true ,
640+ } ) ,
641+ ) . rejects . toThrow ( / O A u t h t o k e n r e f r e s h f a i l e d f o r o p e n a i - c o d e x / ) ;
642+ } ) ;
643+
465644 it ( "adopts fresher stored credentials after refresh_token_reused" , async ( ) => {
466645 const profileId = "openai-codex:default" ;
467646 saveAuthProfileStore (
0 commit comments