Auth Methods
Each auth collection declares an ordered list of methods that say how a request can prove identity for that collection. The four method types compose to express anything from “standard password user” to “machine-to-machine with API keys only”.
crap.collections.define("users", {
auth = {
enabled = true,
methods = crap.auth.default_methods(), -- password_login + bearer + session_cookie
},
})
The four method types
| Type | Purpose | Default surfaces | Notes |
|---|---|---|---|
password_login | Enables the Login RPC; issues JWTs. Owns the password-only knobs (mfa, verify_email, forgot_password). | n/a (Login is one RPC) | At most one per collection. |
bearer | Accept JWTs in Authorization: Bearer … (HTTP) / gRPC metadata. | {"grpc", "admin"} | At most one per collection. |
session_cookie | Accept the crap_session cookie. | {"admin"} | Admin-HTTP only in practice. |
strategy | Custom Lua authenticator (API keys, SSO headers, mTLS). Declares its own activates_on discriminator. | {"admin"} | Any number per collection. |
Surface scoping
Every non-password_login method takes a surfaces array listing the host transports it applies to. Today’s surfaces are "admin" and "grpc". A method whose surfaces doesn’t include the current request’s surface is skipped.
This lets you express:
-- Bearer JWT works on both surfaces, cookie only on admin:
{ type = "bearer", surfaces = { "grpc", "admin" } },
{ type = "session_cookie", surfaces = { "admin" } },
-- API-key strategy only for gRPC clients:
{ type = "strategy",
name = "svc-key",
authenticate = "hooks.auth.svc_key",
activates_on = { header = "x-service-key" },
surfaces = { "grpc" } },
Activation discriminators (strategy only)
A strategy method MUST declare when it fires. Two forms:
activates_on = { header = "x-api-key" }
The strategy is invoked only when the named header (lowercase) is present on the request. Strategy returns nil if it can’t authenticate; the evaluator moves on.
This is the safe, common case. Each strategy is bound to its own header, so two collections both listing API-key strategies on different headers cannot accidentally authenticate each other’s principals.
activates_on = { always = true }
The strategy is invoked on every request that passes the surface filter. The strategy itself decides per-request whether to authenticate. This is the escape hatch for:
- mTLS / TLS client cert — the credential isn’t a header, it’s on the connection.
- Multi-signal strategies — strategy looks at several headers + a query param and composes.
- External IdP introspection — strategy sends the request to an IdP and lets it decide.
Always-active strategies emit a startup warning since accidental always-on patterns run on every request. Use a header discriminator unless you specifically need the catch-all.
activates_on = { always = false } is rejected at config-load time — it has no useful meaning. To disable a method, remove it from methods.
Default and helper methods
crap.auth.default_methods() returns the standard set:
{
{ type = "password_login" },
{ type = "bearer", surfaces = { "grpc", "admin" } },
{ type = "session_cookie", surfaces = { "admin" } },
}
crap.auth.with_defaults(extras) returns default_methods() with the extras list appended — the usual shape for “standard auth plus my strategy”:
auth = {
enabled = true,
methods = crap.auth.with_defaults({
{ type = "strategy",
name = "api-key",
authenticate = "hooks.auth.api_key",
activates_on = { header = "x-api-key" },
surfaces = { "grpc" } },
}),
}
Evaluation order
For each incoming request:
- The evaluator walks every auth collection’s
methodsin declaration order. - For each method, it checks (a) the surface filter matches and (b) the activation discriminator matches the request.
- The first method that matches and produces a principal wins; the request proceeds authenticated as that principal.
- If no method matches, the request is anonymous.
Bearer JWTs unambiguously identify their issuing collection (claims include the collection slug), so JWTs from collection A never authenticate as collection B even if both have bearer in their methods.
Common patterns
Password user collection
auth = {
enabled = true,
methods = crap.auth.default_methods(),
}
Password user + API key for gRPC clients
auth = {
enabled = true,
methods = crap.auth.with_defaults({
{ type = "strategy", name = "api-key",
authenticate = "hooks.auth.api_key",
activates_on = { header = "x-api-key" },
surfaces = { "grpc" } },
}),
}
Machine-to-machine — API key only, no password login, no JWT
auth = {
enabled = true,
methods = {
{ type = "strategy", name = "svc-key",
authenticate = "hooks.auth.svc_key",
activates_on = { header = "x-service-key" },
surfaces = { "grpc" } },
},
}
With no password_login, the Login RPC for this collection returns INVALID_ARGUMENT. With no bearer, JWTs aren’t accepted (and aren’t issued — there’s no password_login to issue them). The API key is the only path in.
Admin-only-with-MFA, gRPC clients use Bearer
auth = {
enabled = true,
methods = {
{ type = "password_login", mfa = "email", verify_email = true },
{ type = "bearer", surfaces = { "grpc", "admin" } },
{ type = "session_cookie", surfaces = { "admin" } },
},
}
Validation rules (enforced at startup)
enabled = truewithmethods = {}→ error.- More than one
password_login→ error. - More than one
bearer→ error. strategywithoutactivates_on→ error (auto-defaults toalways = truein the parser, but the startup warning fires).- Any
strategywithactivates_on = { always = true }→ warning in the logs. - Multiple always-active strategies on the same surface → louder warning (auth depends on registration order).
See also
- Custom Strategies — the Lua function contract,
ctx.headers, returning a user document. - Auth Collections — the collection-level schema concerns.
- Login Flow — what the
LoginRPC actually does, MFA, password verification.