diff options
Diffstat (limited to 'src/auth.rs')
-rw-r--r-- | src/auth.rs | 13 |
1 files changed, 3 insertions, 10 deletions
diff --git a/src/auth.rs b/src/auth.rs index 0321ade..feca652 100644 --- a/src/auth.rs +++ b/src/auth.rs | |||
@@ -2,15 +2,13 @@ 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::Error::{MissingSecret, WrongSecret}; | 4 | use crate::auth::Error::{MissingSecret, WrongSecret}; |
5 | use crate::config::SETTINGS; | 5 | use crate::config::Config; |
6 | 6 | ||
7 | pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, Error> { | 7 | pub fn auth(config: &Config, 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 = &config.apikey; |
12 | .get_string("apikey") | ||
13 | .map_err(Error::Config)?; | ||
14 | if value.to_str().map_err(Error::HeaderToStr)? == key.as_str() { | 12 | if value.to_str().map_err(Error::HeaderToStr)? == key.as_str() { |
15 | debug!("successful auth"); | 13 | debug!("successful auth"); |
16 | Ok(true) | 14 | Ok(true) |
@@ -28,7 +26,6 @@ pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, Error> { | |||
28 | pub enum Error { | 26 | pub enum Error { |
29 | WrongSecret, | 27 | WrongSecret, |
30 | MissingSecret, | 28 | MissingSecret, |
31 | Config(config::ConfigError), | ||
32 | HeaderToStr(ToStrError) | 29 | HeaderToStr(ToStrError) |
33 | } | 30 | } |
34 | 31 | ||
@@ -37,10 +34,6 @@ impl Error { | |||
37 | match self { | 34 | match self { |
38 | Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"), | 35 | Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"), |
39 | Self::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"), | 36 | Self::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"), |
40 | Self::Config(err) => { | ||
41 | error!("server error: {}", err.to_string()); | ||
42 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | ||
43 | }, | ||
44 | Self::HeaderToStr(err) => { | 37 | Self::HeaderToStr(err) => { |
45 | error!("server error: {}", err.to_string()); | 38 | error!("server error: {}", err.to_string()); |
46 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | 39 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") |