use std::{io::Read, os::unix::prelude::AsRawFd};
use zbus::zvariant::{Fd, SerializeDict, Type};
use super::{HandleToken, DESTINATION, PATH};
use crate::{helpers::call_basic_response_method, Error};
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct RetrieveOptions {
handle_token: HandleToken,
token: Option<String>,
}
impl RetrieveOptions {
#[must_use]
#[allow(unused)]
pub fn token(mut self, token: &str) -> Self {
self.token = Some(token.to_owned());
self
}
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.Secret")]
pub struct SecretProxy<'a>(zbus::Proxy<'a>);
impl<'a> SecretProxy<'a> {
pub async fn new(connection: &zbus::Connection) -> Result<SecretProxy<'a>, Error> {
let proxy = zbus::ProxyBuilder::new_bare(connection)
.interface("org.freedesktop.portal.Secret")?
.path(PATH)?
.destination(DESTINATION)?
.build()
.await?;
Ok(Self(proxy))
}
pub fn inner(&self) -> &zbus::Proxy<'_> {
&self.0
}
#[doc(alias = "RetrieveSecret")]
pub async fn retrieve_secret(&self, fd: &impl AsRawFd) -> Result<(), Error> {
let options = RetrieveOptions::default();
call_basic_response_method(
self.inner(),
&options.handle_token,
"RetrieveSecret",
&(Fd::from(fd.as_raw_fd()), &options),
)
.await?;
Ok(())
}
}
pub async fn retrieve_secret() -> Result<Vec<u8>, Error> {
let connection = zbus::Connection::session().await?;
let proxy = SecretProxy::new(&connection).await?;
let (mut x1, x2) = std::os::unix::net::UnixStream::pair()?;
proxy.retrieve_secret(&x2).await?;
drop(x2);
let mut buf = Vec::new();
x1.read_to_end(&mut buf)?;
Ok(buf)
}