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:
use smeltery::hallmark::{Hallmark, HallmarkExt as _};
fn build(app: smeltery::AppBuilder) -> smeltery::AppBuilder {
app.hallmark(Hallmark::new().spa().stateful(&["http://localhost:5173"]))
}
.spa()(orHALLMARK_SPA=true) turns it on. A request on anauth:hallmarkAPI route is first-party when it carriesSec-Fetch-Site: same-origin(a page ofAPP_URL's origin); orSec-Fetch-Site: same-site/cross-sitewith oneOriginlisted with.stateful(...)/HALLMARK_STATEFUL(comma-separatedscheme://host[:port]; scheme, host and port compared exactly); or, from a browser that sends noSec-Fetch-Site, oneOrigin(else theReferer's origin) equal toAPP_URL's origin or a listed one. An unlisted sibling subdomain or port,Sec-Fetch-Site: none, anullor foreignOrigin, and requests without any of these headers (an app or server calling with a token) are not first-party. A*,nullor a URL with a path inHALLMARK_STATEFULstops 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 withfetch(url, { credentials: "include" })(the session cookie isSameSite=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:5173belongs in the development.envonly, 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-TOKENor_token). A signed-in session passes (its principal is thewebguard'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 needsX-XSRF-TOKENon 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(routehallmark.csrf-cookie) answers 204 and sets theXSRF-TOKENcookie, 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 asX-XSRF-TOKENon state-changing API calls. Session cookies keep their__Host-prefix under https, so a frontend on another subdomain or site uses tokens.