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
10/// How the session reached the node (stamped on the ingress).
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12pub enum SessionPath {
13    /// Relayed QUIC / p2p path.
14    Relay,
15    /// WebRTC data-channel path.
16    WebRtc,
17}
18
19impl SessionPath {
20    /// Wire value for `x-node-device-session-path`.
21    pub fn as_str(self) -> &'static str {
22        match self {
23            Self::Relay => "relay",
24            Self::WebRtc => "webrtc",
25        }
26    }
27}
28
29/// Host-owned Unix socket that alone may attest device-session provenance.
30#[derive(Clone, Debug, Default)]
31pub struct IngressSocket {
32    /// Absolute path to the permissioned Unix listener, if bound.
33    pub path: Option<PathBuf>,
34}
35
36/// Node wire socket that `wire/1` streams splice onto (ADR-310).
37///
38/// Carriers pass a configured path (for example from `NODE_WIRE_SOCKET`).
39/// When [`Self::path`] is [`None`], `wire/1` is refused and existing HTTP/WS
40/// behaviour is unchanged.
41#[derive(Clone, Debug, Default)]
42pub struct WireSocket {
43    /// Absolute path to the node's trusted wire listener, if configured.
44    pub path: Option<PathBuf>,
45}
46
47/// Verified (or renewed) session identity returned by the host bus.
48#[derive(Clone, Debug, PartialEq, Eq)]
49pub struct Session {
50    /// Stable device id.
51    pub device_id: String,
52    /// Device DID.
53    pub device_did: String,
54    /// Granted scopes.
55    pub scopes: Vec<String>,
56    /// RFC3339 expiry.
57    pub expires_at: String,
58    /// Fresh credential (renew only); never logged.
59    pub credential: Option<String>,
60}
61
62/// A host refusal the device is allowed to see (`code` + public detail).
63#[derive(Clone, Debug, PartialEq, Eq)]
64pub struct Refusal {
65    /// Stable refusal code (e.g. `invalid_credential`).
66    pub code: String,
67    /// Safe-to-forward detail (no secrets).
68    pub message: String,
69}
70
71impl Refusal {
72    /// Build a refusal.
73    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
74        Self {
75            code: code.into(),
76            message: message.into(),
77        }
78    }
79
80    /// Wire form `"code: message"`.
81    pub fn wire(&self) -> String {
82        format!("{}: {}", self.code, self.message)
83    }
84}
85
86/// Why [`crate::serve`] returned.
87#[derive(Debug, thiserror::Error)]
88pub enum SessionEnd {
89    /// Peer closed or the accept loop ended cleanly.
90    #[error("session closed")]
91    Closed,
92    /// First stream was not a successful open/renew.
93    #[error("session not opened: {0}")]
94    NotOpened(String),
95    /// Open deadline elapsed.
96    #[error("open timed out")]
97    OpenTimeout,
98    /// Transport I/O failed.
99    #[error("transport: {0}")]
100    Transport(#[from] std::io::Error),
101}