Auth Provider
CrateStack does not authenticate requests itself. The host application is responsible for:- reading headers, cookies, bearer tokens, signed requests, or other transport-specific auth material
- validating that material
- projecting the authenticated principal into a CrateStack auth context
cratestack::AuthProvider.
Trait shape
Implement one trait per host auth strategy:CratestackContext and RequestContext are two distinct types, easy to conflate by name alone: RequestContext is the inbound view (method/path/headers/body) the provider reads from, while CratestackContext is the outbound auth/principal result the provider produces. authenticate(...) takes one and returns the other.
RequestContext gives the provider a framework-owned request view:
methodpathqueryheadersbody
Router usage
Register the provider once when building generated routes:authenticate(...) for each request and use the resulting CratestackContext for schema policy enforcement.
Internal callers
Non-HTTP Rust callers can bind auth directly:bind_context(...) when the host already has a CratestackContext.
Principal projection
bind_auth(...) accepts any serde::Serialize principal that serializes to a JSON object.
Example:
CratestackContext principal surface with two layers:
- a structured principal model with
actor,session,tenant, and free-formclaims - a legacy auth projection so existing
auth().fieldpolicies keep working
auth() shape:
CratestackContext::from_principal(...) promotes them into the structured principal.actor, principal.session, and principal.tenant slots while still projecting the same data back through legacy auth() lookups for schema compatibility.
Current auth-derived create defaults are intentionally conservative:
@default(auth().field)only- nested auth paths like
@default(auth().organization.id)are supported - no arbitrary expressions or function calls inside
@default(...) - currently limited to
String/Cuid,Int, andBooleanmodel fields
../reference/auth-support-matrix.md.
Compatibility note
Exact auth keys still win before dotted traversal. That means older flat claim maps such asorganizationId can continue to work even while newer principals project nested structures such as tenant.id or organization.id.
The recommended direction for new integrations is:
- prefer structured principals with top-level
actor,session, andtenantobjects when those concepts exist - treat ad hoc top-level scalar claims as compatibility or app-specific extensions rather than the long-term principal shape
AuthProvider type so auth behavior stays discoverable and reusable across routers, tests, and internal service boundaries.