gent_protocol/service/
invitations.rs1use serde::{Deserialize, Serialize};
3use uuid::{Uuid, Variant};
4
5pub const LIFETIME_SECONDS: u64 = 900;
6pub const MAX_WIRE_BYTES: usize = 6 * 1024 * 1024 + 4096;
7pub const MAX_ATTEMPTS: usize = 64;
8pub const MAX_MESSAGES_PER_SIDE: u64 = 4096;
9pub const MAX_BYTES_PER_SIDE: u64 = 64 * 1024 * 1024;
10
11pub fn valid_attempt(value: &str) -> bool {
12 Uuid::parse_str(value).is_ok_and(|id| {
13 id.to_string() == value && id.get_version_num() == 4 && id.get_variant() == Variant::RFC4122
14 })
15}
16
17pub fn issued_at(id: &str) -> Option<u64> {
20 let value = Uuid::parse_str(id).ok()?;
21 if value.to_string() != id
22 || value.get_version_num() != 7
23 || value.get_variant() != Variant::RFC4122
24 {
25 return None;
26 }
27 let (seconds, nanos) = value.get_timestamp()?.to_unix();
28 (seconds > 0 && nanos == 0).then_some(seconds)
29}
30
31pub fn credential_created_at(token: &str) -> Option<u64> {
32 super::credential_created_at(token, "gent_j1")
33}
34
35#[derive(Serialize, Deserialize)]
37#[serde(deny_unknown_fields)]
38pub struct Registration {
39 pub owner_node_id: String,
40 pub routing_hash: String,
41 pub expires_at: u64,
42}
43
44#[derive(Serialize, Deserialize)]
47#[serde(deny_unknown_fields)]
48pub struct Exchange {
49 pub routing_secret: String,
50 pub join_token: String,
51}
52
53#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
54#[serde(deny_unknown_fields)]
55pub struct Record {
56 pub id: String,
57 pub cluster_id: String,
58 pub owner_node_id: String,
59 pub expires_at: u64,
60}
61
62#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
63#[serde(rename_all = "snake_case")]
64pub enum Side {
65 Owner,
66 Join,
67}
68
69#[derive(Serialize, Deserialize)]
70#[serde(deny_unknown_fields)]
71pub struct Check {
72 pub invitation_id: String,
73 pub side: Side,
74 pub token_hash: String,
75}
76
77#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
78#[serde(deny_unknown_fields)]
79pub struct Scope {
80 pub invitation_id: String,
81 pub cluster_id: String,
82 pub owner_node_id: String,
83 pub side: Side,
84 pub deadline: u64,
85 pub expires_at: u64,
86}
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 #[test]
93 fn invitation_ids_bind_an_exact_issue_time_and_join_tokens_have_a_distinct_type() {
94 let id = Uuid::new_v7(uuid::Timestamp::from_unix(uuid::NoContext, 1794580000, 0));
95 assert_eq!(issued_at(&id.to_string()), Some(1794580000));
96 for invalid in [
97 id.to_string().to_uppercase(),
98 id.simple().to_string(),
99 Uuid::new_v4().to_string(),
100 Uuid::new_v7(uuid::Timestamp::from_unix(uuid::NoContext, 0, 0)).to_string(),
101 Uuid::new_v7(uuid::Timestamp::from_unix(
102 uuid::NoContext,
103 1794580000,
104 1_000_000,
105 ))
106 .to_string(),
107 ] {
108 assert_eq!(issued_at(&invalid), None);
109 }
110 let token = format!("gent_j1.1794580000.{}", "ab".repeat(32));
111 assert_eq!(credential_created_at(&token), Some(1794580000));
112 assert_eq!(super::super::transport_created_at(&token), None);
113 assert_eq!(super::super::session_created_at(&token), None);
114 assert_eq!(
115 credential_created_at(&token.replace("gent_j1", "gent_t1")),
116 None
117 );
118 }
119}