Security: - Token revocation. User gains a TokenVersion counter embedded in both tokens as a "tv" claim; AuthenticateToken rejects a token whose version no longer matches the row. Logout bumps it, so logging out now kills outstanding tokens rather than only clearing cookies. Costs one primary-key SELECT per authenticated request, which is the price of having revocation at all. - Spotify OAuth CSRF. The fixed SPOTIFY_AUTH_STATE constant is replaced by a per-request one-shot nonce, verified and consumed on callback. - Private chat attachments. Attachments on private messages are written to /backend/uploads/private/ and served at /uploads/private/, gated by the same nginx auth_request admin check used for /hasura/ and /notes/. Private uploads are rejected from non-admin callers. - The icecast admin UI (/radio/admin/) is behind the admin gate; listener traffic is untouched. - Store caches are mutex-guarded, so -race is clean. Bugs: - The email pipeline no longer drops emails permanently on transient failure. Failures record an attempt counter and are retried up to 3 times; the fetch window rewinds to the oldest retryable email's ReceivedAt, without which a retry could never re-fetch it. - Rowing duplicate detection distinguishes ErrRecordNotFound from a real DB error, so a transient failure no longer causes a duplicate insert plus a paid Claude call. - DeletePost propagates its delete error instead of reporting success. - RecentlyPlayed populates the cache it reads. - main.go exits non-zero if r.Run fails, so a port conflict is visible. - Python: the scan truncation flag is set on every deadline path; timed_out is reported by the worker rather than inferred from the clock; date validation uses Europe/London rather than server-local time. - Vue: Time.vue and Radio.vue intervals are cleared on unmount; Feed.vue guards a missing author; Timer.vue gets an explicit state machine and compares total elapsed seconds; Elle.vue's comma-operator call and per-frame console.log are fixed. Simplification: - One TTLCache replaces three near-identical cache implementations, with the differing eviction rules as explicit parameters. - run_scan's transport index no longer uses one object as both ordering record and live output; its three phases are separate functions. - _price_hotels submits all work to a single pool, so the nested-executor deadlock hazard is gone by construction. - The NDJSON event generators share one implementation. - IMAP FETCH responses pair each line with its literal in the type, so callers no longer hand-skip alternate entries. - Shared helpers for slice-of-pointers, markdown fences and auth cookies. - Vue: useHotelSearch, createMirrorStore, useRotation, useCreateForm, useLinkAttrs, ListWidget and LinkTableItems replace duplicated blocks. - nginx dev config's duplicated :80/:443 bodies share one partial. Infra: certbot passes --deploy-hook touching a sentinel that nginx's watcher reloads on, so renewed certs are served without a restart, and the watcher also removes the manual restart after first issuance. db is gated on service_healthy, autoheal is pinned, and the unused envsubst entries are dropped. Verified: go build/vet, npx vite build, py_compile, docker compose config for both overlays, bash -n on all scripts, and an envsubst render of every nginx template (no leftover placeholders, runtime vars preserved, braces balanced). Co-Authored-By: Claude Opus 5 <[email protected]>
155 lines
5.0 KiB
Go
155 lines
5.0 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/zmb3/spotify/v2"
|
|
spotifyauth "github.com/zmb3/spotify/v2/auth"
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
// SpotifyConfig holds the OAuth app credentials.
|
|
//
|
|
// There is no state value here any more: the `state` parameter is a one-shot
|
|
// CSRF nonce minted per authorisation request by handlers.Store, so a fixed
|
|
// configured value would defeat the point.
|
|
type SpotifyConfig struct {
|
|
RedirectURL string
|
|
ClientID string
|
|
ClientSecret string
|
|
}
|
|
|
|
// SPOTIFY_TOKEN_JSON_PATH is inside a Docker volume, so the refresh token
|
|
// survives container restarts and the OAuth dance only has to be done once.
|
|
const SPOTIFY_TOKEN_JSON_PATH = "/backend/token/spotify_token.json"
|
|
|
|
// SaveSpotifyToken persists an OAuth token to disk.
|
|
//
|
|
// It copies the fields into a local anonymous struct rather than marshalling
|
|
// oauth2.Token directly, so the on-disk format stays stable regardless of what
|
|
// the oauth2 package adds to its own struct or its JSON tags.
|
|
func SaveSpotifyToken(path string, tok *oauth2.Token) error {
|
|
data := struct {
|
|
AccessToken string `json:"access_token"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
TokenType string `json:"token_type"`
|
|
Expiry time.Time `json:"expiry"`
|
|
}{
|
|
AccessToken: tok.AccessToken,
|
|
RefreshToken: tok.RefreshToken,
|
|
TokenType: tok.TokenType,
|
|
Expiry: tok.Expiry,
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
|
return fmt.Errorf("creating token directory: %w", err)
|
|
}
|
|
|
|
jsonBytes, err := json.MarshalIndent(data, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 0600 ensures only your user can read/write the file
|
|
return os.WriteFile(path, jsonBytes, 0600)
|
|
}
|
|
|
|
// LoadSpotifyToken reads a token previously written by SaveSpotifyToken.
|
|
// A missing file returns an error, which callers treat as "not yet
|
|
// authenticated" rather than a fault.
|
|
func LoadSpotifyToken(path string) (*oauth2.Token, error) {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var saved struct {
|
|
AccessToken string `json:"access_token"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
TokenType string `json:"token_type"`
|
|
Expiry time.Time `json:"expiry"`
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &saved); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tok := &oauth2.Token{
|
|
AccessToken: saved.AccessToken,
|
|
RefreshToken: saved.RefreshToken,
|
|
TokenType: saved.TokenType,
|
|
Expiry: saved.Expiry,
|
|
}
|
|
|
|
return tok, nil
|
|
}
|
|
|
|
// InitSpotifyAuth builds the OAuth authenticator and, if a saved token
|
|
// exists, an authenticated client.
|
|
//
|
|
// Start-up never fails on Spotify problems: when there is no token or the
|
|
// refresh is rejected it logs how to authenticate and returns a nil client.
|
|
// Every Spotify handler and resolver therefore has to nil-check
|
|
// Store.SpotifyClient. The authorisation URL can no longer be printed here
|
|
// because its state nonce is per-request (see handlers.Store.StartSpotifyAuth);
|
|
// following it sends the browser to /spotify/callback, which fills the client
|
|
// in at runtime.
|
|
func InitSpotifyAuth(config *SpotifyConfig) (*spotifyauth.Authenticator, *spotify.Client) {
|
|
auth := spotifyauth.New(
|
|
spotifyauth.WithRedirectURL(config.RedirectURL),
|
|
spotifyauth.WithClientID(config.ClientID),
|
|
spotifyauth.WithClientSecret(config.ClientSecret),
|
|
spotifyauth.WithScopes(
|
|
spotifyauth.ScopeUserReadPlaybackState,
|
|
spotifyauth.ScopeUserReadCurrentlyPlaying,
|
|
spotifyauth.ScopeUserReadRecentlyPlayed,
|
|
),
|
|
)
|
|
|
|
// check if token exists locally
|
|
token, err := LoadSpotifyToken(SPOTIFY_TOKEN_JSON_PATH)
|
|
if err != nil || token == nil {
|
|
fmt.Println("No Spotify token saved. Authenticate by calling GET /api/spotify/auth as an admin and opening the URL it returns.")
|
|
return auth, nil
|
|
}
|
|
|
|
// refresh token and client
|
|
client, err := RefreshClient(auth, token)
|
|
if err != nil {
|
|
fmt.Println("Failed to refresh Spotify token. Re-authenticate by calling GET /api/spotify/auth as an admin and opening the URL it returns.")
|
|
return auth, nil
|
|
}
|
|
|
|
return auth, client
|
|
}
|
|
|
|
// RefreshClient exchanges a stored token for a fresh one and returns a client
|
|
// using it.
|
|
//
|
|
// Spotify may issue a new refresh token during this exchange, so the result is
|
|
// written straight back to disk; the write error is ignored because a failed
|
|
// save only costs a re-authentication later, and failing the whole start-up
|
|
// over it would be worse. The returned client also holds the token source
|
|
// internally and keeps refreshing on its own — but those later refreshes are
|
|
// NOT persisted, so after a long uptime the file on disk can be stale.
|
|
func RefreshClient(auth *spotifyauth.Authenticator, token *oauth2.Token) (*spotify.Client, error) {
|
|
// context.Background rather than a request context: the resulting client
|
|
// is long-lived and must outlive whichever request triggered this.
|
|
ctx := context.Background()
|
|
|
|
token, err := auth.RefreshToken(ctx, token)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
SaveSpotifyToken(SPOTIFY_TOKEN_JSON_PATH, token)
|
|
|
|
client := spotify.New(auth.Client(ctx, token))
|
|
|
|
return client, nil
|
|
}
|