summaryrefslogtreecommitdiff
path: root/src/auth.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/auth.rs')
-rw-r--r--src/auth.rs12
1 files changed, 6 insertions, 6 deletions
diff --git a/src/auth.rs b/src/auth.rs
index 90d920f..0321ade 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -1,17 +1,17 @@
1use axum::http::{StatusCode, HeaderValue}; 1use 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::AuthError::{MissingSecret, WrongSecret}; 4use crate::auth::Error::{MissingSecret, WrongSecret};
5use crate::config::SETTINGS; 5use crate::config::SETTINGS;
6 6
7pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, AuthError> { 7pub fn auth(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 = SETTINGS
12 .get_string("apikey") 12 .get_string("apikey")
13 .map_err(AuthError::Config)?; 13 .map_err(Error::Config)?;
14 if value.to_str().map_err(AuthError::HeaderToStr)? == key.as_str() { 14 if value.to_str().map_err(Error::HeaderToStr)? == key.as_str() {
15 debug!("successful auth"); 15 debug!("successful auth");
16 Ok(true) 16 Ok(true)
17 } else { 17 } else {
@@ -25,14 +25,14 @@ pub fn auth(secret: Option<&HeaderValue>) -> Result<bool, AuthError> {
25} 25}
26 26
27#[derive(Debug)] 27#[derive(Debug)]
28pub enum AuthError { 28pub enum Error {
29 WrongSecret, 29 WrongSecret,
30 MissingSecret, 30 MissingSecret,
31 Config(config::ConfigError), 31 Config(config::ConfigError),
32 HeaderToStr(ToStrError) 32 HeaderToStr(ToStrError)
33} 33}
34 34
35impl AuthError { 35impl Error {
36 pub fn get(self) -> (StatusCode, &'static str) { 36 pub fn get(self) -> (StatusCode, &'static str) {
37 match self { 37 match self {
38 Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"), 38 Self::WrongSecret => (StatusCode::UNAUTHORIZED, "Wrong credentials"),