Auth Overview
Built-in auth middleware provides bridge patterns for WordPress REST routes.
Available middleware
BetterRoute\Middleware\Jwt\JwtAuthMiddlewareBetterRoute\Middleware\Jwt\Rs256JwksJwtVerifier(v0.6.0) — RS256/ES256 verifier backed by JWKS; see JWKS (RS256 / ES256)BetterRoute\Middleware\Jwt\HttpJwksProvider/StaticJwksProvider(v0.6.0)BetterRoute\Middleware\Auth\BearerTokenAuthMiddlewareBetterRoute\Middleware\Auth\HmacSignatureMiddleware(v0.6.0) — HMAC request signatures with replay-window enforcement; see HMAC Request SignaturesBetterRoute\Middleware\Auth\CookieNonceAuthMiddlewareBetterRoute\Middleware\Auth\ApplicationPasswordAuthMiddlewareBetterRoute\Middleware\Auth\WpClaimsUserMapperBetterRoute\Middleware\Auth\OwnershipGuardMiddleware(v0.5.0) — see Ownership guards
Context propagation
On success, middleware writes auth context attributes:
auth(provider, userId, subject, scopes)- optionally
claims,userId,user,scopes hmac(v0.6.0) —keyIdandalgorithmafter successful HMAC signature verificationsingleUseToken(v0.6.0) — issuer-supplied context after a single-use token is consumed
Important policy note
Even with auth middleware, route registration still requires explicit permission_callback (by design in dispatcher integration). For middleware-authenticated routes, declare the intent with ->protectedByMiddleware(...) (since 0.4.0). For routes guarded by HMAC or single-use tokens (no WordPress user behind them), use ->publicRoute() and let the middleware be the gate.
v0.6.0 additions
- Asymmetric JWT.
Rs256JwksJwtVerifierplusHttpJwksProviderandStaticJwksProviderfor OIDC-style providers. Strictkidmatching, algorithm pinning, private-field stripping. See JWKS (RS256 / ES256). - HMAC request signatures.
HmacSignatureMiddlewareplusHmacSecretProviderInterfaceandArrayHmacSecretProvider. Canonical input, replay window, multi-key rotation. See HMAC Request Signatures. - Single-use tokens.
SingleUseTokenMiddlewareplus three stores. See Single-Use Tokens.
v0.5.0 additions
OwnershipGuardMiddlewarefor routes where the authenticated user may only access their own object. DefaultdeniedStatusis404to avoid leaking existence.Resource\OwnedResourcePolicy::currentUserOwns()— Resource DSL preset that wires the URLid, the auth identity, and an optionalbypassCapabilitytogether. See Ownership guards.
v0.3.0 hardening
Hs256JwtVerifierrequiresexpby default and supportsexpectedIssuer,expectedAudience,maxLifetimeSeconds, andmaxTokenLength. See JWT and Bearer.WpClaimsUserMapperremoved'sub'from defaultidClaims— re-add it explicitly when needed.BearerTokenAuthMiddlewareno longer leaks the verifier exception message; failed tokens uniformly return401 invalid_tokenwith nodetails.reason.
Common mistakes
- Relying on middleware alone without route permissions
- Missing
Authorizationheader normalization - Not mapping JWT/Bearer claims to WP user where needed
Validation checklist
401on missing/invalid credential403on missing required scopes- expected auth attributes exist in
RequestContext