From dcfb83fb2069bfcf4642b03453253e35479bf3da Mon Sep 17 00:00:00 2001 From: fx Date: Tue, 24 Oct 2023 01:15:22 +0200 Subject: first ping impl baseline, doesnt work --- src/auth.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src/auth.rs') diff --git a/src/auth.rs b/src/auth.rs index 0fffa60..e4b1c2f 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,8 +1,8 @@ -use std::error::Error; use axum::headers::HeaderValue; use axum::http::StatusCode; +use axum::http::header::ToStrError; use tracing::{debug, error, trace}; -use crate::auth::AuthError::{MissingSecret, ServerError, WrongSecret}; +use crate::auth::AuthError::{MissingSecret, WrongSecret}; use crate::config::SETTINGS; pub fn auth(secret: Option<&HeaderValue>) -> Result { @@ -11,8 +11,8 @@ pub fn auth(secret: Option<&HeaderValue>) -> Result { trace!("value exists"); 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() { + .map_err(AuthError::Config)?; + if value.to_str().map_err(AuthError::HeaderToStr)? == key.as_str() { debug!("successful auth"); Ok(true) } else { @@ -29,15 +29,20 @@ pub fn auth(secret: Option<&HeaderValue>) -> Result { pub enum AuthError { WrongSecret, MissingSecret, - ServerError(Box), + Config(config::ConfigError), + HeaderToStr(ToStrError) } 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) => { + Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"), + Self::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"), + Self::Config(err) => { + error!("server error: {}", err.to_string()); + (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") + }, + Self::HeaderToStr(err) => { error!("server error: {}", err.to_string()); (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") }, -- cgit v1.2.3