| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
use chrono::{Duration, Utc}; |
| 10 |
use goingson_core::{EmailAccount, EmailAuthType}; |
| 11 |
|
| 12 |
use super::credentials::CredentialStore; |
| 13 |
use super::providers::{ProviderConfig, ProviderRegistry}; |
| 14 |
use super::OAuthProvider; |
| 15 |
|
| 16 |
|
| 17 |
pub struct TokenManager { |
| 18 |
|
| 19 |
registry: ProviderRegistry, |
| 20 |
} |
| 21 |
|
| 22 |
impl TokenManager { |
| 23 |
|
| 24 |
pub fn new(config: ProviderConfig) -> Self { |
| 25 |
Self { |
| 26 |
registry: ProviderRegistry::new(config), |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
pub fn from_env() -> Self { |
| 32 |
Self::new(ProviderConfig::from_env()) |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
pub fn registry(&self) -> &ProviderRegistry { |
| 37 |
&self.registry |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
pub fn provider(&self, id: &str) -> Option<&dyn OAuthProvider> { |
| 42 |
self.registry.get(id) |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
pub fn available_providers(&self) -> Vec<&'static str> { |
| 47 |
self.registry.available_providers() |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
pub fn provider_id_for_auth_type(auth_type: &EmailAuthType) -> Option<&'static str> { |
| 52 |
match auth_type { |
| 53 |
EmailAuthType::Password => None, |
| 54 |
EmailAuthType::OAuth2Fastmail => Some("fastmail"), |
| 55 |
EmailAuthType::OAuth2Google => Some("google"), |
| 56 |
EmailAuthType::OAuth2Microsoft => Some("microsoft"), |
| 57 |
EmailAuthType::OAuth2Yahoo => Some("yahoo"), |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
pub fn needs_refresh(account: &EmailAccount) -> bool { |
| 68 |
account.needs_token_refresh() |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
pub async fn refresh_if_needed( |
| 81 |
&self, |
| 82 |
account: &EmailAccount, |
| 83 |
) -> Result<Option<(String, Option<String>, chrono::DateTime<Utc>)>, String> { |
| 84 |
if !Self::needs_refresh(account) { |
| 85 |
return Ok(None); |
| 86 |
} |
| 87 |
|
| 88 |
let provider_id = Self::provider_id_for_auth_type(&account.auth_type) |
| 89 |
.ok_or_else(|| "Account does not use OAuth2".to_string())?; |
| 90 |
|
| 91 |
let provider = self.registry.get(provider_id) |
| 92 |
.ok_or_else(|| format!("OAuth provider '{}' not configured", provider_id))?; |
| 93 |
|
| 94 |
|
| 95 |
let refresh_token = CredentialStore::get_oauth(account.id.into()) |
| 96 |
.and_then(|c| c.refresh_token) |
| 97 |
.or_else(|| account.oauth2_refresh_token.clone()) |
| 98 |
.ok_or_else(|| "No refresh token available".to_string())?; |
| 99 |
|
| 100 |
let result = provider.refresh_token(&refresh_token).await?; |
| 101 |
|
| 102 |
|
| 103 |
let expires_at = Utc::now() |
| 104 |
+ Duration::seconds(result.expires_in.unwrap_or(3600) as i64); |
| 105 |
|
| 106 |
Ok(Some(( |
| 107 |
result.access_token, |
| 108 |
result.refresh_token, |
| 109 |
expires_at, |
| 110 |
))) |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
pub async fn get_valid_token( |
| 124 |
&self, |
| 125 |
account: &EmailAccount, |
| 126 |
) -> Result<TokenRefreshResult, String> { |
| 127 |
if !Self::needs_refresh(account) { |
| 128 |
|
| 129 |
let token = CredentialStore::get_oauth(account.id.into()) |
| 130 |
.map(|c| c.access_token) |
| 131 |
.or_else(|| account.oauth2_access_token.clone()) |
| 132 |
.ok_or_else(|| "No access token available".to_string())?; |
| 133 |
return Ok(TokenRefreshResult::Valid(token)); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
match self.refresh_if_needed(account).await? { |
| 138 |
Some((access_token, refresh_token, expires_at)) => { |
| 139 |
Ok(TokenRefreshResult::Refreshed { |
| 140 |
access_token, |
| 141 |
refresh_token, |
| 142 |
expires_at, |
| 143 |
}) |
| 144 |
} |
| 145 |
None => { |
| 146 |
|
| 147 |
let token = CredentialStore::get_oauth(account.id.into()) |
| 148 |
.map(|c| c.access_token) |
| 149 |
.or_else(|| account.oauth2_access_token.clone()) |
| 150 |
.ok_or_else(|| "No access token available".to_string())?; |
| 151 |
Ok(TokenRefreshResult::Valid(token)) |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
#[derive(Debug)] |
| 159 |
pub enum TokenRefreshResult { |
| 160 |
|
| 161 |
Valid(String), |
| 162 |
|
| 163 |
Refreshed { |
| 164 |
access_token: String, |
| 165 |
refresh_token: Option<String>, |
| 166 |
expires_at: chrono::DateTime<Utc>, |
| 167 |
}, |
| 168 |
} |
| 169 |
|
| 170 |
impl TokenRefreshResult { |
| 171 |
|
| 172 |
pub fn access_token(&self) -> &str { |
| 173 |
match self { |
| 174 |
TokenRefreshResult::Valid(token) => token, |
| 175 |
TokenRefreshResult::Refreshed { access_token, .. } => access_token, |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
pub fn was_refreshed(&self) -> bool { |
| 181 |
matches!(self, TokenRefreshResult::Refreshed { .. }) |
| 182 |
} |
| 183 |
} |
| 184 |
|