Load a stored edge authentication token for a gateway. Returns `None` if no token file exists or the file is empty. Falls back to the legacy `cf_token` path for backwards compatibility. When loading from the legacy path, migrates the token to the new path with proper permissions.
(gateway_name: &str)
| 41 | /// When loading from the legacy path, migrates the token to the new path |
| 42 | /// with proper permissions. |
| 43 | pub fn load_edge_token(gateway_name: &str) -> Option<String> { |
| 44 | // Try the new path first. |
| 45 | if let Some(path) = edge_token_path(gateway_name).ok().filter(|p| p.exists()) { |
| 46 | let contents = std::fs::read_to_string(&path).ok()?; |
| 47 | let token = contents.trim().to_string(); |
| 48 | if !token.is_empty() { |
| 49 | return Some(token); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Fall back to the legacy cf_token path. |
| 54 | let legacy_path = legacy_token_path(gateway_name) |
| 55 | .ok() |
| 56 | .filter(|p| p.exists())?; |
| 57 | let contents = std::fs::read_to_string(&legacy_path).ok()?; |
| 58 | let token = contents.trim().to_string(); |
| 59 | if token.is_empty() { |
| 60 | return None; |
| 61 | } |
| 62 | |
| 63 | // Migrate: write to new path with proper permissions, then remove legacy. |
| 64 | if store_edge_token(gateway_name, &token).is_ok() { |
| 65 | let _ = std::fs::remove_file(&legacy_path); |
| 66 | } |
| 67 | |
| 68 | Some(token) |
| 69 | } |
| 70 | |
| 71 | /// Remove a stored edge authentication token. |
| 72 | pub fn remove_edge_token(gateway_name: &str) -> Result<()> { |
no test coverage detected