auth

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
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"
)
View Source
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
)
View Source
const (
	// DefaultTokenFileName is the default name for the token file.
	DefaultTokenFileName = "spotify_token.json"
)

Variables

View Source
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

type CallbackResult struct {
	Code  string
	State string
	Error string
}

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

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

type Config struct {
	ClientID    string
	RedirectURI string
	Scopes      []string
}

Config holds the OAuth configuration.

func NewConfig

func NewConfig(clientID string) *Config

NewConfig creates a new OAuth configuration with defaults.

func (*Config) BuildAuthURL

func (c *Config) BuildAuthURL(pkce *PKCE) string

BuildAuthURLFromConfig is a convenience method to build an auth URL from config.

type PKCE

type PKCE struct {
	Verifier  string
	Challenge string
	State     string
}

PKCE holds the code verifier and challenge for OAuth PKCE flow.

func NewPKCE

func NewPKCE() (*PKCE, error)

NewPKCE generates a new PKCE code verifier, challenge, and state.

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

func RefreshAccessToken(ctx context.Context, clientID, refreshToken string) (*Token, error)

RefreshAccessToken uses a refresh token to get a new access token.

func (*Token) IsExpired

func (t *Token) IsExpired() bool

IsExpired returns true if the token has expired or will expire within the buffer.

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.

Jump to

Keyboard shortcuts

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