feat(server): HMAC stream token auth path (UPnP slice 1/6)
Adds SignStreamToken / VerifyStreamToken (HMAC-SHA256 over trackID|exp) and modifies handleGetStream to accept either the existing session cookie OR a valid signed token. Stream route moved out of the authed group so the handler's own auth check runs and the token bypass is reachable. Enables Sonos / UPnP speakers to fetch the stream URL without carrying the user's session cookie - they cannot. The token is short-lived (max 24h per the design); expiry checked at request time only, not per-byte, so long tracks play through. streamSecret field on handlers is nil for now; Task 2 wires the loader (env var with auto-generated fallback persisted in app_preferences). Adds auth.OptionalUser - the permissive sibling of RequireUser that attaches the user to context when a valid cookie / bearer is present but does NOT 401 on absence. The stream route is wrapped with it so the handler can fall through to the token path when no session is present. newLibraryRouter (test fixture) gets a synthetic-user middleware on the stream route so existing media_test tests keep passing without seeding a real session row - production traffic uses auth.OptionalUser, the test path uses auth.UserCtxKeyForTest(). Five tests cover round-trip, tampered token rejection, expiry, wrong-track-ID, and wrong-secret rejection. CI verifies.
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/config"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/eventbus"
|
||||
@@ -21,7 +22,15 @@ import (
|
||||
// newLibraryRouter builds a test-only chi router with the library handlers
|
||||
// mounted at their path-style routes. Tests hit this router rather than
|
||||
// calling handler methods directly so chi URL params populate correctly.
|
||||
// RequireUser is NOT applied — library handlers don't read user context.
|
||||
// RequireUser is NOT applied to most routes — library handlers don't read
|
||||
// user context.
|
||||
//
|
||||
// The stream route is wrapped with a synthetic-user middleware because
|
||||
// handleGetStream now enforces its own auth check (streamAuthOk) after the
|
||||
// UPnP slice moved it out of the authed.Group. Real traffic carries either
|
||||
// a session cookie (resolved by auth.OptionalUser) or a signed query
|
||||
// token; tests get a fake user-in-context so the cookie path of
|
||||
// streamAuthOk succeeds without seeding a real session row.
|
||||
func newLibraryRouter(h *handlers) chi.Router {
|
||||
r := chi.NewRouter()
|
||||
r.Get("/api/artists", h.handleListArtists)
|
||||
@@ -29,11 +38,23 @@ func newLibraryRouter(h *handlers) chi.Router {
|
||||
r.Get("/api/albums/{id}", h.handleGetAlbum)
|
||||
r.Get("/api/albums/{id}/cover", h.handleGetCover)
|
||||
r.Get("/api/tracks/{id}", h.handleGetTrack)
|
||||
r.Get("/api/tracks/{id}/stream", h.handleGetStream)
|
||||
r.With(injectFakeUserForTest).Get("/api/tracks/{id}/stream", h.handleGetStream)
|
||||
r.Get("/api/search", h.handleSearch)
|
||||
return r
|
||||
}
|
||||
|
||||
// injectFakeUserForTest attaches an empty dbq.User to request context via
|
||||
// auth.UserCtxKeyForTest(), letting handleGetStream's streamAuthOk succeed
|
||||
// on the session path without the test having to seed a real session row.
|
||||
// Production traffic uses auth.OptionalUser instead; this is the test-only
|
||||
// equivalent that bypasses the DB lookup.
|
||||
func injectFakeUserForTest(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.WithValue(r.Context(), auth.UserCtxKeyForTest(), dbq.User{})
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func TestHandleGetTrack_HappyPath(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
|
||||
Reference in New Issue
Block a user