Get the current access token for a provider, refreshing if expired.
(&self, provider_id: &str)
| 242 | |
| 243 | /// Get the current access token for a provider, refreshing if expired. |
| 244 | pub async fn current_access_token(&self, provider_id: &str) -> Result<String, OAuthError> { |
| 245 | { |
| 246 | let tokens = self.tokens.read().await; |
| 247 | if let Some(t) = tokens.get(provider_id) { |
| 248 | if !t.is_expired() { |
| 249 | return Ok(t.access_token.clone()); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | // Token is expired or missing — try refreshing |
| 254 | self.refresh(provider_id).await?; |
| 255 | let tokens = self.tokens.read().await; |
| 256 | tokens |
| 257 | .get(provider_id) |
| 258 | .map(|t| t.access_token.clone()) |
| 259 | .ok_or_else(|| OAuthError::NoTokens(provider_id.to_string())) |
| 260 | } |
| 261 | |
| 262 | /// Inject auth headers for the given provider. |
| 263 | pub async fn inject_auth( |