orion_accessor/addr/access_ctrl/
auth.rs1use crate::prelude::*;
2use serde_derive::{Deserialize, Serialize};
3#[derive(Debug, Clone, Serialize, Deserialize, Getters, PartialEq)]
4#[getset(get = "pub")]
5pub struct AuthConfig {
6 username: String,
7 password: String,
8}
9
10impl AuthConfig {
11 pub fn new<S: Into<String>>(username: S, password: S) -> Self {
12 Self {
13 username: username.into(),
14 password: password.into(),
15 }
16 }
17 pub fn make_example() -> Self {
18 Self {
19 username: "galaxy".into(),
20 password: "this-is-password".into(),
21 }
22 }
23}
24impl EnvEvalable<AuthConfig> for AuthConfig {
25 fn env_eval(self, dict: &EnvDict) -> AuthConfig {
26 AuthConfig {
27 username: self.username.env_eval(dict),
28 password: self.password.env_eval(dict),
29 }
30 }
31}
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36
37 #[test]
38 fn test_auth_config_env_eval() {
39 let mut env_dict = EnvDict::new();
40 env_dict.insert(
41 "USERNAME".to_string(),
42 ValueType::String("test_user".to_string()),
43 );
44 env_dict.insert(
45 "PASSWORD".to_string(),
46 ValueType::String("test_pass".to_string()),
47 );
48
49 let auth = AuthConfig::new("${USERNAME}", "${PASSWORD}");
50 let evaluated = auth.env_eval(&env_dict);
51
52 assert_eq!(evaluated.username(), "test_user");
53 assert_eq!(evaluated.password(), "test_pass");
54 }
55
56 #[test]
57 fn test_auth_config_env_eval_with_defaults() {
58 let env_dict = EnvDict::new();
59
60 let auth = AuthConfig::new(
61 "${MISSING_USER:default_user}",
62 "${MISSING_PASS:default_pass}",
63 );
64 let evaluated = auth.env_eval(&env_dict);
65
66 assert_eq!(evaluated.username(), "default_user");
67 assert_eq!(evaluated.password(), "default_pass");
68 }
69
70 #[test]
71 fn test_auth_config_make_example() {
72 let example = AuthConfig::make_example();
73
74 assert_eq!(example.username(), "galaxy");
75 assert_eq!(example.password(), "this-is-password");
76 }
77
78 #[test]
79 fn test_auth_config_mixed_env_eval() {
80 let mut env_dict = EnvDict::new();
81 env_dict.insert(
82 "USERNAME".to_string(),
83 ValueType::String("found_user".to_string()),
84 );
85
86 let auth = AuthConfig::new("${USERNAME}", "${MISSING_PASS:default_pass}");
87 let evaluated = auth.env_eval(&env_dict);
88
89 assert_eq!(evaluated.username(), "found_user");
90 assert_eq!(evaluated.password(), "default_pass");
91 }
92}