AI-Studio/runtime/src/runtime_api_token.rs

36 lines
1004 B
Rust
Raw Normal View History

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;
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-05-12 18:31:08 +00:00
impl<S> FromRequestParts<S> for APIToken
where
S: Send + Sync,
{
type Rejection = StatusCode;
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()) {
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)
} else {
2026-05-12 18:31:08 +00:00
Err(StatusCode::UNAUTHORIZED)
}
}
2026-05-12 18:31:08 +00:00
None => Err(StatusCode::UNAUTHORIZED),
}
}
}
/// The API token error types.
#[derive(Debug)]
pub enum APITokenError {
Missing,
Invalid,
}