aboutsummaryrefslogtreecommitdiff
path: root/src/auth.rs
blob: 22f87e74dcb192cf4aa41e105d958b728cea2ef3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use axum::http::HeaderValue;
use tracing::{debug, trace};
use crate::config::Config;
use crate::error::Error;

pub fn auth(config: &Config, secret: Option<&HeaderValue>) -> Result<Response, Error> {
    debug!("auth request with secret {:?}", secret);
    let res = if let Some(value) = secret {
        trace!("auth value exists");
        let key = &config.apikey;
        if value.to_str()? == key.as_str() {
            debug!("successful auth");
            Response::Success
        } else {
            debug!("unsuccessful auth (wrong secret)");
            Response::WrongSecret
        }
    } else {
        debug!("unsuccessful auth (no secret)");
        Response::MissingSecret
    };
    Ok(res)
}

#[derive(Debug)]
pub enum Response {
    Success,
    WrongSecret,
    MissingSecret
}