diff options
author | FxQnLr <[email protected]> | 2024-02-25 15:53:04 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2024-02-25 15:53:04 +0100 |
commit | f0dc13f907a72ffef44f89b5e197567db129b020 (patch) | |
tree | d560273df2eece276cbda021cb4e95c044bb19df /src/auth.rs | |
parent | c663810817183c8f92a4279236ca84d271365088 (diff) | |
parent | 91cd665671d564620bce13e693cd7ecaad697db9 (diff) | |
download | webol-f0dc13f907a72ffef44f89b5e197567db129b020.tar webol-f0dc13f907a72ffef44f89b5e197567db129b020.tar.gz webol-f0dc13f907a72ffef44f89b5e197567db129b020.zip |
Merge pull request #17 from FxQnLr/0.3.2
0.3.2
Diffstat (limited to 'src/auth.rs')
-rw-r--r-- | src/auth.rs | 43 |
1 files changed, 0 insertions, 43 deletions
diff --git a/src/auth.rs b/src/auth.rs deleted file mode 100644 index feca652..0000000 --- a/src/auth.rs +++ /dev/null | |||
@@ -1,43 +0,0 @@ | |||
1 | use axum::http::{StatusCode, HeaderValue}; | ||
2 | use axum::http::header::ToStrError; | ||
3 | use tracing::{debug, error, trace}; | ||
4 | use crate::auth::Error::{MissingSecret, WrongSecret}; | ||
5 | use crate::config::Config; | ||
6 | |||
7 | pub fn auth(config: &Config, secret: Option<&HeaderValue>) -> Result<bool, Error> { | ||
8 | debug!("auth request with secret {:?}", secret); | ||
9 | if let Some(value) = secret { | ||
10 | trace!("value exists"); | ||
11 | let key = &config.apikey; | ||
12 | if value.to_str().map_err(Error::HeaderToStr)? == key.as_str() { | ||
13 | debug!("successful auth"); | ||
14 | Ok(true) | ||
15 | } else { | ||
16 | debug!("unsuccessful auth (wrong secret)"); | ||
17 | Err(WrongSecret) | ||
18 | } | ||
19 | } else { | ||
20 | debug!("unsuccessful auth (no secret)"); | ||
21 | Err(MissingSecret) | ||
22 | } | ||
23 | } | ||
24 | |||
25 | #[derive(Debug)] | ||
26 | pub enum Error { | ||
27 | WrongSecret, | ||
28 | MissingSecret, | ||
29 | HeaderToStr(ToStrError) | ||
30 | } | ||
31 | |||
32 | impl Error { | ||
33 | pub fn get(self) -> (StatusCode, &'static str) { | ||
34 | match self { | ||
35 | Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"), | ||
36 | Self::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"), | ||
37 | Self::HeaderToStr(err) => { | ||
38 | error!("server error: {}", err.to_string()); | ||
39 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | ||
40 | }, | ||
41 | } | ||
42 | } | ||
43 | } | ||