summaryrefslogtreecommitdiff
path: root/src/auth.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/auth.rs')
-rw-r--r--src/auth.rs21
1 files changed, 13 insertions, 8 deletions
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 @@
1use std::error::Error;
2use axum::headers::HeaderValue; 1use axum::headers::HeaderValue;
3use axum::http::StatusCode; 2use axum::http::StatusCode;
3use axum::http::header::ToStrError;
4use tracing::{debug, error, trace}; 4use tracing::{debug, error, trace};
5use crate::auth::AuthError::{MissingSecret, ServerError, WrongSecret}; 5use crate::auth::AuthError::{MissingSecret, WrongSecret};
6use crate::config::SETTINGS; 6use crate::config::SETTINGS;
7 7
8pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, AuthError> { 8pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, AuthError> {
@@ -11,8 +11,8 @@ pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, AuthError> {
11 trace!("value exists"); 11 trace!("value exists");
12 let key = SETTINGS 12 let key = SETTINGS
13 .get_string("apikey") 13 .get_string("apikey")
14 .map_err(|err| ServerError(Box::new(err)))?; 14 .map_err(AuthError::Config)?;
15 if value.to_str().map_err(|err| ServerError(Box::new(err)))? == key.as_str() { 15 if value.to_str().map_err(AuthError::HeaderToStr)? == key.as_str() {
16 debug!("successful auth"); 16 debug!("successful auth");
17 Ok(true) 17 Ok(true)
18 } else { 18 } else {
@@ -29,15 +29,20 @@ pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, AuthError> {
29pub enum AuthError { 29pub enum AuthError {
30 WrongSecret, 30 WrongSecret,
31 MissingSecret, 31 MissingSecret,
32 ServerError(Box<dyn Error>), 32 Config(config::ConfigError),
33 HeaderToStr(ToStrError)
33} 34}
34 35
35impl AuthError { 36impl AuthError {
36 pub fn get(self) -> (StatusCode, &'static str) { 37 pub fn get(self) -> (StatusCode, &'static str) {
37 match self { 38 match self {
38 AuthError::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"), 39 Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"),
39 AuthError::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"), 40 Self::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"),
40 AuthError::ServerError(err) => { 41 Self::Config(err) => {
42 error!("server error: {}", err.to_string());
43 (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
44 },
45 Self::HeaderToStr(err) => {
41 error!("server error: {}", err.to_string()); 46 error!("server error: {}", err.to_string());
42 (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") 47 (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
43 }, 48 },