Save tokens for a provider, creating the directory if needed.
(&self, provider_id: &str, tokens: &OAuthTokens)
| 31 | |
| 32 | /// Save tokens for a provider, creating the directory if needed. |
| 33 | pub fn save(&self, provider_id: &str, tokens: &OAuthTokens) -> Result<(), std::io::Error> { |
| 34 | std::fs::create_dir_all(&self.dir)?; |
| 35 | let path = self.token_path(provider_id); |
| 36 | let content = serde_json::to_string_pretty(tokens) |
| 37 | .map_err(|e| std::io::Error::other(format!("failed to serialize tokens: {e}")))?; |
| 38 | std::fs::write(&path, content)?; |
| 39 | |
| 40 | // Set file permissions to 0600 (owner read/write only) |
| 41 | #[cfg(unix)] |
| 42 | { |
| 43 | use std::os::unix::fs::PermissionsExt; |
| 44 | std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600))?; |
| 45 | } |
| 46 | |
| 47 | debug!(provider = %provider_id, path = %path.display(), "OAuth tokens saved"); |
| 48 | Ok(()) |
| 49 | } |
| 50 | |
| 51 | /// Load tokens for a provider, returning None if the file doesn't exist. |
| 52 | pub fn load(&self, provider_id: &str) -> Result<Option<OAuthTokens>, std::io::Error> { |