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: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 CoolContext for schema policy enforcement.
Internal callers
Non-HTTP Rust callers can bind auth directly:bind_context(...) when the host already has a CoolContext.
Principal projection
bind_auth(...) accepts any serde::Serialize principal that serializes to a JSON object.
Example:
CoolContext 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:
CoolContext::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.