Add optional API key auth and allowed_hosts enforcement

- AuthConfig (api_key, allowed_hosts) read from the shared config; router_with_auth
  applies an axum middleware before any handler.
- Host not in allowlist -> 403 host_not_allowed; missing/wrong key -> 401
  invalid_api_key (accepts Authorization: Bearer <key> or x-api-key); /health open.
- router() stays the open default: backward compatible (no key, wildcard hosts).
- The non-loopback bind warning now fires only when no api_key is configured.
- README updated (auth is a feature, no longer a gap).

4 new tests; 80 green + 2 real-file ignored; clippy clean; cargo fmt applied.
This commit is contained in:
adm committed 2026-11-24 18:48:05 +01:00
1 parent 5bafd000c8
commit b6619d30de
6 files changed
+227 -20

No files matched your search

+1 -1
View File
@@ -11,6 +11,6 @@ pub mod stop;
pub mod types;
pub use formatter::{Decoder, Formatter, Piece, Prompt};
pub use routes::router;
pub use routes::{router, router_with_auth, AuthConfig};
pub use service::ChatService;
pub use types::{ChatMessage, ChatRequest, ChatResponse, ModelList, Role};