auth

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultTokenValidityMinutes = 60

DefaultTokenValidityMinutes is the default token validity period.

Variables

This section is empty.

Functions

func ClearToken

func ClearToken(cachePath string) error

ClearToken removes the cached token, forcing a refresh on next GetToken call.

func DeleteToken

func DeleteToken(path string) error

DeleteToken removes the token cache file. Returns nil if the file doesn't exist.

func SaveToken

func SaveToken(path string, token *Token) error

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

type Token struct {
	AccessToken string
	ExpiresAt   int64
}

Token represents an access token with its expiry.

func ExchangeToken

func ExchangeToken(ctx context.Context, baseURL, secretKey string) (*Token, error)

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

func GetToken(ctx context.Context, cachePath, baseURL, secretKey string) (*Token, error)

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.

func LoadToken

func LoadToken(path string) (*Token, error)

LoadToken reads a token from the cache file. Returns an error if the file doesn't exist or contains invalid JSON.

func (*Token) IsValid

func (t *Token) IsValid() bool

IsValid returns true if the token has not expired.

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.

Jump to

Keyboard shortcuts

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