Documentation
¶
Index ¶
Constants ¶
const ( // SpotifyAuthURL is the Spotify authorization endpoint. SpotifyAuthURL = "https://accounts.spotify.com/authorize" // SpotifyTokenURL is the Spotify token endpoint. SpotifyTokenURL = "https://accounts.spotify.com/api/token" // DefaultRedirectURI is the default callback URI for the local server. DefaultRedirectURI = "http://127.0.0.1:8888/callback" )
const ( // CodeVerifierLength is the length of the PKCE code verifier. // Spotify requires 43-128 characters; we use 64 for good entropy. CodeVerifierLength = 64 // StateLength is the length of the state parameter for CSRF protection. StateLength = 32 )
const (
// DefaultTokenFileName is the default name for the token file.
DefaultTokenFileName = "spotify_token.json"
)
Variables ¶
var DefaultScopes = []string{
"user-read-playback-state",
"user-modify-playback-state",
"user-read-currently-playing",
"user-read-recently-played",
"user-read-private",
"user-read-email",
"streaming",
}
DefaultScopes are the Spotify scopes required for riff functionality.
Functions ¶
func BuildAuthURL ¶
func BuildAuthURL(params AuthURLParams, pkce *PKCE) string
BuildAuthURL constructs the Spotify authorization URL with PKCE parameters.
Types ¶
type AuthURLParams ¶
type AuthURLParams struct {
ClientID string
RedirectURI string
Scopes []string
CodeVerifier string
State string
}
AuthURLParams contains the parameters for building an authorization URL.
type CallbackResult ¶
CallbackResult contains the result of the OAuth callback.
type CallbackServer ¶
type CallbackServer struct {
// contains filtered or unexported fields
}
CallbackServer handles the OAuth callback from Spotify.
func NewCallbackServer ¶
func NewCallbackServer(port int) (*CallbackServer, error)
NewCallbackServer creates a new callback server on the specified port.
func (*CallbackServer) Port ¶
func (cs *CallbackServer) Port() int
Port returns the port the server is listening on.
func (*CallbackServer) Shutdown ¶
func (cs *CallbackServer) Shutdown(ctx context.Context) error
Shutdown gracefully shuts down the server.
func (*CallbackServer) Start ¶
func (cs *CallbackServer) Start()
Start begins serving HTTP requests in the background.
func (*CallbackServer) Wait ¶
func (cs *CallbackServer) Wait(ctx context.Context) (CallbackResult, error)
Wait blocks until a callback is received or context is cancelled. Returns the callback result or an error if the context times out.
type Config ¶
Config holds the OAuth configuration.
func (*Config) BuildAuthURL ¶
BuildAuthURLFromConfig is a convenience method to build an auth URL from config.
type Token ¶
type Token struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
ExpiresAt time.Time `json:"expires_at"`
}
Token represents Spotify OAuth tokens.
func ExchangeCode ¶
func ExchangeCode(ctx context.Context, clientID, code, redirectURI, codeVerifier string) (*Token, error)
ExchangeCode exchanges an authorization code for tokens.
func RefreshAccessToken ¶
RefreshAccessToken uses a refresh token to get a new access token.
type TokenStorage ¶
type TokenStorage struct {
// contains filtered or unexported fields
}
TokenStorage handles persisting tokens to disk.
func NewTokenStorage ¶
func NewTokenStorage(path string) (*TokenStorage, error)
NewTokenStorage creates a new token storage at the specified path. If path is empty, uses the default location (~/.config/riff/spotify_token.json).
func (*TokenStorage) Delete ¶
func (s *TokenStorage) Delete() error
Delete removes the stored token.
func (*TokenStorage) Exists ¶
func (s *TokenStorage) Exists() bool
Exists returns true if a token file exists.
func (*TokenStorage) Load ¶
func (s *TokenStorage) Load() (*Token, error)
Load reads a token from disk.
func (*TokenStorage) Path ¶
func (s *TokenStorage) Path() string
Path returns the path to the token file.
func (*TokenStorage) Save ¶
func (s *TokenStorage) Save(token *Token) error
Save persists a token to disk.