diff options
author | FxQnLr <[email protected]> | 2023-11-06 11:09:34 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2023-11-06 11:09:34 +0100 |
commit | 1cd2a8e4aecfaad2a8385a6bea61580209b86398 (patch) | |
tree | c357bcaca0681caf9a6742c857bb494dc4315900 /src/auth.rs | |
parent | d9d7b125e4fcaa3aedd7b57a69e6880e012ccf33 (diff) | |
parent | 32561060a8dc6fc6118498da06bdd8f5b4c3f0fd (diff) | |
download | webol-1cd2a8e4aecfaad2a8385a6bea61580209b86398.tar webol-1cd2a8e4aecfaad2a8385a6bea61580209b86398.tar.gz webol-1cd2a8e4aecfaad2a8385a6bea61580209b86398.zip |
Merge pull request #6 from FxQnLr/ping
Ping
Diffstat (limited to 'src/auth.rs')
-rw-r--r-- | src/auth.rs | 21 |
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 @@ | |||
1 | use std::error::Error; | ||
2 | use axum::headers::HeaderValue; | 1 | use axum::headers::HeaderValue; |
3 | use axum::http::StatusCode; | 2 | use axum::http::StatusCode; |
3 | use axum::http::header::ToStrError; | ||
4 | use tracing::{debug, error, trace}; | 4 | use tracing::{debug, error, trace}; |
5 | use crate::auth::AuthError::{MissingSecret, ServerError, WrongSecret}; | 5 | use crate::auth::AuthError::{MissingSecret, WrongSecret}; |
6 | use crate::config::SETTINGS; | 6 | use crate::config::SETTINGS; |
7 | 7 | ||
8 | pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, AuthError> { | 8 | pub 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> { | |||
29 | pub enum AuthError { | 29 | pub 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 | ||
35 | impl AuthError { | 36 | impl 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 | }, |