async_ssh2/
agent.rs

1use crate::{util::run_ssh2_fn, Error};
2use smol::Async;
3use ssh2::{self, PublicKey};
4use std::{convert::From, net::TcpStream, sync::Arc};
5
6/// See [`Agent`](ssh2::Agent).
7pub struct Agent {
8    inner: ssh2::Agent,
9    stream: Arc<Async<TcpStream>>,
10}
11
12impl Agent {
13    pub(crate) fn new(agent: ssh2::Agent, stream: Arc<Async<TcpStream>>) -> Self {
14        Self {
15            inner: agent,
16            stream,
17        }
18    }
19
20    /// See [`connect`](ssh2::Agent::connect).
21    pub async fn connect(&mut self) -> Result<(), Error> {
22        run_ssh2_fn(&self.stream.clone(), || self.inner.connect()).await
23    }
24
25    /// See [`disconnect`](ssh2::Agent::disconnect).
26    pub async fn disconnect(&mut self) -> Result<(), Error> {
27        run_ssh2_fn(&self.stream.clone(), || self.inner.disconnect()).await
28    }
29
30    /// See [`list_identities`](ssh2::Agent::list_identities).
31    pub fn list_identities(&mut self) -> Result<(), Error> {
32        self.inner.list_identities().map_err(From::from)
33    }
34
35    /// See [`identities`](ssh2::Agent::identities).
36    pub fn identities(&self) -> Result<Vec<PublicKey>, Error> {
37        self.inner.identities().map_err(From::from)
38    }
39
40    /// See [`userauth`](ssh2::Agent::userauth).
41    pub async fn userauth(&self, username: &str, identity: &PublicKey) -> Result<(), Error> {
42        run_ssh2_fn(&self.stream.clone(), || {
43            self.inner.userauth(username, identity)
44        })
45        .await
46    }
47}