From 52851787329c48c1e70f98a3610ad52fe1fa4aa4 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 8 Apr 2024 15:14:21 +0200 Subject: Closes #25. Apikey not required anymore --- src/auth.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/auth.rs (limited to 'src/auth.rs') diff --git a/src/auth.rs b/src/auth.rs new file mode 100644 index 0000000..1f4518a --- /dev/null +++ b/src/auth.rs @@ -0,0 +1,35 @@ +use crate::AppState; +use axum::{ + extract::{Request, State}, + http::{HeaderMap, StatusCode}, + middleware::Next, + response::Response, +}; +use serde::Deserialize; + +#[derive(Debug, Clone, Deserialize)] +pub enum Methods { + Key, + None, +} + +pub async fn auth( + State(state): State, + headers: HeaderMap, + request: Request, + next: Next, +) -> Result { + let auth = state.config.auth; + match auth.method { + Methods::Key => { + if let Some(secret) = headers.get("authorization") { + if !(auth.secret.as_str() == secret) { return Err(StatusCode::UNAUTHORIZED); }; + let response = next.run(request).await; + Ok(response) + } else { + return Err(StatusCode::UNAUTHORIZED); + } + } + Methods::None => Ok(next.run(request).await), + } +} -- cgit v1.2.3