Skip to main content

Module auth

Module auth 

Source
Expand description

HTTP Basic authentication helpers shared by the H1 and H2 mux paths.

The runtime stores credentials as username:hex(sha256(password)) entries on each cluster’s authorized_hashes. On every request that traverses a frontend with required_auth = true, the mux extracts the Authorization: Basic <token> header from the front kawa, decodes the base64 token, splits on the first : into <user>:<password>, hashes the password with SHA-256, and rebuilds the canonical <user>:<hex(sha256)> form. Comparison against the cluster’s hash list uses subtle::ConstantTimeEq over a full pass, never short-circuiting, so the time spent validating a credential does not leak which slot matched (or whether any did at all).

The extractor is intentionally permissive (Option-returning) — any malformed input is reported as “no credential”, and the caller emits the standard 401 response. We never panic on hostile input.

Functions§

canonicalize_basic_credentials
Decode a Basic <token> value into the canonical username:hex(sha256(password)) shape that check_authorized_hashes compares against. Returns None for any malformed input — wrong scheme, non-base64 token, missing :, non-UTF-8 username, or oversized payload.
check_authorized_hashes
Compare candidate against every entry in authorized_hashes using constant-time equality. Returns true if any entry matches.
check_basic
Convenience: pull Authorization from the kawa, canonicalise, and compare in constant time against the authorized list.
extract_authorization_header
Find the first Authorization header value in the front kawa.
set_max_decoded_credential_bytes
Install the operator-configured cap. Called from lib::server::Server::try_new_from_config exactly once per worker process. Subsequent calls are no-ops (the OnceLock rejects the second set); the first wins. A 0 value is treated as “use the built-in default” so an operator config that explicitly sets 0 does not disable Basic-auth length-bound protection by accident.