auth

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: ISC Imports: 21 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 BuildAuthorizeURLWithPKCE added in v0.0.5

func BuildAuthorizeURLWithPKCE(clientID, state, redirectURI, codeChallenge string) string

BuildAuthorizeURLWithPKCE constructs the Strava OAuth authorization URL with PKCE S256 challenge and a custom redirect URI for non-interactive flows.

func DeletePending added in v0.0.5

func DeletePending(dir string) error

DeletePending removes the oauth_pending.json file from dir. It is idempotent — a missing file is not an error.

func ExchangeAuthCode added in v0.0.5

func ExchangeAuthCode(cfg *config.Config, store TokenStore, pendingDir, code string) error

ExchangeAuthCode loads the pending state, exchanges the authorization code with the PKCE verifier, writes tokens via store, and deletes the pending state.

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 GenerateAuthURL added in v0.0.5

func GenerateAuthURL(clientID, pendingDir, redirectURI string) (string, error)

GenerateAuthURL creates a PKCE-secured authorization URL and persists the pending state (verifier, state, redirect_uri, TTL) under pendingDir.

func GeneratePKCE added in v0.0.5

func GeneratePKCE() (verifier, challenge string, err error)

GeneratePKCE creates a PKCE code_verifier (43-char base64url random) and its S256 code_challenge.

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

func SavePending added in v0.0.5

func SavePending(dir string, p *OAuthPending) error

SavePending writes the PKCE pending state to oauth_pending.json in dir with mode 0600.

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 0o600 permissions, fsyncs, then renames atomically.

type OAuthPending added in v0.0.5

type OAuthPending struct {
	Verifier    string `json:"verifier"`
	State       string `json:"state"`
	RedirectURI string `json:"redirect_uri"`
	Expires     int64  `json:"expires"`
}

OAuthPending holds the PKCE verifier and state between the "auth url" and "auth exchange" steps.

func LoadPending added in v0.0.5

func LoadPending(dir string) (*OAuthPending, error)

LoadPending reads the PKCE pending state from dir. Returns an error if the file is missing or the TTL has expired.

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, codeVerifier 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. If codeVerifier is non-empty it is sent as the code_verifier form field (PKCE).

Jump to

Keyboard shortcuts

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