Skip to main content

Module authn

Module authn 

Source
Expand description

The deny-by-default authentication middleware (ADR 0006).

It runs ahead of every route’s permission_guard, turning an Authorization: Bearer <token> into the Principal the guard and Scoped read:

  • No credential header → Principal::anonymous is inserted, so public routes (/health, /docs, /openapi.json) still serve and a guarded route’s permission_guard answers 403.
  • A present-but-invalid credential (bad signature, wrong aud/iss, expired, unknown/revoked key, disallowed algorithm, or an empty/garbled header) → 401 RFC 9457 Problem with a WWW-Authenticate header. The request never reaches the route.

Two credential sources, each with its own authenticator, run side by side (ADR 0006): an opaque X-API-Key (checked first) and Authorization: Bearer. With the mtls feature a third comes first (ADR 0047): the client certificate the TLS handshake verified, mapped by a CertificateAuthenticator (seiva_http::mtls). A request carrying a verified certificate and a header credential is refused (401): one request, one identity. The Authenticator is the only authority on identity; the middleware never trusts a header or body field to set the subject, tenant or permissions.

Structs§

AuthConfig
App-level authentication the router_with_* builders mount: an optional Authorization: Bearer authenticator and an optional X-API-Key one, each admitted only when set (ADR 0006). This is the config an apps/<name>-api composition root hands the framework.
Authn
The authenticators the authn middleware consults: an optional Authorization: Bearer path (JWT or static token) and an optional opaque X-API-Key path running alongside it (ADR 0006). At least one is set — the middleware is never mounted when both are absent (see AuthConfig).

Functions§

authn
Middleware that authenticates the caller and inserts a Principal.