From 679c774cbe3e049ff98097b0d1b07c22d7c2140c Mon Sep 17 00:00:00 2001 From: Bernd Schoolmann Date: Thu, 7 Nov 2024 22:20:02 +0100 Subject: [PATCH] Add migration --- .../desktop_native/core/src/password/unix.rs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/apps/desktop/desktop_native/core/src/password/unix.rs b/apps/desktop/desktop_native/core/src/password/unix.rs index 3a52cbe736c..337ffe493f2 100644 --- a/apps/desktop/desktop_native/core/src/password/unix.rs +++ b/apps/desktop/desktop_native/core/src/password/unix.rs @@ -1,7 +1,17 @@ use anyhow::{anyhow, Result}; +use oo7::dbus::{self}; use std::collections::HashMap; pub async fn get_password(service: &str, account: &str) -> Result { + match get_password_new(service, account).await { + Ok(res) => Ok(res), + Err(_) => { + get_password_legacy(service, account).await + } + } +} + +async fn get_password_new(service: &str, account: &str) -> Result { let keyring = oo7::Keyring::new().await?; let attributes = HashMap::from([("service", service), ("account", account)]); let results = keyring.search_items(&attributes).await?; @@ -15,6 +25,31 @@ pub async fn get_password(service: &str, account: &str) -> Result { } } +// forces to read via secret service; remvove after 2024.03 +async fn get_password_legacy(service: &str, account: &str) -> Result { + println!("falling back to get legacy {} {}", service, account); + let svc = dbus::Service::new().await?; + let collection = match svc.default_collection().await { + Ok(c) => Ok(c), + Err(e) => Err(e), + }?; + let keyring = oo7::Keyring::DBus(collection); + let attributes = HashMap::from([("service", service), ("account", account)]); + let results = keyring.search_items(&attributes).await?; + let res = results.get(0); + match res { + Some(res) => { + let secret = res.secret().await?; + println!("deleting legacy secret service entry {} {}", service, account); + keyring.delete(&attributes).await?; + let secret_string = String::from_utf8(secret.to_vec())?; + set_password(service, account, &secret_string).await?; + Ok(secret_string) + }, + None => Err(anyhow!("no result")) + } +} + pub async fn set_password(service: &str, account: &str, password: &str) -> Result<()> { let keyring = oo7::Keyring::new().await?; let attributes = HashMap::from([("service", service), ("account", account)]);