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