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/// What the host answers an open (tokenless) pairing session
80/// ([`crate::HostBus::admit_open_pairing`]): no door member is known, so the
81/// forwarded requests carry only the proven endpoint.
82#[derive(Clone, Debug, PartialEq, Eq)]
83pub struct OpenPairingAdmission {
84    /// When the session stops (epoch milliseconds); the session never lives
85    /// longer than the crate's pairing lifetime either.
86    pub expires_at_ms: i64,
87}
88
89/// A host refusal the device is allowed to see (`code` + public detail).
90#[derive(Clone, Debug, PartialEq, Eq)]
91pub struct Refusal {
92    /// Stable refusal code (e.g. `invalid_credential`).
93    pub code: String,
94    /// Safe-to-forward detail (no secrets).
95    pub message: String,
96}
97
98impl Refusal {
99    /// Build a refusal.
100    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
101        Self {
102            code: code.into(),
103            message: message.into(),
104        }
105    }
106
107    /// Wire form `"code: message"`.
108    pub fn wire(&self) -> String {
109        format!("{}: {}", self.code, self.message)
110    }
111}
112
113/// Why [`crate::serve`] returned.
114#[derive(Debug, thiserror::Error)]
115pub enum SessionEnd {
116    /// Peer closed or the accept loop ended cleanly.
117    #[error("session closed")]
118    Closed,
119    /// First stream was not a successful open/renew.
120    #[error("session not opened: {0}")]
121    NotOpened(String),
122    /// Open deadline elapsed.
123    #[error("open timed out")]
124    OpenTimeout,
125    /// Transport I/O failed.
126    #[error("transport: {0}")]
127    Transport(#[from] std::io::Error),
128}