| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 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 |
|
| 19 |
pub struct ProviderRegistry { |
| 20 |
providers: Vec<Box<dyn OAuthProvider>>, |
| 21 |
} |
| 22 |
|
| 23 |
impl ProviderRegistry { |
| 24 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 46 |
pub fn available_providers(&self) -> Vec<&'static str> { |
| 47 |
self.providers.iter().map(|p| p.id()).collect() |
| 48 |
} |
| 49 |
|
| 50 |
|
| 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 |
|
| 56 |
pub fn auth_type_for(id: &str) -> EmailAuthType { |
| 57 |
match id { |
| 58 |
"fastmail" => EmailAuthType::OAuth2Fastmail, |
| 59 |
|
| 60 |
_ => EmailAuthType::Password, |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#[derive(Debug, Clone, Default)] |
| 70 |
pub struct ProviderConfig { |
| 71 |
|
| 72 |
pub fastmail_client_id: Option<String>, |
| 73 |
|
| 74 |
|
| 75 |
pub google_client_id: Option<String>, |
| 76 |
|
| 77 |
pub google_client_secret: Option<String>, |
| 78 |
|
| 79 |
|
| 80 |
pub microsoft_client_id: Option<String>, |
| 81 |
|
| 82 |
pub microsoft_client_secret: Option<String>, |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
impl ProviderConfig { |
| 87 |
|
| 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 |
|