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/// Verified (or renewed) session identity returned by the host bus.
37#[derive(Clone, Debug, PartialEq, Eq)]
38pub struct Session {
39    /// Stable device id.
40    pub device_id: String,
41    /// Device DID.
42    pub device_did: String,
43    /// Granted scopes.
44    pub scopes: Vec<String>,
45    /// RFC3339 expiry.
46    pub expires_at: String,
47    /// Fresh credential (renew only); never logged.
48    pub credential: Option<String>,
49}
50
51/// A host refusal the device is allowed to see (`code` + public detail).
52#[derive(Clone, Debug, PartialEq, Eq)]
53pub struct Refusal {
54    /// Stable refusal code (e.g. `invalid_credential`).
55    pub code: String,
56    /// Safe-to-forward detail (no secrets).
57    pub message: String,
58}
59
60impl Refusal {
61    /// Build a refusal.
62    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
63        Self {
64            code: code.into(),
65            message: message.into(),
66        }
67    }
68
69    /// Wire form `"code: message"`.
70    pub fn wire(&self) -> String {
71        format!("{}: {}", self.code, self.message)
72    }
73}
74
75/// Why [`crate::serve`] returned.
76#[derive(Debug, thiserror::Error)]
77pub enum SessionEnd {
78    /// Peer closed or the accept loop ended cleanly.
79    #[error("session closed")]
80    Closed,
81    /// First stream was not a successful open/renew.
82    #[error("session not opened: {0}")]
83    NotOpened(String),
84    /// Open deadline elapsed.
85    #[error("open timed out")]
86    OpenTimeout,
87    /// Transport I/O failed.
88    #[error("transport: {0}")]
89    Transport(#[from] std::io::Error),
90}