mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-26 19:57:25 +01:00
Always use SecretService asynchronously
This commit is contained in:
parent
5d06ec9faf
commit
9256e60122
2 changed files with 71 additions and 62 deletions
|
|
@ -74,7 +74,7 @@ impl Backend {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(data) = secure::load_login_data()? {
|
if let Some(data) = Self::load_login_data().await? {
|
||||||
self.client.set_login_data(data);
|
self.client.set_login_data(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -95,7 +95,7 @@ impl Backend {
|
||||||
|
|
||||||
/// Set the user credentials to use.
|
/// Set the user credentials to use.
|
||||||
pub async fn set_login_data(&self, data: LoginData) -> Result<()> {
|
pub async fn set_login_data(&self, data: LoginData) -> Result<()> {
|
||||||
secure::store_login_data(data.clone()).await?;
|
Self::store_login_data(data.clone()).await?;
|
||||||
self.client.set_login_data(data);
|
self.client.set_login_data(data);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,36 +1,29 @@
|
||||||
use crate::Result;
|
use crate::{Backend, Result};
|
||||||
use musicus_client::LoginData;
|
use musicus_client::LoginData;
|
||||||
use futures_channel::oneshot;
|
use futures_channel::oneshot;
|
||||||
use secret_service::{Collection, EncryptionType, SecretService};
|
use secret_service::{Collection, EncryptionType, SecretService};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
/// Savely store the user's current login credentials.
|
impl Backend {
|
||||||
pub async fn store_login_data(data: LoginData) -> Result<()> {
|
/// Get the login credentials from secret storage.
|
||||||
|
pub(super) async fn load_login_data() -> Result<Option<LoginData>> {
|
||||||
let (sender, receiver) = oneshot::channel();
|
let (sender, receiver) = oneshot::channel();
|
||||||
thread::spawn(move || sender.send(store_login_data_priv(data)).unwrap());
|
thread::spawn(move || sender.send(Self::load_login_data_priv()).unwrap());
|
||||||
receiver.await?
|
receiver.await?
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Savely store the user's current login credentials.
|
/// Savely store the user's current login credentials.
|
||||||
fn store_login_data_priv(data: LoginData) -> Result<()> {
|
pub(super) async fn store_login_data(data: LoginData) -> Result<()> {
|
||||||
let ss = SecretService::new(EncryptionType::Dh)?;
|
let (sender, receiver) = oneshot::channel();
|
||||||
let collection = get_collection(&ss)?;
|
thread::spawn(move || sender.send(Self::store_login_data_priv(data)).unwrap());
|
||||||
|
receiver.await?
|
||||||
let key = "musicus-login-data";
|
|
||||||
delete_secrets(&collection, key)?;
|
|
||||||
|
|
||||||
let mut attributes = HashMap::new();
|
|
||||||
attributes.insert("username", data.username.as_str());
|
|
||||||
collection.create_item(key, attributes, data.password.as_bytes(), true, "text/plain")?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the login credentials from secret storage.
|
/// Get the login credentials from secret storage.
|
||||||
pub fn load_login_data() -> Result<Option<LoginData>> {
|
fn load_login_data_priv() -> Result<Option<LoginData>> {
|
||||||
let ss = SecretService::new(EncryptionType::Dh)?;
|
let ss = SecretService::new(EncryptionType::Dh)?;
|
||||||
let collection = get_collection(&ss)?;
|
let collection = Self::get_collection(&ss)?;
|
||||||
|
|
||||||
let items = collection.get_all_items()?;
|
let items = collection.get_all_items()?;
|
||||||
|
|
||||||
|
|
@ -49,6 +42,21 @@ pub fn load_login_data() -> Result<Option<LoginData>> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Savely store the user's current login credentials.
|
||||||
|
fn store_login_data_priv(data: LoginData) -> Result<()> {
|
||||||
|
let ss = SecretService::new(EncryptionType::Dh)?;
|
||||||
|
let collection = Self::get_collection(&ss)?;
|
||||||
|
|
||||||
|
let key = "musicus-login-data";
|
||||||
|
Self::delete_secrets(&collection, key)?;
|
||||||
|
|
||||||
|
let mut attributes = HashMap::new();
|
||||||
|
attributes.insert("username", data.username.as_str());
|
||||||
|
collection.create_item(key, attributes, data.password.as_bytes(), true, "text/plain")?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Delete all stored secrets for the provided key.
|
/// Delete all stored secrets for the provided key.
|
||||||
fn delete_secrets(collection: &Collection, key: &str) -> Result<()> {
|
fn delete_secrets(collection: &Collection, key: &str) -> Result<()> {
|
||||||
let items = collection.get_all_items()?;
|
let items = collection.get_all_items()?;
|
||||||
|
|
@ -69,3 +77,4 @@ fn get_collection<'a>(ss: &'a SecretService) -> Result<Collection<'a>> {
|
||||||
|
|
||||||
Ok(collection)
|
Ok(collection)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue