Skip to main content

Browser and native sessions

Imported component documentation. Source: melodee-project/melodee-api at commit 0267c458207082337d9baf1130deeb65f43c9d4a. Version 1.0.1. License MIT. Status: implemented-and-automatically-verified candidate; not published, deployed or production-qualified. This is the component's committed documentation, not proof of a distribution release or production qualification.

Melodee uses one session engine with two credential presentations. Which one you get is decided by the request headers, never by a JavaScript-settable field.

ClientHeaders the browser sends automaticallyWhat the API returns
Browser / Auth UIOrigin on mutations, Sec-Fetch-* on navigationsHttpOnly session cookie + readable CSRF cookie; JSON has no token
CLI / Android / Meloampnone of the aboveopaque bearer in JSON; no cookies

Rationale and alternatives are recorded in ADR 0009.

Browser rules​

  1. Call the API with credentials: "include" from the same origin as the API (MELODEE_PUBLIC_BASE_URL). Cross-origin deployments are not supported.
  2. Every mutating request must send X-CSRF-Token equal to the melodee_csrf cookie value. The cookie itself is SameSite=Lax, Path=/, and Secure whenever the public base URL is HTTPS.
  3. Never copy the session value into localStorage, sessionStorage, a URL, or application state. It is intentionally unreadable from JavaScript.
  4. Sign-in responses (/auth/magic-links/confirm, /auth/recovery/confirm, /auth/passkeys/sign-in/finish, /auth/google/sign-in/callback, /auth/bootstrap) set the cookies. Their JSON carries accountId, rotateAtMillis and sessionExpiresAtMillis only.
  5. Refresh with POST /api/v1/auth/refresh, no body, same origin. The response rotates both cookies. A cookie refresh from a different Origin is 403.
  6. Logout with POST /api/v1/auth/logout; it clears both cookies and revokes the session family.

Native rules​

  1. Store the bearer in the platform keystore (Android Keystore, systemd-creds, macOS Keychain), never in a plain file or log.
  2. POST /api/v1/auth/refresh with {"refreshToken":"<current token>"} returns accessToken. Use single-flight refresh: concurrent refreshes have exactly one winner and the loser's replay revokes the whole family.
  3. Access credentials rotate after 15 minutes; the family expires after 90 days. A 401 means "refresh once, then re-authenticate"; retrying a 401 without refreshing cannot succeed.
  4. Device login uses the device-authorization grant and returns a scoped credential; Google native sign-in uses /auth/google/sign-in/nonce followed by /auth/google/sign-in/token.
  5. Playback continuity: refresh before the rotation deadline and keep the current ranged stream request alive. A rotated credential does not interrupt an in-flight byte range; a fully expired family does require re-authentication.

Operator requirements​

  • Serve the API and the Auth UI from the same origin (for example one reverse proxy path) and preserve Origin/Sec-Fetch-* headers end to end. Rewriting or stripping them would downgrade a browser to the native response shape.
  • Set MELODEE_PUBLIC_BASE_URL to the HTTPS origin; cookie Secure and the Google return-URL check derive from it.
  • If MELODEE_AUTH_BROWSER_RETURN_URL is set it must share the public origin and is used as the 303 target after a Google browser sign-in.
  • /readyz reports database and writable-volume readiness, not session state.

What is not exposed​

  • No persistent browser bearer, refresh credential or session id in JSON, URLs or browser storage.
  • No protected-object ids (passkey public-key objects) or credential ids in the passkey list; only id, optional label, timestamps, counter, transports and backup state.
  • No contact address, token digest or storage path in account administration responses.