Skip to main content

comindly_api/
server.rs

1use serde::{Deserialize, Serialize};
2
3/// A way to sign in: an emailed link, a code, or an OpenID Connect provider; a server offers one or
4/// more, each for invited people only.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
6#[serde(rename_all = "lowercase")]
7pub enum SignIn {
8    Email,
9    Code,
10    Oidc,
11}
12
13/// What someone may do, each role including the ones below it.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
15#[serde(rename_all = "lowercase")]
16pub enum Role {
17    /// Members, repositories, settings, secrets and indexing.
18    Admin,
19    /// Writes to the ledger and conventions.
20    Member,
21    /// Reads.
22    Viewer,
23}
24
25impl Role {
26    pub fn as_str(self) -> &'static str {
27        match self {
28            Role::Admin => "admin",
29            Role::Member => "member",
30            Role::Viewer => "viewer",
31        }
32    }
33
34    fn rank(self) -> u8 {
35        match self {
36            Role::Admin => 2,
37            Role::Member => 1,
38            Role::Viewer => 0,
39        }
40    }
41
42    /// This role may do what `other` may.
43    pub fn at_least(self, other: Role) -> bool {
44        self.rank() >= other.rank()
45    }
46}
47
48/// How a request proved who it is.
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub enum Via {
51    Session,
52    AdminToken,
53    /// A personal access token: enough for agents, not for managing the account.
54    Token,
55}
56
57#[derive(Debug, Serialize, Deserialize)]
58pub struct Member {
59    pub email: String,
60    pub role: Role,
61    pub fixed: bool,
62    pub created: String,
63    pub last_seen: Option<String>,
64    /// A service account: no person, no sign-in, only tokens an admin creates.
65    #[serde(default)]
66    pub service: bool,
67}
68
69/// Service accounts are members at this reserved domain, which no mailbox or identity provider has.
70pub const SERVICE_DOMAIN: &str = "service.invalid";
71
72/// The member address of service account `name`: lowercase letters, digits and dashes, 1-40.
73pub fn service_email(name: &str) -> Option<String> {
74    let name = name.trim();
75    let ok = (1..=40).contains(&name.len())
76        && name
77            .chars()
78            .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
79        && !name.starts_with('-')
80        && !name.ends_with('-');
81    ok.then(|| format!("{name}@{SERVICE_DOMAIN}"))
82}
83
84/// The service account's name, when `email` is one.
85pub fn service_name(email: &str) -> Option<&str> {
86    email
87        .strip_suffix(SERVICE_DOMAIN)
88        .and_then(|n| n.strip_suffix('@'))
89        .filter(|n| !n.is_empty())
90}
91
92/// `POST /api/v1/members/service`: create a service account.
93#[derive(Debug, Clone, Serialize, Deserialize)]
94pub struct NewService {
95    pub name: String,
96    pub role: Role,
97}
98
99/// `POST /api/v1/members/service/token`: a token for a service account.
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct ServiceTokenRequest {
102    pub name: String,
103    pub token_name: String,
104    /// Days until it expires; `None` never.
105    #[serde(default)]
106    pub expires_in_days: Option<u32>,
107    /// Addresses (`203.0.113.7`) or ranges (`203.0.113.0/24`) it works from; empty is anywhere.
108    #[serde(default)]
109    pub allowed: Vec<String>,
110}
111
112/// `POST /api/v1/members/tokens/revoke`: an admin revokes a member's token, or all with no `id`.
113#[derive(Debug, Clone, Serialize, Deserialize)]
114pub struct MemberTokenRevoke {
115    pub email: String,
116    #[serde(default)]
117    pub id: Option<String>,
118}
119
120#[derive(Debug, Serialize, Deserialize)]
121pub struct Invite {
122    pub email: String,
123    pub role: Role,
124    pub created_by: String,
125    pub expires: String,
126}
127
128#[derive(Debug, Serialize, Deserialize)]
129pub struct AccessToken {
130    pub id: String,
131    pub name: String,
132    pub created: String,
133    pub last_used: Option<String>,
134    /// `None` for a token that never expires.
135    pub expires: Option<String>,
136    pub expired: bool,
137    /// Addresses or ranges it works from; empty is anywhere.
138    #[serde(default, skip_serializing_if = "Vec::is_empty")]
139    pub allowed: Vec<String>,
140}
141
142/// One row of the audit log; details never hold token values or codes.
143#[derive(Debug, Clone, Serialize, Deserialize)]
144pub struct AuditEntry {
145    pub at: String,
146    pub actor: String,
147    pub action: String,
148    pub detail: String,
149}
150
151/// Which audit entries to list: `before` is the `next` cursor of the previous page.
152#[derive(Debug, Default, Serialize, Deserialize)]
153pub struct AuditQuery {
154    pub limit: Option<usize>,
155    pub before: Option<String>,
156    pub actor: Option<String>,
157    pub action: Option<String>,
158}
159
160#[derive(Debug, Serialize, Deserialize)]
161pub struct AuditPage {
162    pub entries: Vec<AuditEntry>,
163    /// The cursor for the next (older) page, when there is one.
164    pub next: Option<String>,
165}
166
167/// A secret's state for the UI; its value never leaves the server.
168#[derive(Debug, Serialize, Deserialize)]
169pub struct SecretInfo {
170    pub name: String,
171    pub source: String,
172    pub hint: Option<String>,
173    pub updated: Option<String>,
174    pub updated_by: Option<String>,
175}
176
177/// What a member sees before approving a command-line sign-in.
178#[derive(Debug, Clone, Serialize, Deserialize)]
179pub struct DeviceRequest {
180    pub client: String,
181    pub expires: String,
182}
183
184/// Where a command-line sign-in stands.
185#[derive(Debug, Clone, PartialEq, Eq)]
186pub enum DevicePoll {
187    Pending,
188    Approved {
189        token: String,
190        email: String,
191        expires: Option<String>,
192    },
193    Denied,
194    Expired,
195}
196
197/// How long a token lasts unless chosen otherwise, and a command-line sign-in's always.
198pub const TOKEN_DAYS: u32 = 90;