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)]
46#[serde(deny_unknown_fields)]
47pub(crate) struct PairRequest {
48 pub token: String,
49 pub endpoint_id: String,
50}
51
52#[derive(Clone, Serialize)]
54pub(crate) struct PairOk {
55 pub mode: &'static str,
56 pub expires_at_ms: i64,
57}
58
59#[derive(Clone, PartialEq, Serialize, Deserialize)]
60pub(crate) struct RenewOk {
61 pub credential: String,
62 pub expires_at: String,
63 pub scopes: Vec<String>,
64 pub device_id: String,
65 pub device_did: String,
66 #[serde(default, skip_serializing_if = "Option::is_none")]
67 pub revocation_id: Option<String>,
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize)]
71pub(crate) struct ServiceCall {
72 pub service: String,
73 #[serde(default)]
74 pub payload: Value,
75}
76
77#[derive(Debug, PartialEq, Eq)]
78pub(crate) struct Service {
79 pub name: &'static str,
80 pub capability: &'static str,
81 pub scope: &'static str,
82}
83
84pub(crate) const SERVICES: &[Service] = &[Service {
85 name: "agent",
86 capability: "agent.device.receive",
87 scope: "agent.device",
88}];
89
90pub(crate) fn service(name: &str) -> Option<&'static Service> {
91 SERVICES.iter().find(|s| s.name == name)
92}
93
94pub(crate) fn scope_allows(scopes: &[String], required: &str) -> bool {
95 scopes.iter().any(|scope| {
96 scope == required
97 || scope
98 .strip_suffix(".*")
99 .is_some_and(|prefix| required.starts_with(&format!("{prefix}.")))
100 })
101}
102
103pub(crate) fn endpoint_hex(key: &[u8; 32]) -> String {
104 key.iter().map(|b| format!("{b:02x}")).collect()
105}
106
107pub(crate) struct StampIdentity<'a> {
108 pub device_id: &'a str,
109 pub device_did: &'a str,
110 pub scopes: &'a [String],
111 pub endpoint_hex: &'a str,
112}
113
114pub(crate) fn stamp(payload: Value, id: &StampIdentity<'_>) -> Value {
115 let mut map = match payload {
116 Value::Object(map) => map,
117 other => {
118 let mut m = Map::new();
119 m.insert("payload".into(), other);
120 m
121 }
122 };
123 map.insert("device_id".into(), Value::String(id.device_id.into()));
124 map.insert("device_did".into(), Value::String(id.device_did.into()));
125 map.insert(
126 "device_scopes".into(),
127 Value::Array(id.scopes.iter().cloned().map(Value::String).collect()),
128 );
129 map.insert(
130 "device_endpoint".into(),
131 Value::String(id.endpoint_hex.into()),
132 );
133 map.insert("sig_verified".into(), Value::Bool(false));
134 map.insert("caller".into(), Value::String("device".into()));
135 Value::Object(map)
136}