aboutsummaryrefslogtreecommitdiff
path: root/src/auth.rs
diff options
context:
space:
mode:
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 }