From 4cb50d822a3c57b9c7c4e44321e48241352d45cc Mon Sep 17 00:00:00 2001 From: Randy Hammond Date: Sat, 25 Apr 2026 19:22:47 +0000 Subject: [PATCH] feat(auth): add os_session cookie + dual-auth on /api/calls/:id/audio MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - New auth.SetSessionCookie / ClearSessionCookie helpers (HttpOnly, Secure when HTTPS, SameSite=Strict, Path=/api) - POST /api/auth/login and /api/auth/refresh issue os_session alongside the existing access JWT response; POST /api/auth/logout clears it - New middleware.OptionalJWTOrSessionAuth resolves identity from, in priority order: bearer header, os_session cookie (guarded by Sec-Fetch-Site), anonymous - GET /api/calls/:id/audio swapped to the new middleware; every other route is unchanged. Bearer flow continues to work everywhere. - Routes.RegisterRoutes now promotes deps.Hub into the WSDisconnecter interface only when the concrete pointer is non-nil, fixing a pre-existing typed-nil interface footgun on the logout path. Tests: - backend/internal/auth/cookie_test.go: SetSessionCookie / ClearSessionCookie flag matrix - backend/internal/handler/routes/auth_test.go: login/refresh/logout cookie issuance and rotation - backend/internal/handler/routes/audio_test.go (new): full dual-auth matrix on the audio route — bearer, cookie+same-origin, cookie+missing Sec-Fetch-Site, cookie+cross-site (publicAccess on/off), stale cookie fallthrough (publicAccess on/off), anonymous (publicAccess on/off) go vet, go build, go test ./... all clean. Backwards compatible — no frontend changes. --- CHANGELOG.md | 9 + backend/internal/auth/cookie.go | 31 ++ backend/internal/auth/cookie_test.go | 65 ++++ backend/internal/handler/auth/auth.go | 11 + backend/internal/handler/routes/audio_test.go | 325 ++++++++++++++++++ backend/internal/handler/routes/auth_test.go | 112 ++++++ backend/internal/handler/routes/routes.go | 12 +- backend/internal/middleware/auth.go | 80 +++++ 8 files changed, 643 insertions(+), 2 deletions(-) create mode 100644 backend/internal/handler/routes/audio_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 4249266..3d64283 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Session cookie (`os_session`) issued on login and refresh, cleared on + logout. The `GET /api/calls/:id/audio` route now accepts authentication + via either the existing `Authorization: Bearer` header or the new + cookie, so `