aboutsummaryrefslogtreecommitdiff
path: root/src/auth.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-02-15 17:17:30 +0100
committerFxQnLr <[email protected]>2024-02-15 17:17:30 +0100
commit3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61 (patch)
tree48a38a52ebfc41fadab439b9a87fae3d43f78790 /src/auth.rs
parentc663810817183c8f92a4279236ca84d271365088 (diff)
downloadwebol-3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61.tar
webol-3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61.tar.gz
webol-3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61.zip
Closes #10 & #12. Added `thiserror` crate and changed to `IntoSocketAddr` for easier usage and error handling
Diffstat (limited to 'src/auth.rs')
-rw-r--r--src/auth.rs16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/auth.rs b/src/auth.rs
index feca652..eb4d1bf 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -9,7 +9,7 @@ pub fn auth(config: &Config, secret: Option<&HeaderValue>) -> Result<bool, Error
9 if let Some(value) = secret { 9 if let Some(value) = secret {
10 trace!("value exists"); 10 trace!("value exists");
11 let key = &config.apikey; 11 let key = &config.apikey;
12 if value.to_str().map_err(Error::HeaderToStr)? == key.as_str() { 12 if value.to_str()? == key.as_str() {
13 debug!("successful auth"); 13 debug!("successful auth");
14 Ok(true) 14 Ok(true)
15 } else { 15 } else {
@@ -22,11 +22,17 @@ pub fn auth(config: &Config, secret: Option<&HeaderValue>) -> Result<bool, Error
22 } 22 }
23} 23}
24 24
25#[derive(Debug)] 25#[derive(Debug, thiserror::Error)]
26pub enum Error { 26pub enum Error {
27 #[error("wrong secret")]
27 WrongSecret, 28 WrongSecret,
29 #[error("missing secret")]
28 MissingSecret, 30 MissingSecret,
29 HeaderToStr(ToStrError) 31 #[error("parse error: {source}")]
32 HeaderToStr {
33 #[from]
34 source: ToStrError
35 }
30} 36}
31 37
32impl Error { 38impl Error {
@@ -34,8 +40,8 @@ impl Error {
34 match self { 40 match self {
35 Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"), 41 Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"),
36 Self::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"), 42 Self::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"),
37 Self::HeaderToStr(err) => { 43 Self::HeaderToStr { source } => {
38 error!("server error: {}", err.to_string()); 44 error!("auth: {}", source);
39 (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") 45 (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
40 }, 46 },
41 } 47 }