From bc5f721de8996b48550b5069f5592caf2968e822 Mon Sep 17 00:00:00 2001 From: fx Date: Mon, 9 Oct 2023 13:07:54 +0200 Subject: added wol func and bad auth --- src/auth.rs | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'src/auth.rs') diff --git a/src/auth.rs b/src/auth.rs index b1ad76d..b7693a0 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,3 +1,40 @@ -pub fn auth(secret: &str) -> bool { - secret == "aaa" +use std::error::Error; +use axum::headers::HeaderValue; +use axum::http::StatusCode; +use tracing::error; +use crate::auth::AuthError::{MissingSecret, ServerError, WrongSecret}; +use crate::config::SETTINGS; + +pub fn auth(secret: Option<&HeaderValue>) -> Result { + if let Some(value) = secret { + let key = SETTINGS + .get_string("apikey") + .map_err(|err| ServerError(Box::new(err)))?; + if value.to_str().map_err(|err| ServerError(Box::new(err)))? == key.as_str() { + Ok(true) + } else { + Err(WrongSecret) + } + } else { + Err(MissingSecret) + } } + +pub enum AuthError { + WrongSecret, + MissingSecret, + ServerError(Box), +} + +impl AuthError { + pub fn get(self) -> (StatusCode, &'static str) { + match self { + AuthError::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"), + AuthError::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"), + AuthError::ServerError(err) => { + error!("server error: {}", err.to_string()); + (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") + }, + } + } +} \ No newline at end of file -- cgit v1.2.3