@@ -80,6 +80,11 @@ let previousCodexHome: string | undefined;
8080let previousManualImportEnv : string | undefined ;
8181let previousFetch : typeof fetch ;
8282
83+ function jwtWithExp ( exp : number ) : string {
84+ const enc = ( value : unknown ) => Buffer . from ( JSON . stringify ( value ) ) . toString ( "base64url" ) ;
85+ return enc ( { alg : "RS256" , typ : "JWT" } ) + "." + enc ( { exp } ) + ".sig" ;
86+ }
87+
8388function makeConfig ( overrides : Partial < OcxConfig > = { } ) : OcxConfig {
8489 return {
8590 port : 10100 ,
@@ -742,7 +747,9 @@ describe("codex-auth API", () => {
742747 expect ( main ?. needsReauth ) . toBe ( true ) ;
743748 } ) ;
744749
745- test ( "main account 401 marks needsReauth and exposes it in the DTO (#327)" , async ( ) => {
750+ test ( "main account 401 with an undecodable-exp token is terminal and marks needsReauth (#327, #1932)" , async ( ) => {
751+ // "expired-main" is not a decodable JWT, so its exp cannot vouch for liveness.
752+ // Undecodable exp must fail toward reauth: only a decodable future exp counts as live.
746753 writeFileSync ( join ( TEST_CODEX_HOME , "auth.json" ) , JSON . stringify ( {
747754 tokens : { access_token : "expired-main" , account_id : "acct-main" } ,
748755 } ) ) ;
@@ -757,6 +764,54 @@ describe("codex-auth API", () => {
757764 expect ( isAccountNeedsReauth ( MAIN_CODEX_ACCOUNT_ID ) ) . toBe ( true ) ;
758765 } ) ;
759766
767+ test ( "bare main account 401 with a verifiably live token is transient, not reauth (#1932)" , async ( ) => {
768+ writeFileSync ( join ( TEST_CODEX_HOME , "auth.json" ) , JSON . stringify ( {
769+ tokens : { access_token : jwtWithExp ( Math . floor ( Date . now ( ) / 1000 ) + 3600 ) , account_id : "acct-main" } ,
770+ } ) ) ;
771+ globalThis . fetch = ( async ( ) => new Response ( "" , { status : 401 } ) ) as typeof fetch ;
772+
773+ const req = new Request ( "http://localhost/api/codex-auth/accounts?refresh=1" ) ;
774+ const resp = await handleCodexAuthAPI ( req , new URL ( req . url ) , makeConfig ( ) ) ;
775+ const data = await resp ! . json ( ) as { accounts : Array < { id : string ; hasCredential : boolean ; needsReauth ?: boolean } > } ;
776+ const main = data . accounts . find ( account => account . id === MAIN_CODEX_ACCOUNT_ID ) ;
777+
778+ expect ( main ) . toMatchObject ( { hasCredential : true , needsReauth : false } ) ;
779+ expect ( isAccountNeedsReauth ( MAIN_CODEX_ACCOUNT_ID ) ) . toBe ( false ) ;
780+ } ) ;
781+
782+ test ( "main account 401 with a live token but terminal body code is still terminal (#1932)" , async ( ) => {
783+ writeFileSync ( join ( TEST_CODEX_HOME , "auth.json" ) , JSON . stringify ( {
784+ tokens : { access_token : jwtWithExp ( Math . floor ( Date . now ( ) / 1000 ) + 3600 ) , account_id : "acct-main" } ,
785+ } ) ) ;
786+ globalThis . fetch = ( async ( ) => Response . json (
787+ { detail : { code : "invalid_refresh_token" } } ,
788+ { status : 401 } ,
789+ ) ) as typeof fetch ;
790+
791+ const req = new Request ( "http://localhost/api/codex-auth/accounts?refresh=1" ) ;
792+ const resp = await handleCodexAuthAPI ( req , new URL ( req . url ) , makeConfig ( ) ) ;
793+ const data = await resp ! . json ( ) as { accounts : Array < { id : string ; needsReauth ?: boolean } > } ;
794+
795+ expect ( data . accounts . find ( account => account . id === MAIN_CODEX_ACCOUNT_ID ) ?. needsReauth ) . toBe ( true ) ;
796+ expect ( isAccountNeedsReauth ( MAIN_CODEX_ACCOUNT_ID ) ) . toBe ( true ) ;
797+ clearAccountNeedsReauth ( MAIN_CODEX_ACCOUNT_ID ) ;
798+ } ) ;
799+
800+ test ( "main account 401 with an expired access token is terminal (#1932)" , async ( ) => {
801+ writeFileSync ( join ( TEST_CODEX_HOME , "auth.json" ) , JSON . stringify ( {
802+ tokens : { access_token : jwtWithExp ( 1 ) , account_id : "acct-main" } ,
803+ } ) ) ;
804+ globalThis . fetch = ( async ( ) => new Response ( "" , { status : 401 } ) ) as typeof fetch ;
805+
806+ const req = new Request ( "http://localhost/api/codex-auth/accounts?refresh=1" ) ;
807+ const resp = await handleCodexAuthAPI ( req , new URL ( req . url ) , makeConfig ( ) ) ;
808+ const data = await resp ! . json ( ) as { accounts : Array < { id : string ; needsReauth ?: boolean } > } ;
809+
810+ expect ( data . accounts . find ( account => account . id === MAIN_CODEX_ACCOUNT_ID ) ?. needsReauth ) . toBe ( true ) ;
811+ expect ( isAccountNeedsReauth ( MAIN_CODEX_ACCOUNT_ID ) ) . toBe ( true ) ;
812+ clearAccountNeedsReauth ( MAIN_CODEX_ACCOUNT_ID ) ;
813+ } ) ;
814+
760815 test ( "main account invalid-workspace 403 is terminal but a generic 403 is not (#327)" , async ( ) => {
761816 writeFileSync ( join ( TEST_CODEX_HOME , "auth.json" ) , JSON . stringify ( {
762817 tokens : { access_token : "workspace-main" , account_id : "acct-main" } ,
0 commit comments