feat(api): short-lived signed media tokens for cast (spec 060 T001-T004) #1220
Loading…
Reference in a new issue
No description provided.
Delete branch "feature/060-cast-api"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Backend lane of spec 060 (Android cast to Chromecast): short-lived signed media tokens so Google's Default Media Receiver can fetch media URLs itself — it cannot carry the phone's session cookie. This is a minimal slice of draft spec 035; all other clients stay on cookie auth, no DB migration, no new dependencies.
Token contract (for the Android team)
Mint
Allowed renditions:
original,preview,thumbnail,video_rendition.For the cast photo URL use rendition
preview(the display/fullsize rendition — there is no separate display endpoint; it isGET /api/media/{id}/derived/preview), for videovideo_rendition(fallbackoriginal).Use
st→ 401.st).?st=outside these two endpoints is ignored → normal auth flow (401 unauthenticated).Token properties
base64url("mediaItemId|rendition|exp") + "." + base64url(HMAC-SHA256(payload)),exp= unix seconds.MEMORIES_APP_SECRET(already required non-placeholder by setup readiness) with a context prefix — no new secret to provision.memories.media-token.ttl(MEMORIES_MEDIA_TOKEN_TTL), default 15 min. Mint a fresh token perLOAD; TTL only bounds token leakage, not session length.Security model
MediaTokenAuthenticationFilterruns inside the security chain after session auth, so an already-authenticated request (cookie/bearer) is never touched; no permitAll added —/api/**stays deny-by-default.MediaAuthorizationSqlpredicate the byte endpoints use, so a token is a time-boxed capability minted only for visible items.Tests
rtk ./gradlew :apps:api:test→ BUILD SUCCESSFUL — 2024 tests, 0 failures, 0 errors (9 pre-existing skips).MediaTokenServiceTest: mint payload format, verify round trip, tampered payload/signature, expiry, item/rendition binding, TTL configuration.MediaTokenAccessTest(MockMvc against the realSecurityFilterChain): valid token 200 on original + derived, expired 403, garbage 403, token-for-A-on-B 403, rendition mismatch 403, missing both 401, cookie regression 200, cookie-wins-over-garbage-st 200,stignored on non-allowlisted endpoints 401, mint contract 200/400/401/404.SecurityConfigurationTest,StoragePathsAccessMatrixTest,FirstAdminRemovalContractTest) updated for the new filter bean; contract tests asserting no newpermitAllstill pass.Chromecast's Default Receiver fetches media URLs itself and cannot carry the phone's session cookie. Minimal slice of draft spec 035: - MediaTokenService: HMAC-SHA256 over base64url("mediaItemId|rendition|exp") keyed from memories.app-secret (context-prefixed), stateless verification, default TTL 15 min (memories.media-token.ttl / MEMORIES_MEDIA_TOKEN_TTL). - POST /api/media/token mints a token for one item + rendition; requires media:read and only mints for items the caller may see (404 otherwise). - GET /api/media/{id}/original and GET /api/media/{id}/derived/{type} accept ?st=<token> as an ALTERNATIVE auth via MediaTokenAuthenticationFilter, registered after session auth so a valid cookie always wins. Invalid or expired tokens on those paths are answered 403; every other endpoint keeps its existing auth behavior (st is ignored outside the two paths). Cookie/API-key auth paths are unchanged; the viewer-permission predicate is skipped only for token-authenticated reads (the token is a time-boxed capability for exactly one item + rendition).