Skip to main content

max / goingson

3.2 KB · 98 lines History Blame Raw
1 //! OAuth2 provider implementations.
2 //!
3 //! Implements OAuth2 authentication for various email providers:
4 //! - Fastmail (JMAP)
5 //! - Google/Gmail (Gmail API + IMAP XOAUTH2)
6 //! - Microsoft/Outlook (Graph API + IMAP XOAUTH2)
7
8 pub mod fastmail;
9 pub mod google;
10 pub mod microsoft;
11 pub use fastmail::FastmailProvider;
12 pub use google::GoogleProvider;
13 pub use microsoft::MicrosoftProvider;
14
15 use super::provider::OAuthProvider;
16 use goingson_core::EmailAuthType;
17
18 /// Registry of all supported OAuth providers.
19 pub struct ProviderRegistry {
20 providers: Vec<Box<dyn OAuthProvider>>,
21 }
22
23 impl ProviderRegistry {
24 /// Creates a new provider registry with default provider configurations.
25 ///
26 /// Client IDs should be loaded from environment or configuration.
27 pub fn new(config: ProviderConfig) -> Self {
28 let mut providers: Vec<Box<dyn OAuthProvider>> = Vec::new();
29
30 if let Some(client_id) = config.fastmail_client_id {
31 providers.push(Box::new(FastmailProvider::new(client_id)));
32 }
33
34 if let (Some(client_id), Some(client_secret)) = (config.google_client_id, config.google_client_secret) {
35 providers.push(Box::new(GoogleProvider::new(client_id, client_secret)));
36 }
37
38 if let Some(client_id) = config.microsoft_client_id {
39 providers.push(Box::new(MicrosoftProvider::new(client_id, config.microsoft_client_secret)));
40 }
41
42 Self { providers }
43 }
44
45 /// Returns a list of available provider IDs.
46 pub fn available_providers(&self) -> Vec<&'static str> {
47 self.providers.iter().map(|p| p.id()).collect()
48 }
49
50 /// Gets a provider by its ID.
51 pub fn get(&self, id: &str) -> Option<&dyn OAuthProvider> {
52 self.providers.iter().find(|p| p.id() == id).map(|p| p.as_ref())
53 }
54
55 /// Gets the auth type for a provider ID.
56 pub fn auth_type_for(id: &str) -> EmailAuthType {
57 match id {
58 "fastmail" => EmailAuthType::OAuth2Fastmail,
59 // Future providers will have their own EmailAuthType variants
60 _ => EmailAuthType::Password,
61 }
62 }
63 }
64
65 /// Configuration for OAuth providers.
66 ///
67 /// Client IDs and secrets should be loaded from environment variables
68 /// or a configuration file.
69 #[derive(Debug, Clone, Default)]
70 pub struct ProviderConfig {
71 /// Fastmail OAuth client ID.
72 pub fastmail_client_id: Option<String>,
73
74 /// Google OAuth client ID.
75 pub google_client_id: Option<String>,
76 /// Google OAuth client secret.
77 pub google_client_secret: Option<String>,
78
79 /// Microsoft OAuth client ID.
80 pub microsoft_client_id: Option<String>,
81 /// Microsoft OAuth client secret.
82 pub microsoft_client_secret: Option<String>,
83
84 }
85
86 impl ProviderConfig {
87 /// Loads provider configuration from environment variables.
88 pub fn from_env() -> Self {
89 Self {
90 fastmail_client_id: std::env::var("GOINGSON_FASTMAIL_CLIENT_ID").ok(),
91 google_client_id: std::env::var("GOINGSON_GOOGLE_CLIENT_ID").ok(),
92 google_client_secret: std::env::var("GOINGSON_GOOGLE_CLIENT_SECRET").ok(),
93 microsoft_client_id: std::env::var("GOINGSON_MICROSOFT_CLIENT_ID").ok(),
94 microsoft_client_secret: std::env::var("GOINGSON_MICROSOFT_CLIENT_SECRET").ok(),
95 }
96 }
97 }
98