use axum::extract::{Request, State};
use axum::http::header;
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use crate::error::ApiError;
use crate::state::AppState;
pub async fn require_bearer(
State(state): State<AppState>,
request: Request,
next: Next,
) -> Response {
let Some(expected) = state.auth_token() else {
return next.run(request).await;
};
let presented = request
.headers()
.get(header::AUTHORIZATION)
.and_then(|value| value.to_str().ok());
if presented == Some(&format!("Bearer {expected}")) {
next.run(request).await
} else {
ApiError::Unauthorized.into_response()
}
}