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.
| Client | Headers the browser sends automatically | What the API returns |
|---|---|---|
| Browser / Auth UI | Origin on mutations, Sec-Fetch-* on navigations | HttpOnly session cookie + readable CSRF cookie; JSON has no token |
| CLI / Android / Meloamp | none of the above | opaque bearer in JSON; no cookies |
Rationale and alternatives are recorded in ADR 0009.
Browser rules
- Call the API with
credentials: "include"from the same origin as the API (MELODEE_PUBLIC_BASE_URL). Cross-origin deployments are not supported. - Every mutating request must send
X-CSRF-Tokenequal to themelodee_csrfcookie value. The cookie itself isSameSite=Lax,Path=/, andSecurewhenever the public base URL is HTTPS. - Never copy the session value into
localStorage,sessionStorage, a URL, or application state. It is intentionally unreadable from JavaScript. - 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 carriesaccountId,rotateAtMillisandsessionExpiresAtMillisonly. - Refresh with
POST /api/v1/auth/refresh, no body, same origin. The response rotates both cookies. A cookie refresh from a differentOriginis403. - Logout with
POST /api/v1/auth/logout; it clears both cookies and revokes the session family.
Native rules
- Store the bearer in the platform keystore (Android Keystore, systemd-creds, macOS Keychain), never in a plain file or log.
POST /api/v1/auth/refreshwith{"refreshToken":"<current token>"}returnsaccessToken. Use single-flight refresh: concurrent refreshes have exactly one winner and the loser's replay revokes the whole family.- Access credentials rotate after 15 minutes; the family expires after 90 days.
A
401means "refresh once, then re-authenticate"; retrying a401without refreshing cannot succeed. - Device login uses the device-authorization grant and returns a scoped
credential; Google native sign-in uses
/auth/google/sign-in/noncefollowed by/auth/google/sign-in/token. - 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_URLto the HTTPS origin; cookieSecureand the Google return-URL check derive from it. - If
MELODEE_AUTH_BROWSER_RETURN_URLis set it must share the public origin and is used as the303target after a Google browser sign-in. /readyzreports 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, optionallabel, timestamps, counter, transports and backup state. - No contact address, token digest or storage path in account administration responses.