From 016fa3a31f8847d3f52800941b7f8fe5ef872240 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Thu, 15 Feb 2024 19:29:48 +0100 Subject: Closes #15. No usable Error message right now --- src/auth.rs | 49 +++++++++++++++---------------------------------- 1 file changed, 15 insertions(+), 34 deletions(-) (limited to 'src/auth.rs') diff --git a/src/auth.rs b/src/auth.rs index eb4d1bf..22f87e7 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,49 +1,30 @@ -use axum::http::{StatusCode, HeaderValue}; -use axum::http::header::ToStrError; -use tracing::{debug, error, trace}; -use crate::auth::Error::{MissingSecret, WrongSecret}; +use axum::http::HeaderValue; +use tracing::{debug, trace}; use crate::config::Config; +use crate::error::Error; -pub fn auth(config: &Config, secret: Option<&HeaderValue>) -> Result { +pub fn auth(config: &Config, secret: Option<&HeaderValue>) -> Result { debug!("auth request with secret {:?}", secret); - if let Some(value) = secret { - trace!("value exists"); + let res = if let Some(value) = secret { + trace!("auth value exists"); let key = &config.apikey; if value.to_str()? == key.as_str() { debug!("successful auth"); - Ok(true) + Response::Success } else { debug!("unsuccessful auth (wrong secret)"); - Err(WrongSecret) + Response::WrongSecret } } else { debug!("unsuccessful auth (no secret)"); - Err(MissingSecret) - } + Response::MissingSecret + }; + Ok(res) } -#[derive(Debug, thiserror::Error)] -pub enum Error { - #[error("wrong secret")] +#[derive(Debug)] +pub enum Response { + Success, WrongSecret, - #[error("missing secret")] - MissingSecret, - #[error("parse error: {source}")] - HeaderToStr { - #[from] - source: ToStrError - } -} - -impl Error { - pub fn get(self) -> (StatusCode, &'static str) { - match self { - Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"), - Self::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"), - Self::HeaderToStr { source } => { - error!("auth: {}", source); - (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") - }, - } - } + MissingSecret } -- cgit v1.2.3