From 1b0b0e9eaa546f50f6916cc631edaaa7dc8442e8 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 3 May 2020 02:38:01 -0400 Subject: also store org keys in the local db --- src/actions.rs | 17 +++++++++++++++-- src/api.rs | 24 +++++++++++++++++++++++- src/bin/rbw-agent/actions.rs | 11 +++++++---- src/db.rs | 1 + 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/actions.rs b/src/actions.rs index 9284677..b891670 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -49,7 +49,15 @@ pub async fn unlock( pub async fn sync( access_token: &str, refresh_token: &str, -) -> Result<(Option, (String, String, Vec))> { +) -> Result<( + Option, + ( + String, + String, + std::collections::HashMap, + Vec, + ), +)> { with_exchange_refresh_token_async( access_token, refresh_token, @@ -63,7 +71,12 @@ pub async fn sync( async fn sync_once( access_token: &str, -) -> Result<(String, String, Vec)> { +) -> Result<( + String, + String, + std::collections::HashMap, + Vec, +)> { let config = crate::config::Config::load_async().await?; let client = crate::api::Client::new(&config.base_url(), &config.identity_url()); diff --git a/src/api.rs b/src/api.rs index 41ed7be..03b9c6d 100644 --- a/src/api.rs +++ b/src/api.rs @@ -148,6 +148,16 @@ struct SyncResProfile { key: String, #[serde(rename = "PrivateKey")] private_key: String, + #[serde(rename = "Organizations")] + organizations: Vec, +} + +#[derive(serde::Deserialize, Debug)] +struct SyncResProfileOrganization { + #[serde(rename = "Id")] + id: String, + #[serde(rename = "Key")] + key: String, } #[derive(serde::Deserialize, Debug, Clone)] @@ -321,7 +331,12 @@ impl Client { pub async fn sync( &self, access_token: &str, - ) -> Result<(String, String, Vec)> { + ) -> Result<( + String, + String, + std::collections::HashMap, + Vec, + )> { let client = reqwest::Client::new(); let res = client .get(&self.api_url("/sync")) @@ -339,9 +354,16 @@ impl Client { .iter() .filter_map(|cipher| cipher.to_entry(&folders)) .collect(); + let org_keys = sync_res + .profile + .organizations + .iter() + .map(|org| (org.id.clone(), org.key.clone())) + .collect(); Ok(( sync_res.profile.key, sync_res.profile.private_key, + org_keys, ciphers, )) } diff --git a/src/bin/rbw-agent/actions.rs b/src/bin/rbw-agent/actions.rs index 5b3444d..e24d044 100644 --- a/src/bin/rbw-agent/actions.rs +++ b/src/bin/rbw-agent/actions.rs @@ -189,15 +189,18 @@ pub async fn sync(sock: &mut crate::sock::Sock) -> anyhow::Result<()> { } else { return Err(anyhow::anyhow!("failed to find refresh token in db")); }; - let (access_token, (protected_key, protected_private_key, entries)) = - rbw::actions::sync(&access_token, &refresh_token) - .await - .context("failed to sync database from server")?; + let ( + access_token, + (protected_key, protected_private_key, protected_org_keys, entries), + ) = rbw::actions::sync(&access_token, &refresh_token) + .await + .context("failed to sync database from server")?; if let Some(access_token) = access_token { db.access_token = Some(access_token); } db.protected_key = Some(protected_key); db.protected_private_key = Some(protected_private_key); + db.protected_org_keys = protected_org_keys; db.entries = entries; db.save_async(&email) .await diff --git a/src/db.rs b/src/db.rs index af7a29c..5f200ab 100644 --- a/src/db.rs +++ b/src/db.rs @@ -33,6 +33,7 @@ pub struct Db { pub iterations: Option, pub protected_key: Option, pub protected_private_key: Option, + pub protected_org_keys: std::collections::HashMap, pub entries: Vec, } -- cgit v1.2.3-54-g00ecf