| 1 |
//! # Examples |
| 2 |
//! |
| 3 |
//! ```rust,no_run |
| 4 |
//! use std::{io::Read, os::fd::AsFd}; |
| 5 |
//! |
| 6 |
//! use ashpd::desktop::secret::Secret; |
| 7 |
//! |
| 8 |
//! async fn run() -> ashpd::Result<()> { |
| 9 |
//! let secret = Secret::new().await?; |
| 10 |
//! |
| 11 |
//! let (mut x1, x2) = std::os::unix::net::UnixStream::pair()?; |
| 12 |
//! secret.retrieve(&x2, Default::default()).await?; |
| 13 |
//! drop(x2); |
| 14 |
//! let mut buf = Vec::new(); |
| 15 |
//! x1.read_to_end(&mut buf)?; |
| 16 |
//! |
| 17 |
//! Ok(()) |
| 18 |
//! } |
| 19 |
//! ``` |
| 20 |
|
| 21 |
use std::{ |
| 22 |
io::Read, |
| 23 |
os::{fd::AsFd, unix::net::UnixStream}, |
| 24 |
}; |
| 25 |
|
| 26 |
use serde::Serialize; |
| 27 |
use zbus::zvariant::{ |
| 28 |
Fd, Type, |
| 29 |
as_value::{self, optional}, |
| 30 |
}; |
| 31 |
|
| 32 |
use super::{HandleToken, Request}; |
| 33 |
use crate::{Error, proxy::Proxy}; |
| 34 |
|
| 35 |
#[derive(Serialize, Type, Debug, Default)] |
| 36 |
/// Specified options for a [`Secret::retrieve`] request. |
| 37 |
#[zvariant(signature = "dict")] |
| 38 |
pub struct RetrieveOptions { |
| 39 |
#[serde(with = "as_value")] |
| 40 |
handle_token: HandleToken, |
| 41 |
/// A string returned by a previous call to `retrieve`. |
| 42 |
/// TODO: seems to not be used by the portal... |
| 43 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 44 |
token: Option<String>, |
| 45 |
} |
| 46 |
|
| 47 |
/// The interface lets sandboxed applications retrieve a per-application secret. |
| 48 |
/// |
| 49 |
/// The secret can then be used for encrypting confidential data inside the |
| 50 |
/// sandbox. |
| 51 |
/// |
| 52 |
/// Wrapper of the DBus interface: [`org.freedesktop.portal.Secret`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Secret.html). |
| 53 |
#[derive(Debug)] |
| 54 |
#[doc(alias = "org.freedesktop.portal.Secret")] |
| 55 |
pub struct Secret(Proxy<'static>); |
| 56 |
|
| 57 |
impl Secret { |
| 58 |
/// Create a new instance of [`Secret`]. |
| 59 |
pub async fn new() -> Result<Secret, Error> { |
| 60 |
let proxy = Proxy::new_desktop("org.freedesktop.portal.Secret").await?; |
| 61 |
Ok(Self(proxy)) |
| 62 |
} |
| 63 |
|
| 64 |
/// Create a new instance of [`Secret`]. |
| 65 |
pub async fn with_connection(connection: zbus::Connection) -> Result<Secret, Error> { |
| 66 |
let proxy = |
| 67 |
Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.Secret").await?; |
| 68 |
Ok(Self(proxy)) |
| 69 |
} |
| 70 |
|
| 71 |
/// Returns the version of the portal interface. |
| 72 |
pub fn version(&self) -> u32 { |
| 73 |
self.0.version() |
| 74 |
} |
| 75 |
|
| 76 |
/// Retrieves a master secret for a sandboxed application. |
| 77 |
/// |
| 78 |
/// # Arguments |
| 79 |
/// |
| 80 |
/// * `fd` - Writaeble file descriptor for transporting the secret. |
| 81 |
/// |
| 82 |
/// # Specifications |
| 83 |
/// |
| 84 |
/// See also [`RetrieveSecret`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Secret.html#org-freedesktop-portal-secret-retrievesecret) |
| 85 |
#[doc(alias = "RetrieveSecret")] |
| 86 |
pub async fn retrieve( |
| 87 |
&self, |
| 88 |
fd: &impl AsFd, |
| 89 |
options: RetrieveOptions, |
| 90 |
) -> Result<Request<()>, Error> { |
| 91 |
self.0 |
| 92 |
.empty_request( |
| 93 |
&options.handle_token, |
| 94 |
"RetrieveSecret", |
| 95 |
&(Fd::from(fd), &options), |
| 96 |
) |
| 97 |
.await |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
impl std::ops::Deref for Secret { |
| 102 |
type Target = zbus::Proxy<'static>; |
| 103 |
|
| 104 |
fn deref(&self) -> &Self::Target { |
| 105 |
&self.0 |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/// A handy wrapper around [`Secret::retrieve`]. |
| 110 |
/// |
| 111 |
/// It creates a UnixStream internally for receiving the secret. |
| 112 |
pub async fn retrieve() -> Result<Vec<u8>, Error> { |
| 113 |
let proxy = Secret::new().await?; |
| 114 |
|
| 115 |
let (mut x1, x2) = UnixStream::pair()?; |
| 116 |
proxy.retrieve(&x2, Default::default()).await?; |
| 117 |
drop(x2); |
| 118 |
|
| 119 |
// Read the secret on a blocking thread since it's a small amount of data |
| 120 |
#[cfg(feature = "tokio")] |
| 121 |
let buf = tokio::task::spawn_blocking(move || { |
| 122 |
let mut buf = Vec::with_capacity(64); |
| 123 |
x1.read_to_end(&mut buf)?; |
| 124 |
Ok::<_, std::io::Error>(buf) |
| 125 |
}) |
| 126 |
.await |
| 127 |
.map_err(|e| Error::from(std::io::Error::other(e)))??; |
| 128 |
|
| 129 |
#[cfg(not(feature = "tokio"))] |
| 130 |
let buf = { |
| 131 |
let mut buf = Vec::with_capacity(64); |
| 132 |
x1.read_to_end(&mut buf)?; |
| 133 |
buf |
| 134 |
}; |
| 135 |
|
| 136 |
Ok(buf) |
| 137 |
} |