Skip to content

Authentication & Tokens

Headless ships five composable authentication modes (Totoglu\Headless\Auth\AuthMode) plus signed preview tokens for viewing unpublished pages and draft entries safely.

AuthMode Guard Class Header / Mechanism Best For
AuthMode::Public N/A No auth header required Public catalogs, marketing pages, OpenAPI schemas
AuthMode::ApiKey ApiKeyGuard X-Api-Key: whk_... or Authorization: Bearer whk_... Server-to-server builds, static site generators, partner integrations
AuthMode::BearerJwt BearerJwtGuard Authorization: Bearer <hs256-jwt> issued by POST /auth/login or issueJwtToken() Mobile apps and SPAs using short-lived JWTs + rotating whr_* refresh tokens
AuthMode::Hmac HmacGuard X-Headless-Signature, X-Headless-Timestamp, X-Headless-Nonce High-security server-to-server integrations requiring body integrity + replay protection
AuthMode::Session SessionCookieGuard Logged-in ProcessWire session + X-CSRF-Token on POST/PUT/PATCH/DELETE Same-origin hybrid frontends and admin preview widgets
API Keys lists scoped whk_* Personal Access Tokens with per-key rate limits, expiration dates, and last-used timestamps.

Each key binds a ProcessWire user, explicit scopes (e.g., pages:read, entries:read, pages:write), an optional custom rate limit, and an expiration date. Only key_prefix and key_hash are persisted.

2. JWT Access Tokens & Rotating Refresh Tokens (whr_*)

Section titled “2. JWT Access Tokens & Rotating Refresh Tokens (whr_*)”
Refresh Tokens tracks active whr_* sessions issued via POST /api/v1/auth/login with atomic rotation and reuse detection.
  • POST /api/v1/auth/login: Exchanges ProcessWire credentials (throttled) for an HS256 JWT (jwt_ttl, default 3600s) and a whr_* refresh token (refresh_ttl).
  • POST /api/v1/auth/refresh: Atomically consumes and rotates the whr_* token; if a previously consumed token is replayed, reuse detection revokes the user’s token family immediately.
  • POST /api/v1/auth/logout & GET /api/v1/auth/me: Revoke the active refresh token or inspect the active HeadlessPrincipal (id, name, email, scheme, scopes, roles).

3. HMAC Signatures (HmacGuard) & Preview Tokens

Section titled “3. HMAC Signatures (HmacGuard) & Preview Tokens”
  • HmacGuard: Verifies X-Headless-Signature, X-Headless-Timestamp (default 300s tolerance), and X-Headless-Nonce, recording used nonces in WireCache (HlSecurity) to prevent replay attacks. Compute signatures in PHP with HmacGuard::computeSignature().
  • Preview Tokens: Issue time-limited draft/unpublished preview tokens via $headless->issuePreviewToken($target, 3600, 'pages') and pass ?preview_token=... on GET /pages/{id} or GET /entries/{type}/{id}.

Next steps