aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2020-04-18 03:45:49 -0400
committerJesse Luehrs <doy@tozt.net>2020-04-18 03:45:49 -0400
commitf2e12534b876e73a0c7c4593acb23ac200529309 (patch)
tree3348552733e4fa812d513468ce0f172a27a466ef
parentf1d63c8bfb76b218822ee417e98a9015877ab528 (diff)
downloadrbw-f2e12534b876e73a0c7c4593acb23ac200529309.tar.gz
rbw-f2e12534b876e73a0c7c4593acb23ac200529309.zip
refactor
-rw-r--r--src/actions.rs41
-rw-r--r--src/bin/rbw-agent/actions.rs30
-rw-r--r--src/bin/rbw/commands.rs50
3 files changed, 62 insertions, 59 deletions
diff --git a/src/actions.rs b/src/actions.rs
index 07cf6ba..71dd96a 100644
--- a/src/actions.rs
+++ b/src/actions.rs
@@ -45,6 +45,23 @@ pub async fn unlock(
pub async fn sync(
access_token: &str,
+ refresh_token: &str,
+) -> Result<(Option<String>, String, Vec<crate::api::Cipher>)> {
+ let res = sync_once(access_token).await;
+ match res {
+ Ok((protected_key, ciphers)) => Ok((None, protected_key, ciphers)),
+ Err(crate::error::Error::RequestUnauthorized) => {
+ let access_token =
+ exchange_refresh_token_async(refresh_token).await?;
+ let (protected_key, ciphers) = sync_once(&access_token).await?;
+ Ok((Some(access_token), protected_key, ciphers))
+ }
+ Err(e) => Err(e),
+ }
+}
+
+async fn sync_once(
+ access_token: &str,
) -> Result<(String, Vec<crate::api::Cipher>)> {
let config = crate::config::Config::load_async().await?;
let client =
@@ -52,7 +69,23 @@ pub async fn sync(
client.sync(access_token).await
}
-pub fn add(access_token: &str, cipher: &crate::api::Cipher) -> Result<()> {
+pub fn add(
+ access_token: &str,
+ refresh_token: &str,
+ cipher: &crate::api::Cipher,
+) -> Result<Option<String>> {
+ match add_once(access_token, cipher) {
+ Ok(()) => Ok(None),
+ Err(crate::error::Error::RequestUnauthorized) => {
+ let access_token = exchange_refresh_token(refresh_token)?;
+ add_once(&access_token, cipher)?;
+ Ok(Some(access_token))
+ }
+ Err(e) => Err(e),
+ }
+}
+
+fn add_once(access_token: &str, cipher: &crate::api::Cipher) -> Result<()> {
let config = crate::config::Config::load()?;
let client =
crate::api::Client::new(&config.base_url(), &config.identity_url());
@@ -60,16 +93,14 @@ pub fn add(access_token: &str, cipher: &crate::api::Cipher) -> Result<()> {
Ok(())
}
-pub fn exchange_refresh_token(refresh_token: &str) -> Result<String> {
+fn exchange_refresh_token(refresh_token: &str) -> Result<String> {
let config = crate::config::Config::load()?;
let client =
crate::api::Client::new(&config.base_url(), &config.identity_url());
client.exchange_refresh_token(refresh_token)
}
-pub async fn exchange_refresh_token_async(
- refresh_token: &str,
-) -> Result<String> {
+async fn exchange_refresh_token_async(refresh_token: &str) -> Result<String> {
let config = crate::config::Config::load_async().await?;
let client =
crate::api::Client::new(&config.base_url(), &config.identity_url());
diff --git a/src/bin/rbw-agent/actions.rs b/src/bin/rbw-agent/actions.rs
index 9559f73..75c1c25 100644
--- a/src/bin/rbw-agent/actions.rs
+++ b/src/bin/rbw-agent/actions.rs
@@ -132,28 +132,18 @@ pub async fn sync(sock: &mut crate::sock::Sock) -> anyhow::Result<()> {
} else {
return Err(anyhow::anyhow!("failed to find access token in db"));
};
- let res = rbw::actions::sync(&access_token).await;
- let res = if let Err(e) = &res {
- if let rbw::error::Error::RequestUnauthorized = e {
- if let Some(refresh_token) = &db.refresh_token {
- let access_token =
- rbw::actions::exchange_refresh_token_async(refresh_token)
- .await?;
- db.access_token = Some(access_token.clone());
- rbw::actions::sync(&access_token).await
- } else {
- return Err(anyhow::anyhow!(
- "failed to find refresh token in db"
- ));
- }
- } else {
- res
- }
+ let refresh_token = if let Some(refresh_token) = &db.refresh_token {
+ refresh_token.clone()
} else {
- res
+ return Err(anyhow::anyhow!("failed to find refresh token in db"));
};
- let (protected_key, ciphers) =
- res.context("failed to sync database from server")?;
+ let (access_token, protected_key, ciphers) =
+ 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.ciphers = ciphers;
db.save_async(&email)
diff --git a/src/bin/rbw/commands.rs b/src/bin/rbw/commands.rs
index acf3f32..253ce5d 100644
--- a/src/bin/rbw/commands.rs
+++ b/src/bin/rbw/commands.rs
@@ -114,8 +114,9 @@ pub fn add(name: &str, username: Option<&str>) -> anyhow::Result<()> {
let email = config_email()?;
let mut db = rbw::db::Db::load(&email)?;
// unwrap is safe here because the call to unlock above is guaranteed to
- // populate it or error
- let access_token = db.access_token.unwrap();
+ // populate these or error
+ let access_token = db.access_token.as_ref().unwrap();
+ let refresh_token = db.refresh_token.as_ref().unwrap();
let name = crate::actions::encrypt(name)?;
@@ -153,21 +154,11 @@ pub fn add(name: &str, username: Option<&str>) -> anyhow::Result<()> {
notes,
};
- let res = rbw::actions::add(&access_token, &cipher);
- if let Err(e) = &res {
- if let rbw::error::Error::RequestUnauthorized = e {
- if let Some(refresh_token) = &db.refresh_token {
- let access_token =
- rbw::actions::exchange_refresh_token(refresh_token)?;
- db.access_token = Some(access_token.clone());
- db.save(&email).context("failed to save database")?;
- rbw::actions::add(&access_token, &cipher)?;
- } else {
- return Err(anyhow::anyhow!(
- "failed to find refresh token in db"
- ));
- }
- }
+ if let Some(access_token) =
+ rbw::actions::add(&access_token, &refresh_token, &cipher)?
+ {
+ db.access_token = Some(access_token);
+ db.save(&email).context("failed to save database")?;
}
crate::actions::sync()?;
@@ -190,8 +181,9 @@ pub fn generate(
let email = config_email()?;
let mut db = rbw::db::Db::load(&email)?;
// unwrap is safe here because the call to unlock above is guaranteed
- // to populate it or error
- let access_token = db.access_token.unwrap();
+ // to populate these or error
+ let access_token = db.access_token.as_ref().unwrap();
+ let refresh_token = db.refresh_token.as_ref().unwrap();
let name = crate::actions::encrypt(name)?;
let username = username
@@ -208,21 +200,11 @@ pub fn generate(
notes: None,
};
- let res = rbw::actions::add(&access_token, &cipher);
- if let Err(e) = &res {
- if let rbw::error::Error::RequestUnauthorized = e {
- if let Some(refresh_token) = &db.refresh_token {
- let access_token =
- rbw::actions::exchange_refresh_token(refresh_token)?;
- db.access_token = Some(access_token.clone());
- db.save(&email).context("failed to save database")?;
- rbw::actions::add(&access_token, &cipher)?;
- } else {
- return Err(anyhow::anyhow!(
- "failed to find refresh token in db"
- ));
- }
- }
+ if let Some(access_token) =
+ rbw::actions::add(&access_token, &refresh_token, &cipher)?
+ {
+ db.access_token = Some(access_token);
+ db.save(&email).context("failed to save database")?;
}
crate::actions::sync()?;