Skip to main content

node_device_session/
types.rs

1//! Public types for [`crate::serve`].
2
3use std::path::PathBuf;
4
5/// Provenance header stamped on every attested upstream request.
6pub const SESSION_HEADER: &str = "x-node-device-session";
7/// Path header stamped on every attested upstream request.
8pub const PATH_HEADER: &str = "x-node-device-session-path";
9/// Door member stamped on a pairing-only session's upstream requests (C-7).
10///
11/// Stripped from every client-supplied request in every session mode; only
12/// the pairing-only path sets it, from the [`PairingAdmission`].
13pub const PAIRING_DOOR_HEADER: &str = "x-node-pairing-door";
14/// Proven endpoint stamped beside [`PAIRING_DOOR_HEADER`] (C-7): the key the
15/// connection proved and the door token is bound to. Reserved the same way.
16pub const PAIRING_ENDPOINT_HEADER: &str = "x-node-pairing-endpoint";
17
18/// How the session reached the node (stamped on the ingress).
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub enum SessionPath {
21    /// Relayed QUIC / p2p path.
22    Relay,
23    /// WebRTC data-channel path.
24    WebRtc,
25}
26
27impl SessionPath {
28    /// Wire value for `x-node-device-session-path`.
29    pub fn as_str(self) -> &'static str {
30        match self {
31            Self::Relay => "relay",
32            Self::WebRtc => "webrtc",
33        }
34    }
35}
36
37/// Host-owned Unix socket that alone may attest device-session provenance.
38#[derive(Clone, Debug, Default)]
39pub struct IngressSocket {
40    /// Absolute path to the permissioned Unix listener, if bound.
41    pub path: Option<PathBuf>,
42}
43
44/// Node wire socket that `wire/1` streams splice onto (ADR-310).
45///
46/// Carriers pass a configured path (for example from `NODE_WIRE_SOCKET`).
47/// When [`Self::path`] is [`None`], `wire/1` is refused and existing HTTP/WS
48/// behaviour is unchanged.
49#[derive(Clone, Debug, Default)]
50pub struct WireSocket {
51    /// Absolute path to the node's trusted wire listener, if configured.
52    pub path: Option<PathBuf>,
53}
54
55/// Verified (or renewed) session identity returned by the host bus.
56#[derive(Clone, Debug, PartialEq, Eq)]
57pub struct Session {
58    /// Stable device id.
59    pub device_id: String,
60    /// Device DID.
61    pub device_did: String,
62    /// Granted scopes.
63    pub scopes: Vec<String>,
64    /// RFC3339 expiry.
65    pub expires_at: String,
66    /// Fresh credential (renew only); never logged.
67    pub credential: Option<String>,
68}
69
70/// A door token the host admitted for a pairing-only session (C-7).
71#[derive(Clone, Debug, PartialEq, Eq)]
72pub struct PairingAdmission {
73    /// The member node whose download door issued the token.
74    pub member_node_id: String,
75    /// When the token stops admitting sessions (epoch milliseconds).
76    pub expires_at_ms: i64,
77}
78
79/// A host refusal the device is allowed to see (`code` + public detail).
80#[derive(Clone, Debug, PartialEq, Eq)]
81pub struct Refusal {
82    /// Stable refusal code (e.g. `invalid_credential`).
83    pub code: String,
84    /// Safe-to-forward detail (no secrets).
85    pub message: String,
86}
87
88impl Refusal {
89    /// Build a refusal.
90    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
91        Self {
92            code: code.into(),
93            message: message.into(),
94        }
95    }
96
97    /// Wire form `"code: message"`.
98    pub fn wire(&self) -> String {
99        format!("{}: {}", self.code, self.message)
100    }
101}
102
103/// Why [`crate::serve`] returned.
104#[derive(Debug, thiserror::Error)]
105pub enum SessionEnd {
106    /// Peer closed or the accept loop ended cleanly.
107    #[error("session closed")]
108    Closed,
109    /// First stream was not a successful open/renew.
110    #[error("session not opened: {0}")]
111    NotOpened(String),
112    /// Open deadline elapsed.
113    #[error("open timed out")]
114    OpenTimeout,
115    /// Transport I/O failed.
116    #[error("transport: {0}")]
117    Transport(#[from] std::io::Error),
118}