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