tor_rpc_connect/
auth.rs

1//! Representations for types of required RPC connections.
2
3use std::sync::Arc;
4
5pub mod cookie;
6
7/// A type of authentication required on an RPC connection.
8#[derive(Clone, Debug)]
9#[non_exhaustive]
10pub enum RpcAuth {
11    /// No authentication is expected on the connection:
12    /// just being able to make the connection proves that the client is authorized.
13    Inherent,
14    /// RPC cookie authentication is expected on the connection.
15    Cookie {
16        /// A secret cookie to use for authentication.
17        secret: RpcCookieSource,
18        /// The address that the server is listening on,
19        /// encoded as a string.
20        server_address: String,
21    },
22}
23
24/// A way to get an RPC cookie: Either in-memory, or by loading it from disk.
25///
26/// (We defer loading cookies when running as a client,
27/// since clients should not actually load cookies from disk
28/// until they have received the server's banner message.)
29#[derive(Clone, Debug)]
30#[non_exhaustive]
31pub enum RpcCookieSource {
32    /// A cookie that is already loaded.
33    Loaded(Arc<cookie::Cookie>),
34    /// A cookie that's stored on disk and needs to be loaded.
35    Unloaded(cookie::CookieLocation),
36}
37
38impl RpcCookieSource {
39    /// Try to load this cookie from disk, if it is not already loaded.
40    pub fn load(&self) -> Result<Arc<cookie::Cookie>, cookie::CookieAccessError> {
41        match self {
42            RpcCookieSource::Loaded(cookie) => Ok(Arc::clone(cookie)),
43            RpcCookieSource::Unloaded(cookie_location) => Ok(Arc::new(cookie_location.load()?)),
44        }
45    }
46}