aboutsummaryrefslogtreecommitdiff
path: root/src/auth.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-02-25 15:53:04 +0100
committerGitHub <[email protected]>2024-02-25 15:53:04 +0100
commitf0dc13f907a72ffef44f89b5e197567db129b020 (patch)
treed560273df2eece276cbda021cb4e95c044bb19df /src/auth.rs
parentc663810817183c8f92a4279236ca84d271365088 (diff)
parent91cd665671d564620bce13e693cd7ecaad697db9 (diff)
downloadwebol-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.rs43
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 @@
1use axum::http::{StatusCode, HeaderValue};
2use axum::http::header::ToStrError;
3use tracing::{debug, error, trace};
4use crate::auth::Error::{MissingSecret, WrongSecret};
5use crate::config::Config;
6
7pub 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)]
26pub enum Error {
27 WrongSecret,
28 MissingSecret,
29 HeaderToStr(ToStrError)
30}
31
32impl 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}