auth

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: ISC Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildAuthorizeURL

func BuildAuthorizeURL(clientID, state string) string

BuildAuthorizeURL constructs the Strava OAuth authorization URL with all required parameters.

func FetchAthleteName

func FetchAthleteName(accessToken, athleteEndpoint string) (string, error)

FetchAthleteName calls GET /athlete with the given access token and returns "firstname lastname". This validates the full auth chain end-to-end: tokens were stored correctly and work for API calls. The athleteEndpoint parameter allows overriding for tests; pass athleteURL for production.

func NewCallbackHandler

func NewCallbackHandler(expectedState string, codeCh chan<- string, errCh chan<- error) http.Handler

NewCallbackHandler creates an HTTP handler for the OAuth callback endpoint. It validates the state parameter, checks for errors from Strava, and extracts the authorization code.

func RunOAuthFlow

func RunOAuthFlow(cfg *config.Config, store TokenStore, logger *slog.Logger) error

RunOAuthFlow runs the complete OAuth browser flow: 1. Starts a callback server on port 19876 2. Opens system browser to Strava authorization page 3. Waits for callback with authorization code 4. Exchanges code for tokens 5. Persists tokens to disk 6. Validates by calling GET /athlete 7. Prints "Authenticated as [Name]!" to stderr

Types

type FileTokenStore

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

FileTokenStore implements TokenStore with atomic file writes.

func NewFileTokenStore

func NewFileTokenStore(path string) *FileTokenStore

NewFileTokenStore creates a new FileTokenStore at the given path.

func (*FileTokenStore) IsExpired

func (s *FileTokenStore) IsExpired(tokens *Tokens) bool

IsExpired returns true if the token is expired or will expire within 5 minutes (300 seconds).

func (*FileTokenStore) Read

func (s *FileTokenStore) Read() (*Tokens, error)

Read loads tokens from the file on disk.

func (*FileTokenStore) Write

func (s *FileTokenStore) Write(tokens *Tokens) error

Write persists tokens to disk using atomic write-then-rename. It creates the parent directory if it does not exist, writes to a temporary file with 0600 permissions, fsyncs, then renames atomically.

type TokenStore

type TokenStore interface {
	Read() (*Tokens, error)
	Write(tokens *Tokens) error
	IsExpired(tokens *Tokens) bool
}

TokenStore defines the interface for reading and writing OAuth tokens.

type Tokens

type Tokens struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	ExpiresAt    int64  `json:"expires_at"`
}

Tokens holds the OAuth2 token data persisted to disk. Only access_token, refresh_token, and expires_at are stored. Client credentials come from environment variables, never from the token file.

func ExchangeCode

func ExchangeCode(clientID, clientSecret, code, tokenEndpoint string) (*Tokens, error)

ExchangeCode exchanges an authorization code for tokens by POSTing to the Strava token endpoint. The tokenEndpoint parameter allows overriding for tests; pass tokenURL for production.

Jump to

Keyboard shortcuts

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