summaryrefslogtreecommitdiff
path: root/src/auth.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-02-12 15:05:03 +0100
committerGitHub <[email protected]>2024-02-12 15:05:03 +0100
commitca395b81bcadb9dde9214098f2cab0c7a3561574 (patch)
tree055bae9ef4e65f53b826b240f0b8e3b15b73693e /src/auth.rs
parente4832b4cf36ba0eaed298ee458498eddd7176590 (diff)
parente50a868f69b602cc0bc84d51e9940878924f49ef (diff)
downloadwebol-ca395b81bcadb9dde9214098f2cab0c7a3561574.tar
webol-ca395b81bcadb9dde9214098f2cab0c7a3561574.tar.gz
webol-ca395b81bcadb9dde9214098f2cab0c7a3561574.zip
Merge pull request #13 from FxQnLr/config
Config
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")