oauth

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultCredentialsDir

func DefaultCredentialsDir() (string, error)

DefaultCredentialsDir returns the directory used by the encrypted/plaintext credential helpers. If SetCredentialsDir has been called with a non-empty value, that wins; otherwise returns ~/.forge/credentials.

func DeleteCredentials

func DeleteCredentials(provider string) error

DeleteCredentials removes stored OAuth credentials from both the encrypted store and the plaintext file.

func GenerateState

func GenerateState() (string, error)

GenerateState creates a random state parameter for OAuth flows.

func MigrateToEncrypted

func MigrateToEncrypted(provider string) error

MigrateToEncrypted moves a provider's plaintext credentials into the encrypted store. It is a no-op if no plaintext file exists or the encrypted provider is unavailable.

func SaveCredentials

func SaveCredentials(provider string, token *Token) error

SaveCredentials stores OAuth token data. When FORGE_PASSPHRASE is available the token is saved to the encrypted secrets file and any plaintext file is removed. Otherwise it falls back to writing a plaintext JSON file.

func SetCredentialsDir

func SetCredentialsDir(dir string)

SetCredentialsDir overrides the default OAuth credentials directory. Intended for early-startup wiring; calling it after concurrent Save/Load is in flight is a data race.

Pass "" to clear the override and revert to the home-based default. Review B11.

Types

type CallbackResult

type CallbackResult struct {
	Code  string
	State string
	Error string
}

CallbackResult holds the result from the OAuth callback.

type CallbackServer

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

CallbackServer is a local HTTP server that receives the OAuth authorization code.

func NewCallbackServer

func NewCallbackServer(port int) *CallbackServer

NewCallbackServer creates a callback server on the given port.

func (*CallbackServer) Start

func (s *CallbackServer) Start() error

Start starts the callback server and returns immediately.

func (*CallbackServer) Stop

func (s *CallbackServer) Stop()

Stop shuts down the callback server.

func (*CallbackServer) WaitForCode

func (s *CallbackServer) WaitForCode(ctx context.Context) (CallbackResult, error)

WaitForCode blocks until an authorization code is received or the context expires.

type Flow

type Flow struct {
	Config  ProviderConfig
	Timeout time.Duration // default: 2 minutes
}

Flow orchestrates the OAuth authorization code flow with PKCE.

func NewFlow

func NewFlow(config ProviderConfig) *Flow

NewFlow creates a new OAuth flow with the given provider config.

func (*Flow) Execute

func (f *Flow) Execute(ctx context.Context, provider string) (*Token, error)

Execute runs the full OAuth flow: 1. Generate PKCE params and state 2. Start local callback server 3. Open browser to authorization URL 4. Wait for authorization code 5. Exchange code for tokens 6. Store credentials

type PKCEParams

type PKCEParams struct {
	Verifier  string
	Challenge string
	Method    string // always "S256"
}

PKCEParams holds the PKCE code verifier and challenge for OAuth flows.

func GeneratePKCE

func GeneratePKCE() (*PKCEParams, error)

GeneratePKCE creates a new PKCE code verifier (32 random bytes, base64url-encoded) and its corresponding S256 challenge.

type ProviderConfig

type ProviderConfig struct {
	AuthURL     string
	TokenURL    string
	ClientID    string
	Scopes      string
	RedirectURI string
	BaseURL     string            // API base URL to use with the obtained token
	ExtraParams map[string]string // additional query params for the auth URL
}

ProviderConfig holds the OAuth configuration for a provider.

func OpenAIConfig

func OpenAIConfig() ProviderConfig

OpenAIConfig returns the OAuth configuration for OpenAI. Uses the same public client ID and endpoints as the official Codex CLI. ChatGPT OAuth tokens are scoped to the ChatGPT backend API, not the standard OpenAI API (api.openai.com). The base URL is set accordingly.

type Token

type Token struct {
	AccessToken  string    `json:"access_token"`
	RefreshToken string    `json:"refresh_token,omitempty"`
	TokenType    string    `json:"token_type"`
	ExpiresIn    int       `json:"expires_in,omitempty"`
	ExpiresAt    time.Time `json:"expires_at"`
	Scope        string    `json:"scope,omitempty"`
	BaseURL      string    `json:"base_url,omitempty"` // API base URL for this token
}

Token holds the OAuth token data.

func ExchangeCode

func ExchangeCode(tokenURL, clientID, code, redirectURI, codeVerifier string) (*Token, error)

ExchangeCode exchanges an authorization code for tokens.

Deprecated for new callers — use ExchangeCodeCtx so the request is bounded by a context and rides a caller-provided *http.Client. Kept for backward compatibility with code written against v0.10.

func ExchangeCodeCtx

func ExchangeCodeCtx(ctx context.Context, client *http.Client, tokenURL, clientID, code, redirectURI, codeVerifier string) (*Token, error)

ExchangeCodeCtx is the context- and client-aware variant of ExchangeCode. Caller MUST pass a context with a finite deadline (or a cancellable parent) so a hung IdP cannot wedge the goroutine indefinitely (review B2).

If client is nil, a sensible defaulting client is constructed with a 30s end-to-end timeout — but callers in production should pass the egress-controlled client built by security.Resolve so token endpoints ride the same allowlist as every other outbound call.

func LoadCredentials

func LoadCredentials(provider string) (*Token, error)

LoadCredentials loads OAuth token data. It tries the encrypted store first, then falls back to the plaintext file so that pre-migration credentials continue to work.

func RefreshToken

func RefreshToken(tokenURL, clientID, refreshToken string) (*Token, error)

RefreshToken exchanges a refresh token for new access and refresh tokens.

Deprecated for new callers — use RefreshTokenCtx. See review B2.

func RefreshTokenCtx

func RefreshTokenCtx(ctx context.Context, client *http.Client, tokenURL, clientID, refreshToken string) (*Token, error)

RefreshTokenCtx is the context- and client-aware variant of RefreshToken. Caller MUST pass a context with a finite deadline. See ExchangeCodeCtx docstring for the client-injection contract.

func (*Token) IsExpired

func (t *Token) IsExpired() bool

IsExpired returns true if the token has expired or will expire within the given buffer duration (default 5 minutes).

func (*Token) IsExpiredWithBuffer

func (t *Token) IsExpiredWithBuffer(buffer time.Duration) bool

IsExpiredWithBuffer returns true if the token expires within the buffer.

Jump to

Keyboard shortcuts

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