summaryrefslogtreecommitdiff
path: root/src/auth.rs
diff options
context:
space:
mode:
authorfx <[email protected]>2023-10-09 13:07:54 +0200
committerfx <[email protected]>2023-10-09 13:07:54 +0200
commitbc5f721de8996b48550b5069f5592caf2968e822 (patch)
treefe7bf485cda800d6fff595c0573b9bca4a4c1dd0 /src/auth.rs
parent159cb1b3c940440ebe03e5042c361be563324978 (diff)
downloadwebol-bc5f721de8996b48550b5069f5592caf2968e822.tar
webol-bc5f721de8996b48550b5069f5592caf2968e822.tar.gz
webol-bc5f721de8996b48550b5069f5592caf2968e822.zip
added wol func and bad auth
Diffstat (limited to 'src/auth.rs')
-rw-r--r--src/auth.rs41
1 files changed, 39 insertions, 2 deletions
diff --git a/src/auth.rs b/src/auth.rs
index b1ad76d..b7693a0 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -1,3 +1,40 @@
1pub fn auth(secret: &str) -> bool { 1use std::error::Error;
2 secret == "aaa" 2use axum::headers::HeaderValue;
3use axum::http::StatusCode;
4use tracing::error;
5use crate::auth::AuthError::{MissingSecret, ServerError, WrongSecret};
6use crate::config::SETTINGS;
7
8pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, AuthError> {
9 if let Some(value) = secret {
10 let key = SETTINGS
11 .get_string("apikey")
12 .map_err(|err| ServerError(Box::new(err)))?;
13 if value.to_str().map_err(|err| ServerError(Box::new(err)))? == key.as_str() {
14 Ok(true)
15 } else {
16 Err(WrongSecret)
17 }
18 } else {
19 Err(MissingSecret)
20 }
3} 21}
22
23pub enum AuthError {
24 WrongSecret,
25 MissingSecret,
26 ServerError(Box<dyn Error>),
27}
28
29impl AuthError {
30 pub fn get(self) -> (StatusCode, &'static str) {
31 match self {
32 AuthError::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"),
33 AuthError::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"),
34 AuthError::ServerError(err) => {
35 error!("server error: {}", err.to_string());
36 (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
37 },
38 }
39 }
40} \ No newline at end of file