Skip to main content

stakpak_shared/oauth/
config.rs

1//! OAuth configuration types
2
3/// Configuration for an OAuth 2.0 provider
4#[derive(Debug, Clone)]
5pub struct OAuthConfig {
6    /// OAuth client ID
7    pub client_id: String,
8    /// Authorization endpoint URL
9    pub auth_url: String,
10    /// Token exchange endpoint URL
11    pub token_url: String,
12    /// Redirect URI for authorization callback
13    pub redirect_url: String,
14    /// Scopes to request
15    pub scopes: Vec<String>,
16}
17
18impl OAuthConfig {
19    /// Create a new OAuth configuration
20    pub fn new(
21        client_id: impl Into<String>,
22        auth_url: impl Into<String>,
23        token_url: impl Into<String>,
24        redirect_url: impl Into<String>,
25        scopes: Vec<String>,
26    ) -> Self {
27        Self {
28            client_id: client_id.into(),
29            auth_url: auth_url.into(),
30            token_url: token_url.into(),
31            redirect_url: redirect_url.into(),
32            scopes,
33        }
34    }
35
36    /// Get the scopes as a space-separated string
37    pub fn scopes_string(&self) -> String {
38        self.scopes.join(" ")
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn test_oauth_config_creation() {
48        let config = OAuthConfig::new(
49            "client-id",
50            "https://example.com/auth",
51            "https://example.com/token",
52            "https://example.com/callback",
53            vec!["scope1".to_string(), "scope2".to_string()],
54        );
55
56        assert_eq!(config.client_id, "client-id");
57        assert_eq!(config.auth_url, "https://example.com/auth");
58        assert_eq!(config.token_url, "https://example.com/token");
59        assert_eq!(config.redirect_url, "https://example.com/callback");
60        assert_eq!(config.scopes, vec!["scope1", "scope2"]);
61    }
62
63    #[test]
64    fn test_scopes_string() {
65        let config = OAuthConfig::new(
66            "client-id",
67            "https://example.com/auth",
68            "https://example.com/token",
69            "https://example.com/callback",
70            vec!["read".to_string(), "write".to_string(), "admin".to_string()],
71        );
72
73        assert_eq!(config.scopes_string(), "read write admin");
74    }
75
76    #[test]
77    fn test_empty_scopes() {
78        let config = OAuthConfig::new(
79            "client-id",
80            "https://example.com/auth",
81            "https://example.com/token",
82            "https://example.com/callback",
83            vec![],
84        );
85
86        assert_eq!(config.scopes_string(), "");
87    }
88}