| 1 |
// Fragments - secret_store.rs |
| 2 |
// Copyright (C) 2022-2024 Felix Häcker <[email protected]> |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License |
| 15 |
// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
use std::collections::HashMap; |
| 18 |
use std::sync::OnceLock; |
| 19 |
|
| 20 |
use transmission_gobject::TrAuthentication; |
| 21 |
|
| 22 |
use crate::backend::FrgConnection; |
| 23 |
|
| 24 |
static KEYRING: OnceLock<oo7::Keyring> = OnceLock::new(); |
| 25 |
|
| 26 |
pub async fn open_keyring() -> oo7::Result<()> { |
| 27 |
let keyring = oo7::Keyring::new().await?; |
| 28 |
|
| 29 |
if KEYRING.set(keyring).is_err() { |
| 30 |
debug!("Keyring was already set"); |
| 31 |
} |
| 32 |
|
| 33 |
Ok(()) |
| 34 |
} |
| 35 |
|
| 36 |
pub async fn store(conn: &FrgConnection, auth: &TrAuthentication) -> Result<(), oo7::Error> { |
| 37 |
remove(conn).await?; |
| 38 |
|
| 39 |
let username = auth.username(); |
| 40 |
let uuid = conn.uuid(); |
| 41 |
|
| 42 |
let attributes: HashMap<&str, &str> = |
| 43 |
[("username", username.as_str()), ("uuid", uuid.as_str())] |
| 44 |
.iter() |
| 45 |
.cloned() |
| 46 |
.collect(); |
| 47 |
|
| 48 |
let title = format!("Fragments ({})", conn.title()); |
| 49 |
|
| 50 |
KEYRING.get().unwrap().unlock().await?; |
| 51 |
KEYRING |
| 52 |
.get() |
| 53 |
.unwrap() |
| 54 |
.create_item(&title, &attributes, auth.password().as_bytes(), true) |
| 55 |
.await?; |
| 56 |
|
| 57 |
Ok(()) |
| 58 |
} |
| 59 |
|
| 60 |
pub async fn get(conn: &FrgConnection) -> Result<Option<TrAuthentication>, oo7::Error> { |
| 61 |
let uuid = conn.uuid(); |
| 62 |
let attributes: HashMap<&str, &str> = [("uuid", uuid.as_str())].iter().cloned().collect(); |
| 63 |
|
| 64 |
KEYRING.get().unwrap().unlock().await?; |
| 65 |
let items = KEYRING.get().unwrap().search_items(&attributes).await?; |
| 66 |
|
| 67 |
if let Some(item) = items.first() { |
| 68 |
let attributes = item.attributes().await?; |
| 69 |
|
| 70 |
let username = attributes |
| 71 |
.get("username") |
| 72 |
.expect("Couldn't get secretservice username attribute"); |
| 73 |
|
| 74 |
let password = String::from_utf8(item.secret().await?.to_vec()) |
| 75 |
.expect("Couldn't parse secretservice secret"); |
| 76 |
|
| 77 |
Ok(Some(TrAuthentication::new(username, &password))) |
| 78 |
} else { |
| 79 |
Ok(None) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
pub async fn remove(conn: &FrgConnection) -> Result<(), oo7::Error> { |
| 84 |
let uuid = conn.uuid(); |
| 85 |
let attributes: HashMap<&str, &str> = [("uuid", uuid.as_str())].iter().cloned().collect(); |
| 86 |
|
| 87 |
KEYRING.get().unwrap().unlock().await?; |
| 88 |
KEYRING.get().unwrap().delete(&attributes).await?; |
| 89 |
|
| 90 |
Ok(()) |
| 91 |
} |