auth

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package auth handles Schwab API authentication and configuration.

Index

Constants

This section is empty.

Variables

View Source
var ErrMissingCredentials = errors.New("missing required credentials")

ErrMissingCredentials is a sentinel error wrapped by the ValidationError returned from LoadConfig when client_id or client_secret are absent. Callers can use errors.Is with ErrMissingCredentials to distinguish missing credentials from other validation failures (e.g., invalid base_url) without matching on error message text.

Functions

func AuthorizeURL

func AuthorizeURL(cfg *Config) (string, string, error)

AuthorizeURL builds the Schwab authorization URL and returns it with a random state value.

func IsAccessTokenExpired

func IsAccessTokenExpired(tf *TokenFile) bool

IsAccessTokenExpired checks if the access token is expired (with 300s leeway). Returns true if time.Now is past ExpiresAt minus the leeway.

func IsRefreshTokenStale

func IsRefreshTokenStale(tf *TokenFile) bool

IsRefreshTokenStale checks if the refresh token is stale (> 6.5 days old). Returns true if time.Now is past creation_timestamp plus the max age.

func RunLogin

func RunLogin(cfg *Config, tokenPath, tokenEndpoint string, openBrowser bool, w io.Writer) error

RunLogin performs the full OAuth login flow and persists the resulting token.

func SaveConfig

func SaveConfig(configPath string, cfg *Config) error

SaveConfig writes config to file with 0600 permissions. Creates parent directory with 0700 if missing.

func SaveToken

func SaveToken(tokenPath string, tf *TokenFile) error

SaveToken writes the token file with 0600 permissions. Creates the parent directory with 0700 if missing.

func SetDefaultAccount

func SetDefaultAccount(configPath, hash string) error

SetDefaultAccount loads config, sets DefaultAccount, and saves it.

func StartCallbackServer

func StartCallbackServer(addr, expectedState string) (string, error)

StartCallbackServer starts a loopback-only HTTPS callback server and waits for one callback.

Types

type Config

type Config struct {
	ClientID                   string `json:"client_id"`
	ClientSecret               string `json:"client_secret"`
	BaseURL                    string `json:"base_url"`
	BaseURLInsecure            bool   `json:"base_url_insecure"`
	CallbackURL                string `json:"callback_url"`
	DefaultAccount             string `json:"default_account"`
	IAlsoLikeToLiveDangerously bool   `json:"i-also-like-to-live-dangerously"`
}

Config holds Schwab API credentials and settings.

func LoadConfig

func LoadConfig(configPath string) (*Config, error)

LoadConfig reads config from file and applies env var overrides. Default path: ~/.config/schwab-agent/config.json Priority: env vars > config file > defaults Returns ValidationError if ClientID or ClientSecret are empty after all sources.

func (*Config) APIBaseURL added in v0.2.0

func (cfg *Config) APIBaseURL() string

APIBaseURL returns the normalized REST/OAuth base URL, defaulting to Schwab's production host when the config leaves base_url empty.

func (*Config) OAuthAuthorizeURL added in v0.2.0

func (cfg *Config) OAuthAuthorizeURL() string

OAuthAuthorizeURL derives the authorize endpoint from base_url so a Schwab-compatible proxy can front both REST and OAuth traffic consistently.

func (*Config) OAuthTokenURL added in v0.2.0

func (cfg *Config) OAuthTokenURL() string

OAuthTokenURL derives the token/refresh endpoint from base_url. We keep this separate from callback_url because the callback is still the local loopback listener, while authorize/token traffic may be routed through a proxy.

func (*Config) TLSConfig added in v1.6.0

func (cfg *Config) TLSConfig() *tls.Config

TLSConfig returns a TLS configuration for OAuth and API HTTP clients. When base_url_insecure is true we skip certificate verification so local self-signed proxy setups can terminate TLS without requiring a custom trust store on every developer machine. Returns nil for standard secure connections.

type TokenData

type TokenData = schwabauth.TokenData

TokenData holds the OAuth token fields shared with schwab-go.

type TokenFile

type TokenFile = schwabauth.TokenFile

TokenFile is the on-disk token format shared with schwab-go.

func ExchangeCode

func ExchangeCode(cfg *Config, code, tokenEndpoint string, now time.Time) (*TokenFile, error)

ExchangeCode exchanges an OAuth authorization code for a token file.

The now parameter is retained for the test seam this package already exposed. schwab-go owns the exchange request and response parsing, then this adapter normalizes the saved timestamps when tests pass a deterministic clock.

func LoadToken

func LoadToken(tokenPath string) (*TokenFile, error)

LoadToken reads and parses the token file. Returns AuthRequiredError if the file doesn't exist.

func RefreshAccessToken

func RefreshAccessToken(cfg *Config, tf *TokenFile, endpoint string) (*TokenFile, error)

RefreshAccessToken exchanges the refresh token for a new access token. Preserves creation_timestamp from the original TokenFile. Returns AuthExpiredError if the refresh fails with invalid_grant. The endpoint parameter allows overriding the token URL for testing.

Jump to

Keyboard shortcuts

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