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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use base64::prelude::*;
use serde::Deserialize;

#[doc(hidden)]
fn default_username() -> String {
    "admin".to_string()
}

#[doc(hidden)]
fn default_password() -> String {
    "password".to_string()
}

#[doc(hidden)]
const fn default_enabled() -> bool {
    true
}

#[derive(Deserialize, Clone, Debug)]
pub struct Auth {
    /// Whether authentication is enabled (default: true)
    #[serde(default = "default_enabled")]
    pub enabled: bool,
    /// Username for basic auth (default: admin)
    #[serde(default = "default_username")]
    pub username: String,
    /// Password for basic auth (default: password)
    #[serde(default = "default_password")]
    pub password: String,
}

impl Default for Auth {
    fn default() -> Self {
        Self {
            enabled: default_enabled(),
            username: default_username(),
            password: default_password(),
        }
    }
}

impl Auth {
    pub fn to_auth_encoded(&self) -> String {
        format!(
            "Basic {}",
            BASE64_STANDARD.encode(format!("{}:{}", self.username, self.password))
        )
    }
}