codex

package
v1.1.9 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package codex provides authentication and token management for OpenAI's Codex API. It handles token refreshing for scheduler-managed Codex credentials.

Index

Constants

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

OAuth configuration constants for OpenAI Codex refresh.

View Source
const RefreshOutcomeMetadataKey = "codex_refresh_outcomes"

RefreshOutcomeMetadataKey stores unresolved exchanges without persisting tokens or error bodies.

Variables

This section is empty.

Functions

func IsRefreshOutcomeUnknown added in v1.1.9

func IsRefreshOutcomeUnknown(err error) bool

IsRefreshOutcomeUnknown distinguishes unresolved exchanges from terminal rejection.

func MergeRefreshOutcomes added in v1.1.9

func MergeRefreshOutcomes(metadata, source map[string]any)

MergeRefreshOutcomes adopts persisted fences without overwriting newer grants.

func PreserveRefreshOutcome added in v1.1.9

func PreserveRefreshOutcome(ctx context.Context, token string, outcome error) error

PreserveRefreshOutcome migrates an existing metadata fence without overwriting a newer journal result. A completed owner receipt can recover stale metadata.

func RecordRefreshOutcomeUnknown added in v1.1.9

func RecordRefreshOutcomeUnknown(metadata map[string]any, err error) bool

RecordRefreshOutcomeUnknown applies only failures of the tokens still present in metadata, so a stale refresh cannot quarantine replacement credentials. It returns whether the error describes a current grant, even if already recorded.

func RefreshOutcomeUnknown added in v1.1.9

func RefreshOutcomeUnknown(metadata map[string]any, siwc bool) error

RefreshOutcomeUnknown returns a persisted fence only for the current grant. Replacing one partition does not release the other partition's fence. Loading the fence also prevents other in-process callers from replaying it.

func WithRefreshJournal added in v1.1.9

func WithRefreshJournal(ctx context.Context, journal RefreshJournal) context.Context

WithRefreshJournal binds refreshes to the authoritative credential database.

Types

type CodexAuth

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

CodexAuth handles the OpenAI OAuth2 authentication flow. It manages the HTTP client and provides methods for generating authorization URLs, exchanging authorization codes for tokens, and refreshing access tokens.

func NewCodexAuthWithProxyURL

func NewCodexAuthWithProxyURL(cfg *config.Config, proxyURL string) *CodexAuth

NewCodexAuthWithProxyURL creates a new CodexAuth service instance. proxyURL takes precedence over cfg.ProxyURL when non-empty.

func (*CodexAuth) RefreshTokens

func (o *CodexAuth) RefreshTokens(ctx context.Context, refreshToken string) (*CodexTokenData, error)

RefreshTokens refreshes an access token using a refresh token. This method is called when an access token has expired. It makes a request to the token endpoint to obtain a new set of tokens.

func (*CodexAuth) RefreshTokensWithRetry

func (o *CodexAuth) RefreshTokensWithRetry(ctx context.Context, refreshToken string, maxRetries int) (*CodexTokenData, error)

RefreshTokensWithRetry retries only failures known to precede submission. A rotating refresh grant must not be replayed after an uncertain exchange.

type CodexAuthInfo

type CodexAuthInfo struct {
	ChatgptAccountID               string          `json:"chatgpt_account_id"`
	ChatgptPlanType                string          `json:"chatgpt_plan_type"`
	ChatgptSubscriptionActiveStart any             `json:"chatgpt_subscription_active_start"`
	ChatgptSubscriptionActiveUntil any             `json:"chatgpt_subscription_active_until"`
	ChatgptSubscriptionLastChecked time.Time       `json:"chatgpt_subscription_last_checked"`
	ChatgptUserID                  string          `json:"chatgpt_user_id"`
	Groups                         []any           `json:"groups"`
	Organizations                  []Organizations `json:"organizations"`
	UserID                         string          `json:"user_id"`
}

CodexAuthInfo contains authentication-related details specific to Codex. This includes ChatGPT account information, subscription status, and user/organization IDs.

type CodexTokenData

type CodexTokenData struct {
	// IDToken is the JWT ID token containing user claims
	IDToken string `json:"id_token"`
	// AccessToken is the OAuth2 access token for API access
	AccessToken string `json:"access_token"`
	// RefreshToken is used to obtain new access tokens
	RefreshToken string `json:"refresh_token"`
	// AccountID is the OpenAI account identifier
	AccountID string `json:"account_id"`
	// Email is the OpenAI account email
	Email string `json:"email"`
	// Expire is the timestamp of the token expire
	Expire string `json:"expired"`
}

CodexTokenData holds the OAuth token information obtained from OpenAI. It includes the ID token, access token, refresh token, and associated user details.

type JWTClaims

type JWTClaims struct {
	AtHash        string        `json:"at_hash"`
	Aud           []string      `json:"aud"`
	AuthProvider  string        `json:"auth_provider"`
	AuthTime      int           `json:"auth_time"`
	Email         string        `json:"email"`
	EmailVerified bool          `json:"email_verified"`
	Exp           int           `json:"exp"`
	CodexAuthInfo CodexAuthInfo `json:"https://api.openai.com/auth"`
	Iat           int           `json:"iat"`
	Iss           string        `json:"iss"`
	Jti           string        `json:"jti"`
	Rat           int           `json:"rat"`
	Sid           string        `json:"sid"`
	Sub           string        `json:"sub"`
}

JWTClaims represents the claims section of a JSON Web Token (JWT). It includes standard claims like issuer, subject, and expiration time, as well as custom claims specific to OpenAI's authentication.

func ParseJWTToken

func ParseJWTToken(token string) (*JWTClaims, error)

ParseJWTToken parses a JWT token string and extracts its claims without performing cryptographic signature verification. This is useful for introspecting the token's contents to retrieve user information from an ID token after it has been validated by the authentication server.

func (*JWTClaims) GetAccountID

func (c *JWTClaims) GetAccountID() string

GetAccountID extracts the user's account ID (subject) from the JWT claims. It retrieves the unique identifier for the user's ChatGPT account.

func (*JWTClaims) GetUserEmail

func (c *JWTClaims) GetUserEmail() string

GetUserEmail extracts the user's email address from the JWT claims.

type Organizations

type Organizations struct {
	ID        string `json:"id"`
	IsDefault bool   `json:"is_default"`
	Role      string `json:"role"`
	Title     string `json:"title"`
}

Organizations defines the structure for organization details within the JWT claims. It holds information about the user's organization, such as ID, role, and title.

type RefreshJournal added in v1.1.9

type RefreshJournal interface {
	RefreshClaim(context.Context, string) (string, error)
	RefreshRead(context.Context, string) ([]byte, error)
	RefreshComplete(context.Context, string, string, []byte) error
	RefreshRelease(context.Context, string, string) error
}

RefreshJournal claims a grant in the authoritative credential database before submission. Claims never expire. Only their owner may complete or release them; release is allowed only when the request is known not to have been submitted. Receipts contain credentials and must be backed up and protected with auth data.

type RefreshOutcomeUnknownError added in v1.1.9

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

RefreshOutcomeUnknownError means the provider may have consumed the refresh grant. It is neither a rejected grant nor permission to submit the same grant again.

func NewRefreshOutcomeUnknownError added in v1.1.9

func NewRefreshOutcomeUnknownError(clientID, refreshToken, stage string, cause error) *RefreshOutcomeUnknownError

NewRefreshOutcomeUnknownError records which grant must not be replayed.

func (*RefreshOutcomeUnknownError) Error added in v1.1.9

func (*RefreshOutcomeUnknownError) Stage added in v1.1.9

Stage identifies where the result became uncertain, without upstream error text.

func (*RefreshOutcomeUnknownError) Unwrap added in v1.1.9

func (e *RefreshOutcomeUnknownError) Unwrap() error

Jump to

Keyboard shortcuts

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