Skip to main content

node_device_session/
messages.rs

1//! Session open/renew message shapes and device.call service table.
2
3use serde::{Deserialize, Serialize};
4use serde_json::{Map, Value};
5
6#[derive(Clone, Serialize, Deserialize)]
7pub(crate) struct OpenRequest {
8    pub credential: Option<String>,
9    #[serde(default)]
10    pub mode: Option<OpenMode>,
11}
12
13/// Session mode after a successful open.
14#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
15#[serde(rename_all = "snake_case")]
16pub enum OpenMode {
17    /// Full attested session (credential verified).
18    #[default]
19    Full,
20    /// Renewal-only session (`http.request/1` to registration paths).
21    Renewal,
22}
23
24/// Proven session identity after a successful `session.open/1`.
25#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
26pub struct OpenOk {
27    /// Full or renewal.
28    #[serde(default)]
29    pub mode: OpenMode,
30    /// Stable device id.
31    pub device_id: String,
32    /// Device DID.
33    pub device_did: String,
34    /// Granted scopes (empty in renewal mode).
35    pub scopes: Vec<String>,
36    /// Expiry (RFC3339 or epoch seconds for renewal).
37    pub expires_at: String,
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize)]
41#[serde(deny_unknown_fields)]
42pub(crate) struct RenewRequest {}
43
44/// `session.pair/1` body. No `Debug`: the token is a secret.
45///
46/// `token` is the owner's one-time door token. Without it the browser asks
47/// for an open pairing session: the node applies its ordinary pairing policy
48/// to the pairing sent through it (ADR-399 Amendment 2).
49#[derive(Clone, Deserialize)]
50#[serde(deny_unknown_fields)]
51pub(crate) struct PairRequest {
52    #[serde(default)]
53    pub token: Option<String>,
54    pub endpoint_id: String,
55}
56
57/// `session.pair/1` answer. Carries neither the token nor the member.
58#[derive(Clone, Serialize)]
59pub(crate) struct PairOk {
60    pub mode: &'static str,
61    pub expires_at_ms: i64,
62}
63
64#[derive(Clone, PartialEq, Serialize, Deserialize)]
65pub(crate) struct RenewOk {
66    pub credential: String,
67    pub expires_at: String,
68    pub scopes: Vec<String>,
69    pub device_id: String,
70    pub device_did: String,
71    #[serde(default, skip_serializing_if = "Option::is_none")]
72    pub revocation_id: Option<String>,
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize)]
76pub(crate) struct ServiceCall {
77    pub service: String,
78    #[serde(default)]
79    pub payload: Value,
80}
81
82#[derive(Debug, PartialEq, Eq)]
83pub(crate) struct Service {
84    pub name: &'static str,
85    pub capability: &'static str,
86    pub scope: &'static str,
87}
88
89pub(crate) const SERVICES: &[Service] = &[Service {
90    name: "agent",
91    capability: "agent.device.receive",
92    scope: "agent.device",
93}];
94
95pub(crate) fn service(name: &str) -> Option<&'static Service> {
96    SERVICES.iter().find(|s| s.name == name)
97}
98
99pub(crate) fn scope_allows(scopes: &[String], required: &str) -> bool {
100    scopes.iter().any(|scope| {
101        scope == required
102            || scope
103                .strip_suffix(".*")
104                .is_some_and(|prefix| required.starts_with(&format!("{prefix}.")))
105    })
106}
107
108pub(crate) fn endpoint_hex(key: &[u8; 32]) -> String {
109    key.iter().map(|b| format!("{b:02x}")).collect()
110}
111
112pub(crate) struct StampIdentity<'a> {
113    pub device_id: &'a str,
114    pub device_did: &'a str,
115    pub scopes: &'a [String],
116    pub endpoint_hex: &'a str,
117}
118
119pub(crate) fn stamp(payload: Value, id: &StampIdentity<'_>) -> Value {
120    let mut map = match payload {
121        Value::Object(map) => map,
122        other => {
123            let mut m = Map::new();
124            m.insert("payload".into(), other);
125            m
126        }
127    };
128    map.insert("device_id".into(), Value::String(id.device_id.into()));
129    map.insert("device_did".into(), Value::String(id.device_did.into()));
130    map.insert(
131        "device_scopes".into(),
132        Value::Array(id.scopes.iter().cloned().map(Value::String).collect()),
133    );
134    map.insert(
135        "device_endpoint".into(),
136        Value::String(id.endpoint_hex.into()),
137    );
138    map.insert("sig_verified".into(), Value::Bool(false));
139    map.insert("caller".into(), Value::String("device".into()));
140    Value::Object(map)
141}