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