aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/rbw-agent/actions.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/rbw-agent/actions.rs')
-rw-r--r--src/bin/rbw-agent/actions.rs56
1 files changed, 31 insertions, 25 deletions
diff --git a/src/bin/rbw-agent/actions.rs b/src/bin/rbw-agent/actions.rs
index 3ca6d51..4e76d4b 100644
--- a/src/bin/rbw-agent/actions.rs
+++ b/src/bin/rbw-agent/actions.rs
@@ -5,12 +5,7 @@ pub async fn login(
state: std::sync::Arc<tokio::sync::RwLock<crate::agent::State>>,
tty: Option<&str>,
) -> anyhow::Result<()> {
- let email = config_email()
- .await
- .context("failed to read email from config")?;
- let mut db = rbw::db::Db::load_async(&email)
- .await
- .unwrap_or_else(|_| rbw::db::Db::new());
+ let mut db = load_db().await.unwrap_or_else(|_| rbw::db::Db::new());
if db.needs_login() {
let url_str = config_base_url()
@@ -27,6 +22,8 @@ pub async fn login(
));
};
+ let email = config_email().await?;
+
for i in 1_u8..=3 {
let err = if i > 1 {
Some(format!("Incorrect password (attempt {}/3)", i))
@@ -56,9 +53,7 @@ pub async fn login(
db.refresh_token = Some(refresh_token);
db.iterations = Some(iterations);
db.protected_key = Some(protected_key);
- db.save_async(&email)
- .await
- .context("failed to save local database")?;
+ save_db(&db).await?;
break;
}
@@ -93,13 +88,7 @@ pub async fn unlock(
tty: Option<&str>,
) -> anyhow::Result<()> {
if state.read().await.needs_unlock() {
- let email = config_email()
- .await
- .context("failed to read email from config")?;
-
- let db = rbw::db::Db::load_async(&email)
- .await
- .context("failed to load local database")?;
+ let db = load_db().await?;
let iterations = if let Some(iterations) = db.iterations {
iterations
@@ -124,6 +113,8 @@ pub async fn unlock(
));
};
+ let email = config_email().await?;
+
for i in 1u8..=3 {
let err = if i > 1 {
Some(format!("Incorrect password (attempt {}/3)", i))
@@ -184,12 +175,7 @@ pub async fn lock(
}
pub async fn sync(sock: &mut crate::sock::Sock) -> anyhow::Result<()> {
- let email = config_email()
- .await
- .context("failed to read email from config")?;
- let mut db = rbw::db::Db::load_async(&email)
- .await
- .context("failed to load local database")?;
+ let mut db = load_db().await?;
let access_token = if let Some(access_token) = &db.access_token {
access_token.clone()
@@ -214,9 +200,7 @@ pub async fn sync(sock: &mut crate::sock::Sock) -> anyhow::Result<()> {
db.protected_private_key = Some(protected_private_key);
db.protected_org_keys = protected_org_keys;
db.entries = entries;
- db.save_async(&email)
- .await
- .context("failed to save database")?;
+ save_db(&db).await?;
respond_ack(sock).await?;
@@ -327,6 +311,28 @@ async fn config_email() -> anyhow::Result<String> {
}
}
+async fn load_db() -> anyhow::Result<rbw::db::Db> {
+ let config = rbw::config::Config::load_async().await?;
+ if let Some(email) = &config.email {
+ rbw::db::Db::load_async(&config.server_name(), &email)
+ .await
+ .context("failed to load password database")
+ } else {
+ Err(anyhow::anyhow!("failed to find email address in config"))
+ }
+}
+
+async fn save_db(db: &rbw::db::Db) -> anyhow::Result<()> {
+ let config = rbw::config::Config::load_async().await?;
+ if let Some(email) = &config.email {
+ db.save_async(&config.server_name(), &email)
+ .await
+ .context("failed to save password database")
+ } else {
+ Err(anyhow::anyhow!("failed to find email address in config"))
+ }
+}
+
async fn config_base_url() -> anyhow::Result<String> {
let config = rbw::config::Config::load_async()
.await