Smeltery
Get started

Hallmark (API tokens)

v0.1

SPA authentication

A JavaScript frontend served from the app's own origin (from public/, or through a proxy or a Vite dev server under the same origin) can call auth:hallmark API routes with the session cookie instead of a token:

rust
use smeltery::hallmark::{Hallmark, HallmarkExt as _};

fn build(app: smeltery::AppBuilder) -> smeltery::AppBuilder {
    app.hallmark(Hallmark::new().spa().stateful(&["http://localhost:5173"]))
}
  • .spa() (or HALLMARK_SPA=true) turns it on. A request on an auth:hallmark API route is first-party when it carries Sec-Fetch-Site: same-origin (a page of APP_URL's origin); or Sec-Fetch-Site: same-site / cross-site with one Origin listed with .stateful(...) / HALLMARK_STATEFUL (comma-separated scheme://host[:port]; scheme, host and port compared exactly); or, from a browser that sends no Sec-Fetch-Site, one Origin (else the Referer's origin) equal to APP_URL's origin or a listed one. An unlisted sibling subdomain or port, Sec-Fetch-Site: none, a null or foreign Origin, and requests without any of these headers (an app or server calling with a token) are not first-party. A *, null or a URL with a path in HALLMARK_STATEFUL stops the app at boot.
  • A listed origin is a page of the same site on another origin, such as the Vite dev server above on another port of localhost: it calls the API with fetch(url, { credentials: "include" }) (the session cookie is SameSite=Lax, so it is sent only to the same site). Smeltery sends no CORS headers, so such a page reads the answers only when the app adds them; a proxy that serves the page and the app under one origin needs neither.
  • List only origins you control: any script on a listed origin acts with the signed-in user's session. A development origin such as http://localhost:5173 belongs in the development .env only, never in production.
  • A first-party request runs the web session stack: the session cookie, remember-me, the password binding, and the CSRF check of web routes on every method except GET, HEAD, OPTIONS and TRACE (419 without a valid X-XSRF-TOKEN, X-CSRF-TOKEN or _token). A signed-in session passes (its principal is the web guard's, with every ability); without one, a bearer token in the request is still checked. The CSRF check comes first, so a first-party page that sends a token instead still needs X-XSRF-TOKEN on state-changing calls (419 without it), and when a first-party request carries both a signed-in session and a token, the session decides who it is. Every other request is bearer-only: the session cookie is not read.
  • GET /hallmark/csrf-cookie (route hallmark.csrf-cookie) answers 204 and sets the XSRF-TOKEN cookie, which SPA mode turns on for every web response. The SPA reads it, signs in through the app's login route, and sends the cookie's value as X-XSRF-TOKEN on state-changing API calls. Session cookies keep their __Host- prefix under https, so a frontend on another subdomain or site uses tokens.

↑↓ moveEnter openEsc close