Skip to content

Commit 0aefece

Browse files
committed
Add doc example to agent::Session
Signed-off-by: Wiktor Kwapisiewicz <wiktor@metacode.biz>
1 parent 16ba495 commit 0aefece

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/agent.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,43 @@ impl ListeningSocket for NamedPipeListener {
9191
///
9292
/// This type is implemented by agents that want to handle incoming SSH agent
9393
/// connections.
94+
///
95+
/// # Examples
96+
///
97+
/// The following examples shows the most minimal [`Session`]
98+
/// implementation: one that returns a list of public keys that it
99+
/// manages and signs all incoming signing requests.
100+
///
101+
/// Note that the `MyAgent` struct is cloned for all new sessions
102+
/// (incoming connections). If the cloning needs special behavior
103+
/// implementing [`Clone`] manually is a viable approach. If the newly
104+
/// created sessions require information from the underlying socket it
105+
/// is advisable to implement the [`Agent`] trait.
106+
///
107+
/// ```
108+
/// use ssh_agent_lib::{agent::Session, error::AgentError};
109+
/// use ssh_agent_lib::proto::{Identity, SignRequest};
110+
/// use ssh_key::{Algorithm, Signature};
111+
///
112+
/// #[derive(Default, Clone)]
113+
/// struct MyAgent;
114+
///
115+
/// #[ssh_agent_lib::async_trait]
116+
/// impl Session for MyAgent {
117+
/// async fn request_identities(&mut self) -> Result<Vec<Identity>, AgentError> {
118+
/// Ok(vec![ /* public keys that this agent knows of */ ])
119+
/// }
120+
///
121+
/// async fn sign(&mut self, request: SignRequest) -> Result<Signature, AgentError> {
122+
/// // get the signature by signing `request.data`
123+
/// let signature = vec![];
124+
/// Ok(Signature::new(
125+
/// Algorithm::new("algorithm").map_err(AgentError::other)?,
126+
/// signature,
127+
/// ).map_err(AgentError::other)?)
128+
/// }
129+
/// }
130+
/// ```
94131
#[async_trait]
95132
pub trait Session: 'static + Sync + Send + Unpin {
96133
/// Request a list of keys managed by this session.

0 commit comments

Comments
 (0)