diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go index 4ad51a8..ba7c362 100644 --- a/cmd/sondad/main.go +++ b/cmd/sondad/main.go @@ -20,6 +20,7 @@ import ( "codeberg.org/squidd/sonda/internal/library" "codeberg.org/squidd/sonda/internal/listening" "codeberg.org/squidd/sonda/internal/storage" + "codeberg.org/squidd/sonda/internal/streaming" "codeberg.org/squidd/sonda/migrations" ) @@ -97,6 +98,7 @@ func run() error { Tokens: tokens, Listening: listening.NewService(storage.NewListeningRepo(db)), Scan: scanControl{ctx: ctx, m: scans}, + Streams: streaming.NewService(libraryRepo, cfg.Library.Roots), }) // No WriteTimeout: audio streaming (later) needs long responses. diff --git a/internal/api/http/api_test.go b/internal/api/http/api_test.go index eac6d34..10100b0 100644 --- a/internal/api/http/api_test.go +++ b/internal/api/http/api_test.go @@ -17,6 +17,7 @@ import ( "codeberg.org/squidd/sonda/internal/auth" "codeberg.org/squidd/sonda/internal/listening" "codeberg.org/squidd/sonda/internal/storage" + "codeberg.org/squidd/sonda/internal/streaming" "codeberg.org/squidd/sonda/migrations" ) @@ -31,6 +32,12 @@ func newTestAPI(t *testing.T) http.Handler { } func newTestAPIWithScan(t *testing.T, scan scanService) http.Handler { + t.Helper() + return newTestAPIWith(t, func(d *Deps) { d.Scan = scan }) +} + +// newTestAPIWith wires real services and lets a test replace parts of Deps. +func newTestAPIWith(t *testing.T, mutate func(*Deps)) http.Handler { t.Helper() db, err := storage.Open(storage.DriverSQLite, filepath.Join(t.TempDir(), "api.db")) if err != nil { @@ -49,13 +56,23 @@ func newTestAPIWithScan(t *testing.T, scan scanService) http.Handler { } tokens := auth.NewTokenIssuer(secret, time.Hour) - return NewRouter(Deps{ + deps := Deps{ Logger: zap.NewNop(), Auth: auth.NewService(storage.NewUserRepo(db), storage.NewRefreshRepo(db), tokens, 24*time.Hour), Tokens: tokens, Listening: listening.NewService(storage.NewListeningRepo(db)), - Scan: scan, - }) + Scan: &fakeScan{}, + Streams: streaming.NewService(noFiles{}, nil), + } + mutate(&deps) + return NewRouter(deps) +} + +// noFiles is a streaming source with nothing to play. +type noFiles struct{} + +func (noFiles) PlayableFile(context.Context, int64) (streaming.File, error) { + return streaming.File{}, streaming.ErrNotFound } type resp struct { diff --git a/internal/api/http/auth_test.go b/internal/api/http/auth_test.go index 4fe65d2..7dc9eda 100644 --- a/internal/api/http/auth_test.go +++ b/internal/api/http/auth_test.go @@ -2,6 +2,7 @@ package httpapi import ( "net/http" + "net/http/httptest" "strconv" "testing" ) @@ -320,3 +321,34 @@ func TestAdminCanStartScan(t *testing.T) { t.Errorf("starts = %d", scan.starts) } } + +// Only "Authorization: Bearer " is accepted. A bare token must not pass. +func TestAuthorizationHeaderMustUseTheBearerScheme(t *testing.T) { + h := newTestAPI(t) + tok := registerAndLogin(t, h) + + for name, header := range map[string]string{ + "bare token": tok, + "basic scheme": "Basic " + tok, + "lowercase": "bearer " + tok, + "token scheme": "Token " + tok, + "empty bearer": "Bearer ", + "bearer no gap": "Bearer" + tok, + } { + req := httptest.NewRequest("GET", "/api/v1/auth/me", nil) + req.Header.Set("Authorization", header) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + if rec.Code != http.StatusUnauthorized { + t.Errorf("%s: status %d, want 401", name, rec.Code) + } + } + + req := httptest.NewRequest("GET", "/api/v1/auth/me", nil) + req.Header.Set("Authorization", "Bearer "+tok) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Errorf("proper Bearer header: %d", rec.Code) + } +} diff --git a/internal/api/http/handlers_stream.go b/internal/api/http/handlers_stream.go new file mode 100644 index 0000000..44862bb --- /dev/null +++ b/internal/api/http/handlers_stream.go @@ -0,0 +1,51 @@ +package httpapi + +import ( + "errors" + "net/http" + + "github.com/gin-gonic/gin" + + "codeberg.org/squidd/sonda/internal/streaming" +) + +type streamHandlers struct{ svc streamService } + +// stream serves a track's original file. http.ServeContent does the protocol +// work: Range (206, 416), HEAD, Content-Length, conditional requests, and it +// stops copying when the client goes away. +func (h *streamHandlers) stream(c *gin.Context) { + id, ok := pathID(c) + if !ok { + return + } + st, err := h.svc.Open(c.Request.Context(), id) + switch { + case errors.Is(err, streaming.ErrNotFound): + writeError(c, http.StatusNotFound, "track_not_found", "no playable file for this track") + return + case errors.Is(err, streaming.ErrFileMissing): + writeError(c, http.StatusNotFound, "file_missing", "the audio file is not available") + return + case errors.Is(err, streaming.ErrUnsafePath): + // A stored path that leaves the library is a data problem, not the + // client's: log it, but say no more than "not available". + _ = c.Error(err) + writeError(c, http.StatusNotFound, "file_missing", "the audio file is not available") + return + case err != nil: + _ = c.Error(err) + writeError(c, http.StatusInternalServerError, "internal", "internal error") + return + } + defer st.Close() + + hdr := c.Writer.Header() + hdr.Set("Content-Type", st.ContentType) + hdr.Set("X-Content-Type-Options", "nosniff") + // The URL may carry the access token: never leak it through Referer. + hdr.Set("Referrer-Policy", "no-referrer") + hdr.Set("Cache-Control", "private, no-cache") + + http.ServeContent(c.Writer, c.Request, st.Name, st.ModTime, st.File) +} diff --git a/internal/api/http/middleware.go b/internal/api/http/middleware.go index bf03245..2175df9 100644 --- a/internal/api/http/middleware.go +++ b/internal/api/http/middleware.go @@ -32,12 +32,17 @@ func requestLog(logger *zap.Logger) gin.HandlerFunc { return func(c *gin.Context) { start := time.Now() c.Next() - logger.Info("http request", + fields := []zap.Field{ zap.String("method", c.Request.Method), zap.String("path", c.Request.URL.Path), zap.Int("status", c.Writer.Status()), zap.Duration("latency", time.Since(start)), - ) + } + if last := c.Errors.Last(); last != nil { + // Internal errors are hidden from the client; keep them in the log. + fields = append(fields, zap.String("error", last.Error())) + } + logger.Info("http request", fields...) } } @@ -73,12 +78,25 @@ func requireAdmin(users userLookup) gin.HandlerFunc { } } -// requireAuth accepts only "Authorization: Bearer ". -func requireAuth(tokens tokenParser) gin.HandlerFunc { +// requireAuth checks the access token. It reads "Authorization: Bearer "; +// when allowQuery is true and the header is absent it also accepts the +// access_token query parameter. That is for routes loaded by an