| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| use crate::{PairingError, RelayEndpoint, parse_peer_id}; | |
| use serde::{Deserialize, Serialize}; | |
| use std::{fmt, str::FromStr}; | |
| /// The canonical State Root identity used by Maka Server enrollment and directory records. | |
| #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] | |
| #[serde(try_from = "String")] | |
| pub struct RegisteredRootId(String); | |
| #[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] | |
| #[error("registered Root ID must consist of 64 lowercase hexadecimal digits")] | |
| pub struct InvalidRegisteredRootId; | |
| impl RegisteredRootId { | |
| pub fn validate(value: &str) -> Result<(), InvalidRegisteredRootId> { | |
| if value.len() == 64 | |
| && value | |
| .bytes() | |
| .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)) | |
| { | |
| Ok(()) | |
| } else { | |
| Err(InvalidRegisteredRootId) | |
| } | |
| } | |
| pub fn as_str(&self) -> &str { | |
| &self.0 | |
| } | |
| } | |
| impl TryFrom<String> for RegisteredRootId { | |
| type Error = InvalidRegisteredRootId; | |
| fn try_from(value: String) -> Result<Self, Self::Error> { | |
| Self::validate(&value)?; | |
| Ok(Self(value)) | |
| } | |
| } | |
| impl From<RegisteredRootId> for String { | |
| fn from(id: RegisteredRootId) -> Self { | |
| id.0 | |
| } | |
| } | |
| impl FromStr for RegisteredRootId { | |
| type Err = InvalidRegisteredRootId; | |
| fn from_str(value: &str) -> Result<Self, Self::Err> { | |
| Self::validate(value)?; | |
| Ok(Self(value.to_owned())) | |
| } | |
| } | |
| impl fmt::Display for RegisteredRootId { | |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| f.write_str(self.as_str()) | |
| } | |
| } | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| #[serde(try_from = "String")] | |
| pub struct ServerPeerId(String); | |
| impl ServerPeerId { | |
| pub fn as_str(&self) -> &str { | |
| &self.0 | |
| } | |
| } | |
| impl fmt::Display for ServerPeerId { | |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| f.write_str(self.as_str()) | |
| } | |
| } | |
| impl TryFrom<String> for ServerPeerId { | |
| type Error = PairingError; | |
| fn try_from(value: String) -> Result<Self, Self::Error> { | |
| parse_peer_id(&value)?; | |
| Ok(Self(value)) | |
| } | |
| } | |
| impl From<ServerPeerId> for String { | |
| fn from(id: ServerPeerId) -> Self { | |
| id.0 | |
| } | |
| } | |
| #[derive(Clone, Debug, Serialize, Deserialize)] | |
| #[serde(rename_all = "camelCase")] | |
| pub struct ServerHostEntry { | |
| pub root_id: RegisteredRootId, | |
| pub peer_id: ServerPeerId, | |
| pub display_name: String, | |
| pub relay_urls: Vec<RelayEndpoint>, | |
| pub capabilities: Vec<String>, | |
| pub last_heartbeat_at_ms: Option<u64>, | |
| pub joined_at_ms: u64, | |
| pub revoked_at_ms: Option<u64>, | |
| pub revision: u64, | |
| pub online: bool, | |
| } | |
| #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| #[serde(rename_all = "lowercase")] | |
| pub enum ServerDeviceRole { | |
| Device, | |
| Control, | |
| } | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| #[serde(rename_all = "camelCase")] | |
| pub struct ServerDeviceEntry { | |
| pub peer_id: ServerPeerId, | |
| pub display_name: String, | |
| pub role: ServerDeviceRole, | |
| pub joined_at_ms: u64, | |
| pub revoked_at_ms: Option<u64>, | |
| pub revision: u64, | |
| #[serde(default, skip_serializing_if = "Option::is_none")] | |
| pub host_root_id: Option<RegisteredRootId>, | |
| } | |
| /// The directory and heartbeat report revoked device IDs as JSON strings. | |
| pub type RevokedPeer = ServerPeerId; | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| #[serde(rename_all = "camelCase")] | |
| pub struct RevokedHost { | |
| pub root_id: RegisteredRootId, | |
| pub peer_id: ServerPeerId, | |
| pub revoked_at_ms: u64, | |
| pub revision: u64, | |
| } | |
| #[derive(Clone, Serialize, Deserialize)] | |
| #[serde(rename_all = "camelCase")] | |
| pub struct ServerDirectorySnapshot { | |
| pub directory_revision: u64, | |
| pub hosts: Vec<ServerHostEntry>, | |
| pub devices: Vec<ServerDeviceEntry>, | |
| pub revoked_peers: Vec<RevokedPeer>, | |
| pub revoked_hosts: Vec<RevokedHost>, | |
| pub relay_auth_token: Option<String>, | |
| } |