| 1 | //! Saved Google OAuth Desktop client (ADR 0024). |
| 2 | //! |
| 3 | //! The client id lives in store meta. The secret lives in the OS keyring and is |
| 4 | //! never written to the calendar account document or to logs. |
| 5 | |
| 6 | use store::Store; |
| 7 | |
| 8 | use chatsvc::GoogleOAuth; |
| 9 | |
| 10 | pub const CLIENT_ID_KEY: &str = "calendar.google_client_id"; |
| 11 | const SERVICE: &str = "rusty-teams.google-oauth"; |
| 12 | const ACCOUNT: &str = "desktop"; |
| 13 | |
| 14 | pub enum GoogleClientSave { |
| 15 | Saved, |
| 16 | Cleared, |
| 17 | } |
| 18 | |
| 19 | pub enum GoogleClientSaveError { |
| 20 | /// An id was given without a secret, and no secret is stored for that id. |
| 21 | SecretRequired, |
| 22 | Failed, |
| 23 | } |
| 24 | |
| 25 | pub fn load_client_id(store: &Store) -> String { |
| 26 | store |
| 27 | .meta(CLIENT_ID_KEY) |
| 28 | .ok() |
| 29 | .flatten() |
| 30 | .map(|value| value.trim().to_owned()) |
| 31 | .filter(|value| !value.is_empty()) |
| 32 | .unwrap_or_default() |
| 33 | } |
| 34 | |
| 35 | pub async fn secret_is_stored() -> bool { |
| 36 | load_secret() |
| 37 | .await |
| 38 | .map(|value| !value.trim().is_empty()) |
| 39 | .unwrap_or(false) |
| 40 | } |
| 41 | |
| 42 | /// Saved pair, then the environment, then the client compiled into the build. |
| 43 | pub async fn resolve(store: &Store) -> Option<GoogleOAuth> { |
| 44 | let stored_id = load_client_id(store); |
| 45 | // Without a saved id the keyring is never asked, so Secret Service stays out of every fetch. |
| 46 | let stored_secret = if stored_id.is_empty() { |
| 47 | String::new() |
| 48 | } else { |
| 49 | match load_secret().await { |
| 50 | Ok(secret) => secret, |
| 51 | Err(_) => { |
| 52 | log::warn!("saved Google client secret not read from the keyring"); |
| 53 | String::new() |
| 54 | } |
| 55 | } |
| 56 | }; |
| 57 | chatsvc::client_from_stored(Some(stored_id.as_str()), Some(stored_secret.as_str())) |
| 58 | } |
| 59 | |
| 60 | pub async fn save( |
| 61 | store: &Store, |
| 62 | client_id: &str, |
| 63 | secret: &str, |
| 64 | ) -> Result<GoogleClientSave, GoogleClientSaveError> { |
| 65 | let id = client_id.trim(); |
| 66 | let secret = secret.trim(); |
| 67 | if id.is_empty() { |
| 68 | store |
| 69 | .set_meta(CLIENT_ID_KEY, "") |
| 70 | .map_err(|_| GoogleClientSaveError::Failed)?; |
| 71 | delete_secret().await?; |
| 72 | return Ok(GoogleClientSave::Cleared); |
| 73 | } |
| 74 | if secret.is_empty() { |
| 75 | // The kept secret belongs to the saved id; a new id needs its own secret. |
| 76 | if load_client_id(store) != id { |
| 77 | return Err(GoogleClientSaveError::SecretRequired); |
| 78 | } |
| 79 | return match load_secret().await { |
| 80 | Ok(existing) if !existing.trim().is_empty() => Ok(GoogleClientSave::Saved), |
| 81 | _ => Err(GoogleClientSaveError::SecretRequired), |
| 82 | }; |
| 83 | } |
| 84 | // Secret before id: a failed keyring write must not leave a new id beside the old secret. |
| 85 | store_secret(secret).await?; |
| 86 | store |
| 87 | .set_meta(CLIENT_ID_KEY, id) |
| 88 | .map_err(|_| GoogleClientSaveError::Failed)?; |
| 89 | Ok(GoogleClientSave::Saved) |
| 90 | } |
| 91 | |
| 92 | async fn load_secret() -> Result<String, GoogleClientSaveError> { |
| 93 | spawn_blocking(move || { |
| 94 | let entry = |
| 95 | keyring::Entry::new(SERVICE, ACCOUNT).map_err(|_| GoogleClientSaveError::Failed)?; |
| 96 | entry |
| 97 | .get_password() |
| 98 | .map_err(|_| GoogleClientSaveError::Failed) |
| 99 | }) |
| 100 | .await |
| 101 | } |
| 102 | |
| 103 | async fn store_secret(secret: &str) -> Result<(), GoogleClientSaveError> { |
| 104 | let secret = secret.to_owned(); |
| 105 | spawn_blocking(move || { |
| 106 | let entry = |
| 107 | keyring::Entry::new(SERVICE, ACCOUNT).map_err(|_| GoogleClientSaveError::Failed)?; |
| 108 | entry |
| 109 | .set_password(&secret) |
| 110 | .map_err(|_| GoogleClientSaveError::Failed) |
| 111 | }) |
| 112 | .await |
| 113 | } |
| 114 | |
| 115 | async fn delete_secret() -> Result<(), GoogleClientSaveError> { |
| 116 | spawn_blocking(move || { |
| 117 | let entry = |
| 118 | keyring::Entry::new(SERVICE, ACCOUNT).map_err(|_| GoogleClientSaveError::Failed)?; |
| 119 | match entry.delete_credential() { |
| 120 | Ok(()) => Ok(()), |
| 121 | Err(keyring::Error::NoEntry) => Ok(()), |
| 122 | Err(_) => Err(GoogleClientSaveError::Failed), |
| 123 | } |
| 124 | }) |
| 125 | .await |
| 126 | } |
| 127 | |
| 128 | async fn spawn_blocking<T: Send + 'static>( |
| 129 | work: impl FnOnce() -> Result<T, GoogleClientSaveError> + Send + 'static, |
| 130 | ) -> Result<T, GoogleClientSaveError> { |
| 131 | match tokio::task::spawn_blocking(work).await { |
| 132 | Ok(result) => result, |
| 133 | Err(_) => Err(GoogleClientSaveError::Failed), |
| 134 | } |
| 135 | } |