node-device-session 7.7.0

Shared server for /econ-v1/device-session/1 behind transport-neutral traits (C-5)
Documentation
//! Public types for [`crate::serve`].

use std::path::PathBuf;

/// Provenance header stamped on every attested upstream request.
pub const SESSION_HEADER: &str = "x-node-device-session";
/// Path header stamped on every attested upstream request.
pub const PATH_HEADER: &str = "x-node-device-session-path";

/// How the session reached the node (stamped on the ingress).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SessionPath {
    /// Relayed QUIC / p2p path.
    Relay,
    /// WebRTC data-channel path.
    WebRtc,
}

impl SessionPath {
    /// Wire value for `x-node-device-session-path`.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Relay => "relay",
            Self::WebRtc => "webrtc",
        }
    }
}

/// Host-owned Unix socket that alone may attest device-session provenance.
#[derive(Clone, Debug, Default)]
pub struct IngressSocket {
    /// Absolute path to the permissioned Unix listener, if bound.
    pub path: Option<PathBuf>,
}

/// Node wire socket that `wire/1` streams splice onto (ADR-310).
///
/// Carriers pass a configured path (for example from `NODE_WIRE_SOCKET`).
/// When [`Self::path`] is [`None`], `wire/1` is refused and existing HTTP/WS
/// behaviour is unchanged.
#[derive(Clone, Debug, Default)]
pub struct WireSocket {
    /// Absolute path to the node's trusted wire listener, if configured.
    pub path: Option<PathBuf>,
}

/// Verified (or renewed) session identity returned by the host bus.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Session {
    /// Stable device id.
    pub device_id: String,
    /// Device DID.
    pub device_did: String,
    /// Granted scopes.
    pub scopes: Vec<String>,
    /// RFC3339 expiry.
    pub expires_at: String,
    /// Fresh credential (renew only); never logged.
    pub credential: Option<String>,
}

/// A host refusal the device is allowed to see (`code` + public detail).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Refusal {
    /// Stable refusal code (e.g. `invalid_credential`).
    pub code: String,
    /// Safe-to-forward detail (no secrets).
    pub message: String,
}

impl Refusal {
    /// Build a refusal.
    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            code: code.into(),
            message: message.into(),
        }
    }

    /// Wire form `"code: message"`.
    pub fn wire(&self) -> String {
        format!("{}: {}", self.code, self.message)
    }
}

/// Why [`crate::serve`] returned.
#[derive(Debug, thiserror::Error)]
pub enum SessionEnd {
    /// Peer closed or the accept loop ended cleanly.
    #[error("session closed")]
    Closed,
    /// First stream was not a successful open/renew.
    #[error("session not opened: {0}")]
    NotOpened(String),
    /// Open deadline elapsed.
    #[error("open timed out")]
    OpenTimeout,
    /// Transport I/O failed.
    #[error("transport: {0}")]
    Transport(#[from] std::io::Error),
}