chatgpt

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package chatgpt is the codex ChatGPT login the gateway sends to chatgpt.com, and its Refresher (gateway.md, "ChatGPT refresh"). One Refresher per supervisor, shared by its gateways, refreshes the access token when its exp is less than 5 minutes away and after an upstream 401, one refresh at a time, and writes the new tokens back to the Store before they are used. The VM never refreshes: its auth.json has placeholders.

The endpoint, the client id, the request and the failure classes are codex's at rust-v0.163.0, the version provision/base.sh pins: codex-rs/login/src/auth/manager.rs (REFRESH_TOKEN_URL, CLIENT_ID, request_chatgpt_token_refresh, classify_refresh_token_failure, RefreshResponse, the 5-minute window and the 8-day interval) and codex-rs/login/src/oauth/client.rs (OAuthClient::refresh, TokenEncoding::Json), with the error code read as codex-rs/login/src/oauth/error.rs reads it. They are unverified against the live service until the owner's smoke run.

No token is ever logged, printed or put in an error: Login redacts, and errors carry the status and an error code from the known list only.

Index

Constants

View Source
const (
	TokenURL = "https://auth.openai.com/oauth/token"
	ClientID = "app_EMoamEEZ73f0CkXaXp7hrann"
)

The refresh endpoint and client id of a login without provenance (codex rust-v0.163.0's REFRESH_TOKEN_URL and CLIENT_ID).

Variables

View Source
var ErrNoRefreshToken = errors.New("chatgpt: the login has no refresh token: factory-agent login codex again")

ErrNoRefreshToken is a login without a refresh token (or with the VM's placeholder): never sent.

View Source
var ErrPermanent = errors.New("chatgpt: the auth server refused the refresh token: log codex in again, then factory-agent login codex")

ErrPermanent is wrapped by a refresh the auth server refused for good (codex's RefreshTokenError::Permanent): the refresher stops trying until the stored login changes.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Store is required.
	Store Store
	// Client sends the refresh; default 30 s, the system roots and no proxy.
	Client *http.Client
	// TokenURL is the endpoint of a login without provenance; default TokenURL (tests).
	TokenURL string
	// Clock defaults to the real clock; Logger to discarding.
	Clock  clock.Clock
	Logger *slog.Logger
}

Config configures a Refresher.

type Login

type Login struct {
	AccessToken, RefreshToken string
	AccountID                 string
	Issuer, ClientID          string
	LastRefresh               time.Time
}

Login is the owner's codex ChatGPT login. Issuer and ClientID are codex's tokens.oauth_provenance, when auth.json has it (codex main records it; rust-v0.163.0 doesn't): a refresh then goes to <Issuer>/oauth/token with ClientID. String, Format, GoString, LogValue, MarshalJSON and MarshalText redact.

func (Login) Format

func (l Login) Format(f fmt.State, _ rune)

Format redacts every verb, %#v included.

func (Login) GoString

func (l Login) GoString() string

GoString redacts.

func (Login) LogValue

func (l Login) LogValue() slog.Value

LogValue redacts.

func (Login) MarshalJSON

func (l Login) MarshalJSON() ([]byte, error)

MarshalJSON is the redacted form as a JSON string.

func (Login) MarshalText

func (l Login) MarshalText() ([]byte, error)

MarshalText is the redacted form.

func (Login) String

func (Login) String() string

type Refresher

type Refresher struct {
	// contains filtered or unexported fields
}

Refresher holds the cached login and refreshes it, one refresh at a time.

func New

func New(cfg Config) (*Refresher, error)

New returns a Refresher; it loads the login at first use.

func (*Refresher) Current

func (r *Refresher) Current(ctx context.Context) (Login, error)

Current returns the cached login, refreshed first when it's due: its access token's exp is less than 5 minutes away, or, without a readable exp, it was last refreshed more than 8 days ago. A failed refresh returns the cached login with the error, so the request still goes out with it. Only a login that can't be loaded returns no token.

func (*Refresher) Err

func (r *Refresher) Err() error

Err is the permanent failure that stopped the refreshes, or nil.

func (*Refresher) Unauthorized

func (r *Refresher) Unauthorized(ctx context.Context, used string) (Login, error)

Unauthorized follows an upstream 401 for the access token used: when the cached token is no longer used, another caller has refreshed it already and it is returned; when the login was refreshed less than minUnauthorizedRefresh ago, the cached login is returned unrefreshed (the gateway then passes the 401 on); otherwise it refreshes.

type Store

type Store interface {
	Load(ctx context.Context) (Login, error)
	Save(ctx context.Context, l Login) error
}

Store is where the login lives (the supervisor's login store). Load's and Save's errors must never hold a token.

Jump to

Keyboard shortcuts

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