use axum::extract::{MatchedPath, Request, State};
use axum::http::header::{AUTHORIZATION, WWW_AUTHENTICATE};
use axum::http::{HeaderMap, StatusCode};
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use axum::Json;
use sha2::{Digest, Sha256};
use super::AppState;
#[derive(Debug, Clone, Default)]
pub(super) struct Policy {
digests: Vec<[u8; 32]>,
required: bool,
}
impl Policy {
pub(super) fn from_env() -> anyhow::Result<Self> {
let policy = Self::with_keys(crate::auth::server_keys().iter().map(String::as_str));
if crate::auth::disabled_by_env() {
return Ok(policy.optional());
}
anyhow::ensure!(
policy.enforced() || crate::daemon::reachable_only_locally(),
"llmman serve is bound to {} (LLMMAN_HOST), which the network can reach, but no \
API key is configured. Set LLMMAN_API_KEYS (or `llmman config set auth.api_keys \
<key>`), or set LLMMAN_AUTH=off to serve everyone who can connect.",
crate::daemon::bind_addr()
);
Ok(policy)
}
pub(super) fn optional(self) -> Self {
Self {
required: false,
..self
}
}
pub(super) fn with_keys<'a>(keys: impl IntoIterator<Item = &'a str>) -> Self {
Self {
digests: keys
.into_iter()
.map(str::trim)
.filter(|k| !k.is_empty())
.map(digest)
.collect(),
required: true,
}
}
pub(super) fn enforced(&self) -> bool {
self.required && !self.digests.is_empty()
}
pub(super) fn accepts(&self, key: &str) -> bool {
let presented = digest(key.trim());
let mut matched = false;
for expected in &self.digests {
matched |= digests_match(&presented, expected);
}
matched
}
}
pub(super) fn digest(key: &str) -> [u8; 32] {
Sha256::digest(key.as_bytes()).into()
}
pub(super) fn digests_match(a: &[u8; 32], b: &[u8; 32]) -> bool {
let mut diff = 0u8;
for (x, y) in a.iter().zip(b) {
diff |= x ^ y;
}
diff == 0
}
pub(super) fn bearer(headers: &HeaderMap) -> Option<&str> {
let (scheme, token) = headers
.get(AUTHORIZATION)?
.to_str()
.ok()?
.trim()
.split_once(' ')?;
scheme.eq_ignore_ascii_case("bearer").then(|| token.trim())
}
pub(super) fn offered_ws_protocol(headers: &HeaderMap) -> Option<String> {
offered_ws_protocol_with(headers, crate::auth::WS_PROTOCOL_PREFIX)
}
pub(super) fn offered_ws_protocol_with(headers: &HeaderMap, prefix: &str) -> Option<String> {
headers
.get_all("sec-websocket-protocol")
.iter()
.filter_map(|v| v.to_str().ok())
.flat_map(|v| v.split(','))
.map(str::trim)
.find(|p| p.starts_with(prefix))
.map(str::to_string)
}
pub(super) async fn require_key(
State(state): State<AppState>,
mut req: Request,
next: Next,
) -> Response {
let policy = &state.0.auth;
if policy.digests.is_empty() || is_ui_asset(&req) {
return next.run(req).await;
}
let headers = req.headers_mut();
let mut admitted = false;
if bearer(headers).is_some_and(|k| policy.accepts(k)) {
headers.remove(AUTHORIZATION);
admitted = true;
}
for header in ["x-api-key", "x-goog-api-key"] {
if headers
.get(header)
.and_then(|value| value.to_str().ok())
.is_some_and(|key| policy.accepts(key))
{
headers.remove(header);
admitted = true;
}
}
if offered_ws_protocol(headers)
.as_deref()
.and_then(crate::auth::ws_protocol_key)
.is_some_and(|k| policy.accepts(&k))
{
admitted = true;
}
if admitted || !policy.enforced() {
next.run(req).await
} else {
refused()
}
}
fn is_ui_asset(req: &Request) -> bool {
let path = req.uri().path();
(matches!(*req.method(), axum::http::Method::GET | axum::http::Method::HEAD)
&& (path == "/" || path.starts_with("/ui/")))
|| (req.extensions().get::<MatchedPath>().is_none()
&& super::webui::is_page(req.method(), path, req.headers()))
}
fn refused() -> Response {
let body = serde_json::json!({
"error": "this llmman serve requires an API key: send it as `Authorization: Bearer <key>` \
or `x-api-key: <key>` or `x-goog-api-key: <key>` (for the CLI, set LLMMAN_API_KEY)"
});
(
StatusCode::UNAUTHORIZED,
[(WWW_AUTHENTICATE, "Bearer realm=\"llmman\"")],
Json(body),
)
.into_response()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_policy_accepts_exactly_its_keys() {
let p = Policy::with_keys(["alpha", " beta ", ""]);
assert!(p.enforced());
assert!(p.accepts("alpha"));
assert!(
p.accepts("beta") && p.accepts(" beta"),
"keys are trimmed both ways"
);
assert!(!p.accepts("gamma"));
assert!(!p.accepts(""), "a blank key is never configured");
assert!(!p.accepts("alph"));
}
#[test]
fn an_open_policy_enforces_nothing() {
let p = Policy::default();
assert!(!p.enforced());
assert!(!Policy::with_keys(["", " "]).enforced());
let mut off = Policy::with_keys(["k"]);
off.required = false;
assert!(
!off.enforced() && off.accepts("k"),
"recognized, not required"
);
}
#[test]
fn the_debug_form_carries_no_key() {
let p = Policy::with_keys(["hunter2"]);
assert!(!format!("{p:?}").contains("hunter2"));
}
#[test]
fn the_bearer_scheme_is_case_insensitive() {
let mut h = HeaderMap::new();
h.insert(AUTHORIZATION, "bearer tok ".parse().unwrap());
assert_eq!(bearer(&h), Some("tok"));
h.insert(AUTHORIZATION, "Basic tok".parse().unwrap());
assert_eq!(bearer(&h), None);
h.insert(AUTHORIZATION, "Bearer".parse().unwrap());
assert_eq!(bearer(&h), None);
}
#[test]
fn the_offered_ws_protocol_is_found_among_others() {
let mut h = HeaderMap::new();
h.insert(
"sec-websocket-protocol",
format!("chat, {}", crate::auth::ws_protocol("k"))
.parse()
.unwrap(),
);
let offered = offered_ws_protocol(&h).unwrap();
assert_eq!(crate::auth::ws_protocol_key(&offered).as_deref(), Some("k"));
h.insert("sec-websocket-protocol", "chat".parse().unwrap());
assert_eq!(offered_ws_protocol(&h), None);
}
}