node_device_session/
messages.rs1use 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#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
15#[serde(rename_all = "snake_case")]
16pub enum OpenMode {
17 #[default]
19 Full,
20 Renewal,
22}
23
24#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
26pub struct OpenOk {
27 #[serde(default)]
29 pub mode: OpenMode,
30 pub device_id: String,
32 pub device_did: String,
34 pub scopes: Vec<String>,
36 pub expires_at: String,
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize)]
41#[serde(deny_unknown_fields)]
42pub(crate) struct RenewRequest {}
43
44#[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#[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}