Expand description
Bearer-key authentication and per-request metering (Phase 2 design §4, §5).
This module is the only place a secret is ever visible: handlers receive a
KeyId (and, since paid-path S2, the key’s KeyPlan) and nothing
else, so a handler cannot leak a secret by accident and the metering
layer has a stable identity to record without touching the credential.
Order of operations per request:
- Read
Authorizationand require the exact formBearer <key_id>:<secret>. - Verify against the runtime
KeyStorein constant time. The store is read on every request, so issuance, rotation, and revocation take effect without a restart (seecrate::keys). - On success, attach
KeyIdandKeyPlanto the request extensions. - Enforce the plan’s monthly query quota against the metering log
(paid-path S3): a key at its limit is answered
429and, like a401, is not metered. - Run the inner service.
- Measure the response body and append exactly one metering record.
A rejected request (step 1 or 2) is answered 401 and is not metered. See
the deviation note in crate::metering.
Structs§
- Auth
Metering - Everything the auth + metering layer needs, shared by reference.
- KeyId
- The authenticated
key_id, attached to the request by [auth_and_meter]. - KeyPlan
- The authenticated key’s plan, attached beside
KeyIdby [auth_and_meter].
Constants§
- MAX_
RESPONSE_ BYTES - Largest response body the middleware will buffer in order to measure it.
Functions§
- metered
- Wraps
routerso every route requires a bearer key and is metered.