hb_auth/
config.rs

1use std::sync::Arc;
2
3#[derive(Clone, Debug)]
4pub struct AuthConfig {
5    pub team_domain: Arc<String>,
6    pub audience: Arc<String>,
7}
8
9impl AuthConfig {
10    pub fn new<S: Into<String>>(team_domain: S, audience: S) -> Self {
11        Self {
12            team_domain: Arc::new(team_domain.into()),
13            audience: Arc::new(audience.into()),
14        }
15    }
16
17    pub(crate) fn issuer(&self) -> String {
18        self.team_domain.to_string()
19    }
20
21    pub fn team_name(&self) -> String {
22        let domain = self
23            .team_domain
24            .strip_prefix("https://")
25            .or_else(|| self.team_domain.strip_prefix("http://"))
26            .unwrap_or(&self.team_domain);
27
28        domain.split('.').next().unwrap_or(domain).to_string()
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn test_auth_config_new() {
38        let config = AuthConfig::new("https://myteam.cloudflareaccess.com", "aud123");
39        assert_eq!(&*config.team_domain, "https://myteam.cloudflareaccess.com");
40        assert_eq!(&*config.audience, "aud123");
41    }
42
43    #[test]
44    fn test_auth_config_issuer() {
45        let config = AuthConfig::new("https://myteam.cloudflareaccess.com", "aud123");
46        assert_eq!(config.issuer(), "https://myteam.cloudflareaccess.com");
47    }
48
49    #[test]
50    fn test_team_name_with_https() {
51        let config = AuthConfig::new("https://myteam.cloudflareaccess.com", "aud");
52        assert_eq!(config.team_name(), "myteam");
53    }
54
55    #[test]
56    fn test_team_name_with_http() {
57        let config = AuthConfig::new("http://myteam.cloudflareaccess.com", "aud");
58        assert_eq!(config.team_name(), "myteam");
59    }
60
61    #[test]
62    fn test_team_name_without_protocol() {
63        let config = AuthConfig::new("myteam.cloudflareaccess.com", "aud");
64        assert_eq!(config.team_name(), "myteam");
65    }
66}