diff options
Diffstat (limited to 'src/auth.rs')
-rw-r--r-- | src/auth.rs | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/src/auth.rs b/src/auth.rs index 90d920f..0321ade 100644 --- a/src/auth.rs +++ b/src/auth.rs | |||
@@ -1,17 +1,17 @@ | |||
1 | use axum::http::{StatusCode, HeaderValue}; | 1 | use axum::http::{StatusCode, HeaderValue}; |
2 | use axum::http::header::ToStrError; | 2 | use axum::http::header::ToStrError; |
3 | use tracing::{debug, error, trace}; | 3 | use tracing::{debug, error, trace}; |
4 | use crate::auth::AuthError::{MissingSecret, WrongSecret}; | 4 | use crate::auth::Error::{MissingSecret, WrongSecret}; |
5 | use crate::config::SETTINGS; | 5 | use crate::config::SETTINGS; |
6 | 6 | ||
7 | pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, AuthError> { | 7 | pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, Error> { |
8 | debug!("auth request with secret {:?}", secret); | 8 | debug!("auth request with secret {:?}", secret); |
9 | if let Some(value) = secret { | 9 | if let Some(value) = secret { |
10 | trace!("value exists"); | 10 | trace!("value exists"); |
11 | let key = SETTINGS | 11 | let key = SETTINGS |
12 | .get_string("apikey") | 12 | .get_string("apikey") |
13 | .map_err(AuthError::Config)?; | 13 | .map_err(Error::Config)?; |
14 | if value.to_str().map_err(AuthError::HeaderToStr)? == key.as_str() { | 14 | if value.to_str().map_err(Error::HeaderToStr)? == key.as_str() { |
15 | debug!("successful auth"); | 15 | debug!("successful auth"); |
16 | Ok(true) | 16 | Ok(true) |
17 | } else { | 17 | } else { |
@@ -25,14 +25,14 @@ pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, AuthError> { | |||
25 | } | 25 | } |
26 | 26 | ||
27 | #[derive(Debug)] | 27 | #[derive(Debug)] |
28 | pub enum AuthError { | 28 | pub enum Error { |
29 | WrongSecret, | 29 | WrongSecret, |
30 | MissingSecret, | 30 | MissingSecret, |
31 | Config(config::ConfigError), | 31 | Config(config::ConfigError), |
32 | HeaderToStr(ToStrError) | 32 | HeaderToStr(ToStrError) |
33 | } | 33 | } |
34 | 34 | ||
35 | impl AuthError { | 35 | impl Error { |
36 | pub fn get(self) -> (StatusCode, &'static str) { | 36 | pub fn get(self) -> (StatusCode, &'static str) { |
37 | match self { | 37 | match self { |
38 | Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"), | 38 | Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"), |