Skip to main content

Module preview_token

Module preview_token 

Source
Expand description

Signed preview tokens for decoupled / headless frontends.

The public API serves published pages only (404 on drafts). A preview token lets an authorized decoupled frontend fetch a page’s draft content: the admin mints a short-lived signed token for a page, the frontend appends it to its API request (/api/v2/pages/{id}/?preview_token=…), and the API serves the page regardless of status when the token verifies.

Token shape: "{page_id}.{expires_unix}.{sig}", where sig is the first 16 hex chars of HMAC-SHA256 over "{org_id}:{tenant_slug}:{page_id}.{expires_unix}" keyed by crate::signing::secret. The expiry is signed, so it can’t be extended by tampering. The tenant is signed but not carried: every tenant numbers its pages from 1, so a token must verify only on the tenant that minted it, and not on a later tenant reusing its slug.

Functions§

mint
Mint a preview token for page_id that expires at expires_unix (UNIX seconds).
verify
Verify token for tenant at now_unix; returns the page id when the signature is valid for this tenant AND the token hasn’t expired. None when the token is malformed/forged, was minted for another tenant, or has expired.