2026-02-03 13:32:17 +00:00
|
|
|
use once_cell::sync::Lazy;
|
2026-05-12 18:31:08 +00:00
|
|
|
use axum::extract::FromRequestParts;
|
|
|
|
|
use axum::http::request::Parts;
|
|
|
|
|
use axum::http::StatusCode;
|
2026-02-03 13:32:17 +00:00
|
|
|
use crate::api_token::{generate_api_token, APIToken};
|
|
|
|
|
|
2026-05-12 18:31:08 +00:00
|
|
|
pub static API_TOKEN: Lazy<APIToken> = Lazy::new(generate_api_token);
|
2026-02-03 13:32:17 +00:00
|
|
|
|
2026-05-12 18:31:08 +00:00
|
|
|
impl<S> FromRequestParts<S> for APIToken
|
|
|
|
|
where
|
|
|
|
|
S: Send + Sync,
|
|
|
|
|
{
|
|
|
|
|
type Rejection = StatusCode;
|
2026-02-03 13:32:17 +00:00
|
|
|
|
2026-05-12 18:31:08 +00:00
|
|
|
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
|
|
|
|
match parts.headers.get("token").and_then(|value| value.to_str().ok()) {
|
2026-02-03 13:32:17 +00:00
|
|
|
Some(token) => {
|
|
|
|
|
let received_token = APIToken::from_hex_text(token);
|
|
|
|
|
if API_TOKEN.validate(&received_token) {
|
2026-05-12 18:31:08 +00:00
|
|
|
Ok(received_token)
|
2026-02-03 13:32:17 +00:00
|
|
|
} else {
|
2026-05-12 18:31:08 +00:00
|
|
|
Err(StatusCode::UNAUTHORIZED)
|
2026-02-03 13:32:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-12 18:31:08 +00:00
|
|
|
None => Err(StatusCode::UNAUTHORIZED),
|
2026-02-03 13:32:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The API token error types.
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum APITokenError {
|
|
|
|
|
Missing,
|
|
|
|
|
Invalid,
|
|
|
|
|
}
|