summaryrefslogtreecommitdiff
path: root/src/auth.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-02-12 14:58:08 +0100
committerFxQnLr <[email protected]>2024-02-12 14:58:08 +0100
commit8ed77d7ab484121e9d70158e14c9fd6c243f1c70 (patch)
treedabecfb3eaec1420782eb9d3987e54ba83612b18 /src/auth.rs
parente4832b4cf36ba0eaed298ee458498eddd7176590 (diff)
downloadwebol-8ed77d7ab484121e9d70158e14c9fd6c243f1c70.tar
webol-8ed77d7ab484121e9d70158e14c9fd6c243f1c70.tar.gz
webol-8ed77d7ab484121e9d70158e14c9fd6c243f1c70.zip
Close #9. Config impl with struct and files
Diffstat (limited to 'src/auth.rs')
-rw-r--r--src/auth.rs13
1 files changed, 3 insertions, 10 deletions
diff --git a/src/auth.rs b/src/auth.rs
index 0321ade..feca652 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -2,15 +2,13 @@ use axum::http::{StatusCode, HeaderValue};
2use axum::http::header::ToStrError; 2use axum::http::header::ToStrError;
3use tracing::{debug, error, trace}; 3use tracing::{debug, error, trace};
4use crate::auth::Error::{MissingSecret, WrongSecret}; 4use crate::auth::Error::{MissingSecret, WrongSecret};
5use crate::config::SETTINGS; 5use crate::config::Config;
6 6
7pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, Error> { 7pub fn auth(config: &Config, secret: Option<&HeaderValue>) -> Result<bool, Error> {
8 debug!("auth request with secret {:?}", secret); 8 debug!("auth request with secret {:?}", secret);
9 if let Some(value) = secret { 9 if let Some(value) = secret {
10 trace!("value exists"); 10 trace!("value exists");
11 let key = SETTINGS 11 let key = &config.apikey;
12 .get_string("apikey")
13 .map_err(Error::Config)?;
14 if value.to_str().map_err(Error::HeaderToStr)? == key.as_str() { 12 if value.to_str().map_err(Error::HeaderToStr)? == key.as_str() {
15 debug!("successful auth"); 13 debug!("successful auth");
16 Ok(true) 14 Ok(true)
@@ -28,7 +26,6 @@ pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, Error> {
28pub enum Error { 26pub enum Error {
29 WrongSecret, 27 WrongSecret,
30 MissingSecret, 28 MissingSecret,
31 Config(config::ConfigError),
32 HeaderToStr(ToStrError) 29 HeaderToStr(ToStrError)
33} 30}
34 31
@@ -37,10 +34,6 @@ impl Error {
37 match self { 34 match self {
38 Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"), 35 Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"),
39 Self::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"), 36 Self::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"),
40 Self::Config(err) => {
41 error!("server error: {}", err.to_string());
42 (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
43 },
44 Self::HeaderToStr(err) => { 37 Self::HeaderToStr(err) => {
45 error!("server error: {}", err.to_string()); 38 error!("server error: {}", err.to_string());
46 (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") 39 (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")