hb-auth
Identity and permissions for Cloudflare Workers.
hb-auth provides drop-in Cloudflare Access JWT validation with a strongly-typed permission DSL. It handles key rotation, signature verification, and identity extraction so you can focus on your business logic.
Part of the hb stack.
Features
- ✅ Zero-Config Validation – Automatically fetches and caches JWKS from your Cloudflare Access team domain.
- ✅ Type-Safe Identity – Extract a strongly-typed
Userstruct from requests. - ✅ Role-Based Access – Map Access Groups (e.g., "super-admins") to internal Rust enums automatically.
- ✅ Framework Agnostic – First-class support for
axum(via extractors) and rawworker::Request. - ✅ Optional KV Caching – Opt-in cross-isolate JWKS caching via Cloudflare KV.
Installation
[]
= { = "0.1", = ["axum"] }
Optional: KV-backed JWKS Caching
By default, hb-auth caches JWKS keys in memory. Since Workers isolates are ephemeral, this cache is lost on restart. Enable the kv feature for cross-isolate caching:
[]
= { = "0.1", = ["axum", "kv"] }
This stores JWKS keys in Cloudflare KV with a 10-minute TTL, so all isolates share the same cached keys.
Quick Start (Axum)
1. Configure
In your router setup, initialize the config and add it to your state. Your state must implement HasAuthConfig.
use ;
2. Protect Routes
Add auth: User to your handler arguments. The handler will only run if a valid Cloudflare Access JWT is present.
use User;
async
Setting Up KV Caching
If you enabled the kv feature, you need to:
1. Add a KV Namespace
In your wrangler.toml:
[[]]
= "AUTH_CACHE"
= "<your-kv-namespace-id>"
2. Implement HasJwksCache
Your state must implement HasJwksCache to provide the KV binding:
use ;
The extractor automatically uses KV when available, falling back to in-memory caching if jwks_kv() returns None.
Advanced: Role-Based Access
You can map Cloudflare Access Groups (available in the JWT groups claim) to your own internal roles.
1. Define Roles
use ;
2. Enforce Permissions
Use User<Role> instead of the default User.
async
Usage with Raw Workers
If you aren't using Axum, you can still use hb-auth to validate requests.
use User;
async
With KV caching enabled:
use User;
async
Configuring Cloudflare Access
To get the groups claim in your JWT:
- Go to Zero Trust Dashboard > Access > Applications.
- Edit your application.
- Under Settings (or "Overview" -> "Edit"), find OIDC Claims or Additional Settings.
- Enable Groups (this might require adding "groups" to the scope depending on your configuration).
- Ensure the groups you want to map are assigned to the application policy.
The audience (AUD) is found in the Overview tab of your Access Application.
License
MIT