summaryrefslogtreecommitdiff
path: root/src/auth.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-02-12 16:00:45 +0100
committerGitHub <[email protected]>2024-02-12 16:00:45 +0100
commitc663810817183c8f92a4279236ca84d271365088 (patch)
tree0c844cc883e5e474a9cdad30004108852f13f903 /src/auth.rs
parentda6367885d31698464e1bec122e3e673974427c6 (diff)
parent9139d76cb1cf462820b2ddfa80d9a8d55bb30996 (diff)
downloadwebol-c663810817183c8f92a4279236ca84d271365088.tar
webol-c663810817183c8f92a4279236ca84d271365088.tar.gz
webol-c663810817183c8f92a4279236ca84d271365088.zip
Merge pull request #14 from FxQnLr/axum7
Axum7 & config changes
Diffstat (limited to 'src/auth.rs')
-rw-r--r--src/auth.rs24
1 files changed, 8 insertions, 16 deletions
diff --git a/src/auth.rs b/src/auth.rs
index e4b1c2f..feca652 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -1,18 +1,15 @@
1use axum::headers::HeaderValue; 1use axum::http::{StatusCode, HeaderValue};
2use axum::http::StatusCode;
3use axum::http::header::ToStrError; 2use axum::http::header::ToStrError;
4use tracing::{debug, error, trace}; 3use tracing::{debug, error, trace};
5use crate::auth::AuthError::{MissingSecret, WrongSecret}; 4use crate::auth::Error::{MissingSecret, WrongSecret};
6use crate::config::SETTINGS; 5use crate::config::Config;
7 6
8pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, AuthError> { 7pub fn auth(config: &Config, secret: Option<&HeaderValue>) -> Result<bool, Error> {
9 debug!("auth request with secret {:?}", secret); 8 debug!("auth request with secret {:?}", secret);
10 if let Some(value) = secret { 9 if let Some(value) = secret {
11 trace!("value exists"); 10 trace!("value exists");
12 let key = SETTINGS 11 let key = &config.apikey;
13 .get_string("apikey") 12 if value.to_str().map_err(Error::HeaderToStr)? == key.as_str() {
14 .map_err(AuthError::Config)?;
15 if value.to_str().map_err(AuthError::HeaderToStr)? == key.as_str() {
16 debug!("successful auth"); 13 debug!("successful auth");
17 Ok(true) 14 Ok(true)
18 } else { 15 } else {
@@ -26,22 +23,17 @@ pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, AuthError> {
26} 23}
27 24
28#[derive(Debug)] 25#[derive(Debug)]
29pub enum AuthError { 26pub enum Error {
30 WrongSecret, 27 WrongSecret,
31 MissingSecret, 28 MissingSecret,
32 Config(config::ConfigError),
33 HeaderToStr(ToStrError) 29 HeaderToStr(ToStrError)
34} 30}
35 31
36impl AuthError { 32impl Error {
37 pub fn get(self) -> (StatusCode, &'static str) { 33 pub fn get(self) -> (StatusCode, &'static str) {
38 match self { 34 match self {
39 Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"), 35 Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"),
40 Self::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"), 36 Self::MissingSecret => (StatusCode::BAD_REQUEST, "Missing credentials"),
41 Self::Config(err) => {
42 error!("server error: {}", err.to_string());
43 (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
44 },
45 Self::HeaderToStr(err) => { 37 Self::HeaderToStr(err) => {
46 error!("server error: {}", err.to_string()); 38 error!("server error: {}", err.to_string());
47 (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") 39 (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")