cat_gateway/service/common/auth/
api_key.rsuse anyhow::{bail, Result};
use poem::{http::HeaderMap, Request};
use poem_openapi::{auth::ApiKey, SecurityScheme};
use crate::settings::Settings;
const API_KEY_HEADER: &str = "X-API-Key";
#[derive(SecurityScheme)]
#[oai(
ty = "api_key",
key_name = "X-API-Key", key_in = "header",
checker = "api_checker"
)]
#[allow(dead_code)]
pub(crate) struct InternalApiKeyAuthorization(String);
#[allow(clippy::unused_async)]
async fn api_checker(_req: &Request, api_key: ApiKey) -> Option<String> {
if Settings::check_internal_api_key(&api_key.key) {
Some(api_key.key)
} else {
None
}
}
pub(crate) fn check_api_key(headers: &HeaderMap) -> Result<()> {
if let Some(key) = headers.get(API_KEY_HEADER) {
if Settings::check_internal_api_key(key.to_str()?) {
return Ok(());
}
}
bail!("Invalid API Key");
}