store

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package store persists signed-in accounts to a JSON file.

Index

Constants

View Source
const (
	// FileVersion is the accounts file format written by this build.
	FileVersion = 1
	// HomeEnvVar overrides the directory holding the accounts file.
	HomeEnvVar = "CODEX_USAGE_HOME"
)

Variables

This section is empty.

Functions

func DefaultPath

func DefaultPath(getenv func(string) string) (string, error)

DefaultPath returns $CODEX_USAGE_HOME/accounts.json, or ~/.codex-usage/accounts.json.

Types

type Account

type Account struct {
	Label        string      `json:"label"`
	Email        string      `json:"email,omitempty"`
	PlanType     string      `json:"plan_type,omitempty"`
	UserID       string      `json:"chatgpt_user_id,omitempty"`
	AccountID    string      `json:"chatgpt_account_id,omitempty"`
	FedRAMP      bool        `json:"fedramp,omitempty"`
	Tokens       auth.Tokens `json:"tokens"`
	LastRefresh  time.Time   `json:"last_refresh"`
	AddedAt      time.Time   `json:"added_at"`
	NeedsRelogin bool        `json:"needs_relogin,omitempty"`
	// InCodex marks the account whose session was handed to the Codex CLI
	// with `switch`. Codex's auth.json then holds the same session, and the
	// newer copy wins whenever they differ.
	InCodex bool `json:"in_codex,omitempty"`
}

Account is one signed-in ChatGPT account and workspace.

func (*Account) ApplyIdentity

func (a *Account) ApplyIdentity(id auth.Identity)

ApplyIdentity copies identity claims onto the account.

func (Account) Key

func (a Account) Key() string

Key identifies the account across logins. One user can sign in to several workspaces, so the workspace is part of the key.

type File

type File struct {
	Version  int       `json:"version"`
	Accounts []Account `json:"accounts"`
}

File is the on-disk accounts file.

func (*File) Find

func (f *File) Find(query string) []int

Find resolves a label or email to account positions. An exact label match wins; otherwise every account with that email matches.

func (*File) Index

func (f *File) Index(key string) int

Index returns the position of the account with key, or -1.

func (*File) Remove

func (f *File) Remove(i int)

Remove deletes the account at position i.

func (*File) Upsert

func (f *File) Upsert(a Account, label string) Account

Upsert stores a, replacing any account with the same identity. The existing label is kept unless label is non-empty; labels are made unique. It returns the stored account.

type Store

type Store struct {
	Path string
}

Store reads and writes the accounts file at Path.

func (*Store) Load

func (s *Store) Load() (*File, error)

Load reads the accounts file. A missing file is an empty account list.

func (*Store) Lock

func (s *Store) Lock(ctx context.Context) (unlock func(), err error)

Lock takes an exclusive lock shared by every codex-usage process using this file. Hold it across load-modify-save so two processes never spend the same refresh token.

func (*Store) Save

func (s *Store) Save(f *File) error

Save atomically replaces the accounts file. The file holds credentials, so it is only readable by the owner.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL