Documentation
¶
Index ¶
- Constants
- func ClearToken(cachePath string) error
- func DeleteToken(path string) error
- func SaveToken(path string, token *Token) error
- func TokenCachePath() string
- type Token
- func ExchangeToken(ctx context.Context, baseURL, secretKey string) (*Token, error)
- func ExchangeTokenWithValidity(ctx context.Context, baseURL, secretKey string, validityMinutes int) (*Token, error)
- func GetToken(ctx context.Context, cachePath, baseURL, secretKey string) (*Token, error)
- func GetTokenWithRefresh(ctx context.Context, cachePath, baseURL, secretKey string, forceRefresh bool) (*Token, error)
- func LoadToken(path string) (*Token, error)
- type TokenRequest
- type TokenResponse
Constants ¶
const DefaultTokenValidityMinutes = 60
DefaultTokenValidityMinutes is the default token validity period.
Variables ¶
This section is empty.
Functions ¶
func ClearToken ¶
ClearToken removes the cached token, forcing a refresh on next GetToken call.
func DeleteToken ¶
DeleteToken removes the token cache file. Returns nil if the file doesn't exist.
func SaveToken ¶
SaveToken writes a token to the cache file. Creates parent directories if needed with 0700 permissions. The file is written with 0600 permissions.
func TokenCachePath ¶
func TokenCachePath() string
TokenCachePath returns the path to the token cache file. Uses XDG_CONFIG_HOME if set, otherwise ~/.config/pub.
Types ¶
type Token ¶
Token represents an access token with its expiry.
func ExchangeToken ¶
ExchangeToken exchanges a secret key for an access token. It calls the Public.com API to perform the token exchange.
func ExchangeTokenWithValidity ¶
func ExchangeTokenWithValidity(ctx context.Context, baseURL, secretKey string, validityMinutes int) (*Token, error)
ExchangeTokenWithValidity exchanges a secret key for an access token with a custom validity period.
func GetToken ¶
GetToken returns a valid access token, refreshing if necessary. It first tries to load a cached token. If the cached token is valid, it returns immediately. If the token is expired, missing, or corrupted, it exchanges the secret key for a new token and caches it.
func GetTokenWithRefresh ¶
func GetTokenWithRefresh(ctx context.Context, cachePath, baseURL, secretKey string, forceRefresh bool) (*Token, error)
GetTokenWithRefresh returns a valid access token. If forceRefresh is true, it ignores any cached token and exchanges for a new one. Use forceRefresh=true when you get a 401 error with a cached token.
type TokenRequest ¶
type TokenRequest struct {
ValidityInMinutes int `json:"validityInMinutes"`
Secret string `json:"secret"`
}
TokenRequest represents the request body for token exchange.
type TokenResponse ¶
type TokenResponse struct {
AccessToken string `json:"accessToken"`
}
TokenResponse represents the API response from token exchange.