MCPcopy Index your code
hub / github.com/arctic-cli/interface / ensureValidTokenFor

Function ensureValidTokenFor

packages/arctic/src/auth/codex.ts:403–428  ·  view source on GitHub ↗
(
    authKey: string,
    auth: Extract<Auth.Info, { type: "codex" }>,
  )

Source from the content-addressed store, hash-verified

401 }
402
403 export async function ensureValidTokenFor(
404 authKey: string,
405 auth: Extract<Auth.Info, { type: "codex" }>,
406 ): Promise<string> {
407 // Refresh 5 minutes before expiration to be safe
408 if (Date.now() > auth.expiresAt - 5 * 60 * 1000) {
409 try {
410 const tokens = await refresh(auth.refreshToken)
411 const idTokenInfo = parseIdToken(tokens.id_token)
412 const expiresIn = tokens.expires_in ?? 3600
413
414 await Auth.set(authKey, {
415 ...auth,
416 accessToken: tokens.access_token,
417 refreshToken: tokens.refresh_token,
418 idToken: tokens.id_token,
419 expiresAt: Date.now() + expiresIn * 1000,
420 accountId: idTokenInfo?.chatgpt_account_id ?? auth.accountId,
421 })
422 return tokens.access_token
423 } catch (e) {
424 throw new Error("Session expired, please login again")
425 }
426 }
427 return auth.accessToken
428 }
429
430 export async function ensureValidToken(): Promise<string> {
431 const auth = await Auth.get("codex")

Callers 1

ensureValidTokenFunction · 0.85

Calls 3

parseIdTokenFunction · 0.85
refreshFunction · 0.70
setMethod · 0.45

Tested by

no test coverage detected