1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
//! # Examples
//!
//! ```rust,no_run
//! use std::{io::Read, os::fd::AsFd};
//!
//! use ashpd::desktop::secret::Secret;
//!
//! async fn run() -> ashpd::Result<()> {
//! let secret = Secret::new().await?;
//!
//! let (mut x1, x2) = std::os::unix::net::UnixStream::pair()?;
//! secret.retrieve(&x2.as_fd()).await?;
//! drop(x2);
//! let mut buf = Vec::new();
//! x1.read_to_end(&mut buf)?;
//!
//! Ok(())
//! }
//! ```
use std::os::fd::{AsFd, BorrowedFd};
#[cfg(feature = "async-std")]
use async_net::unix::UnixStream;
#[cfg(feature = "async-std")]
use futures_util::AsyncReadExt;
#[cfg(feature = "tokio")]
use tokio::{io::AsyncReadExt, net::UnixStream};
use zbus::zvariant::{Fd, SerializeDict, Type};
use super::{HandleToken, Request};
use crate::{proxy::Proxy, Error};
#[derive(SerializeDict, Type, Debug, Default)]
/// Specified options for a [`Secret::retrieve`] request.
#[zvariant(signature = "dict")]
struct RetrieveOptions {
handle_token: HandleToken,
/// A string returned by a previous call to `retrieve`.
/// TODO: seems to not be used by the portal...
token: Option<String>,
}
/// The interface lets sandboxed applications retrieve a per-application secret.
///
/// The secret can then be used for encrypting confidential data inside the
/// sandbox.
///
/// Wrapper of the DBus interface: [`org.freedesktop.portal.Secret`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Secret.html).
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.Secret")]
pub struct Secret<'a>(Proxy<'a>);
impl<'a> Secret<'a> {
/// Create a new instance of [`Secret`].
pub async fn new() -> Result<Secret<'a>, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.Secret").await?;
Ok(Self(proxy))
}
/// Retrieves a master secret for a sandboxed application.
///
/// # Arguments
///
/// * `fd` - Writaeble file descriptor for transporting the secret.
///
/// # Specifications
///
/// See also [`RetrieveSecret`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Secret.html#org-freedesktop-portal-secret-retrievesecret)
#[doc(alias = "RetrieveSecret")]
pub async fn retrieve(&self, fd: &BorrowedFd<'_>) -> Result<Request<()>, Error> {
let options = RetrieveOptions::default();
self.0
.empty_request(
&options.handle_token,
"RetrieveSecret",
&(Fd::from(fd), &options),
)
.await
}
}
impl<'a> std::ops::Deref for Secret<'a> {
type Target = zbus::Proxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
/// A handy wrapper around [`Secret::retrieve`].
///
/// It crates a UnixStream internally for receiving the secret.
pub async fn retrieve() -> Result<Vec<u8>, Error> {
let proxy = Secret::new().await?;
let (mut x1, x2) = UnixStream::pair()?;
proxy.retrieve(&x2.as_fd()).await?;
drop(x2);
let mut buf = Vec::new();
x1.read_to_end(&mut buf).await?;
Ok(buf)
}