Add basic-auth example: the simplest whole-site auth gate - #8
Merged
Conversation
`ephpm-middleware-basic-auth` — an HTTP Basic (RFC 7617) gate, added as the simplest auth-gate teaching piece alongside `api-key`. Verifies an `Authorization: Basic` credential with a constant-time compare (subtle), challenges with 401 + WWW-Authenticate otherwise, and optionally forwards the authenticated username to PHP. Reworked onto ePHPm #408's static-path request gate: because the request phase now runs on the static-file path too (fail-closed, before the file is opened), the gate denies unauthenticated static assets as well as PHP — the #395 fix. `a_static_asset_is_challenged_before_it_is_read` pins it. Relocated in spirit from the held ephpm/ephpm PR #387, trimmed to example scope (no PBKDF2/KV/verification-cache machinery — this teaches the pattern; the compiled-in module keeps the full feature set). Adds `base64ct` to the workspace deps to decode the Basic credential. README updated to list four modules.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
ephpm-middleware-basic-auth— an HTTP Basic (RFC 7617) whole-site gate, the simplest auth-gate teaching piece, alongsideapi-key.What it teaches
Authorization: Basic <base64(user:pass)>(scheme token case-insensitive).subtle::ConstantTimeEq) against every configured credential — no early return, so neither the match position nor which usernames exist leaks via timing.401+WWW-Authenticate: Basic realm="…"challenge otherwise (browser login dialog).forward_user_headerto REWRITE the authenticated username into a request header for PHP (our SAPI does not populatePHP_AUTH_USER).Reworked onto the #408 static gate
Since ePHPm #408 the request phase runs on the static-file path too (fail-closed, before the file is opened), so this gate denies unauthenticated static assets as well as PHP — the #395 fix.
a_static_asset_is_challenged_before_it_is_readpins it: an unauthenticated request for/assets/app.jsgets the 401 and the bytes are never served, while a valid credential lets the same asset through.Scope
Relocated in spirit from the held ephpm/ephpm PR #387 and trimmed to example scope — no PBKDF2 hashing, KV-sourced per-site credentials, or verification cache. This teaches the pattern (like
api-key); the compiled-in official module keeps the full feature set. Passwords are plaintext-in-config for clarity, with a doc note that production should store a slow hash.Housekeeping
base64ctto the workspace deps (tiny, no_std, already used across the ePHPm middleware) to decode the Basic credential.basic-authas the "start here" gate.Verification
Local, against the pinned host ABI:
cargo +nightly fmt --all -- --check,cargo clippy --workspace --all-targets -- -D warnings,cargo test -p ephpm-middleware-basic-auth(12 tests), andcargo build -p ephpm-middleware-basic-auth --releaseall clean.