Documentation
¶
Overview ¶
Package auth handles Schwab API authentication and configuration.
Index ¶
- Variables
- func AuthorizeURL(cfg *Config) (string, string, error)
- func IsAccessTokenExpired(tf *TokenFile) bool
- func IsRefreshTokenStale(tf *TokenFile) bool
- func RunLogin(cfg *Config, tokenPath, tokenEndpoint string, openBrowser bool, w io.Writer) error
- func SaveConfig(configPath string, cfg *Config) error
- func SaveToken(tokenPath string, tf *TokenFile) error
- func SetDefaultAccount(configPath, hash string) error
- func StartCallbackServer(addr, expectedState string) (string, error)
- type Config
- type TokenData
- type TokenFile
Constants ¶
This section is empty.
Variables ¶
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 ¶
AuthorizeURL builds the Schwab authorization URL and returns it with a random state value.
func IsAccessTokenExpired ¶
IsAccessTokenExpired checks if the access token is expired (with 300s leeway). Returns true if time.Now is past ExpiresAt minus the leeway.
func IsRefreshTokenStale ¶
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 SaveConfig ¶
SaveConfig writes config to file with 0600 permissions. Creates parent directory with 0700 if missing.
func SaveToken ¶
SaveToken writes the token file with 0600 permissions. Creates the parent directory with 0700 if missing.
func SetDefaultAccount ¶
SetDefaultAccount loads config, sets DefaultAccount, and saves it.
func StartCallbackServer ¶
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 ¶
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
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
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
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
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 ¶
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 ¶
LoadToken reads and parses the token file. Returns AuthRequiredError if the file doesn't exist.
func RefreshAccessToken ¶
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.