diff options
author | FxQnLr <[email protected]> | 2024-02-15 19:29:48 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-02-15 19:29:48 +0100 |
commit | 016fa3a31f8847d3f52800941b7f8fe5ef872240 (patch) | |
tree | 59bca278b082b0801c1a4174aac856be9ccd298f /src/auth.rs | |
parent | 3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61 (diff) | |
download | webol-016fa3a31f8847d3f52800941b7f8fe5ef872240.tar webol-016fa3a31f8847d3f52800941b7f8fe5ef872240.tar.gz webol-016fa3a31f8847d3f52800941b7f8fe5ef872240.zip |
Closes #15. No usable Error message right now
Diffstat (limited to 'src/auth.rs')
-rw-r--r-- | src/auth.rs | 49 |
1 files changed, 15 insertions, 34 deletions
diff --git a/src/auth.rs b/src/auth.rs index eb4d1bf..22f87e7 100644 --- a/src/auth.rs +++ b/src/auth.rs | |||
@@ -1,49 +1,30 @@ | |||
1 | use axum::http::{StatusCode, HeaderValue}; | 1 | use axum::http::HeaderValue; |
2 | use axum::http::header::ToStrError; | 2 | use tracing::{debug, trace}; |
3 | use tracing::{debug, error, trace}; | ||
4 | use crate::auth::Error::{MissingSecret, WrongSecret}; | ||
5 | use crate::config::Config; | 3 | use crate::config::Config; |
4 | use crate::error::Error; | ||
6 | 5 | ||
7 | pub fn auth(config: &Config, secret: Option<&HeaderValue>) -> Result<bool, Error> { | 6 | pub fn auth(config: &Config, secret: Option<&HeaderValue>) -> Result<Response, Error> { |
8 | debug!("auth request with secret {:?}", secret); | 7 | debug!("auth request with secret {:?}", secret); |
9 | if let Some(value) = secret { | 8 | let res = if let Some(value) = secret { |
10 | trace!("value exists"); | 9 | trace!("auth value exists"); |
11 | let key = &config.apikey; | 10 | let key = &config.apikey; |
12 | if value.to_str()? == key.as_str() { | 11 | if value.to_str()? == key.as_str() { |
13 | debug!("successful auth"); | 12 | debug!("successful auth"); |
14 | Ok(true) | 13 | Response::Success |
15 | } else { | 14 | } else { |
16 | debug!("unsuccessful auth (wrong secret)"); | 15 | debug!("unsuccessful auth (wrong secret)"); |
17 | Err(WrongSecret) | 16 | Response::WrongSecret |
18 | } | 17 | } |
19 | } else { | 18 | } else { |
20 | debug!("unsuccessful auth (no secret)"); | 19 | debug!("unsuccessful auth (no secret)"); |
21 | Err(MissingSecret) | 20 | Response::MissingSecret |
22 | } | 21 | }; |
22 | Ok(res) | ||
23 | } | 23 | } |
24 | 24 | ||
25 | #[derive(Debug, thiserror::Error)] | 25 | #[derive(Debug)] |
26 | pub enum Error { | 26 | pub enum Response { |
27 | #[error("wrong secret")] | 27 | Success, |
28 | WrongSecret, | 28 | WrongSecret, |
29 | #[error("missing secret")] | 29 | MissingSecret |
30 | MissingSecret, | ||
31 | #[error("parse error: {source}")] | ||
32 | HeaderToStr { | ||
33 | #[from] | ||
34 | source: ToStrError | ||
35 | } | ||
36 | } | ||
37 | |||
38 | impl Error { | ||
39 | pub fn get(self) -> (StatusCode, &'static str) { | ||
40 | match self { | ||
41 | Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"), | ||
42 | Self::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"), | ||
43 | Self::HeaderToStr { source } => { | ||
44 | error!("auth: {}", source); | ||
45 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | ||
46 | }, | ||
47 | } | ||
48 | } | ||
49 | } | 30 | } |