1use serde::{Deserialize, Serialize};
2
3#[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#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
15#[serde(rename_all = "lowercase")]
16pub enum Role {
17 Admin,
19 Member,
21 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 pub fn at_least(self, other: Role) -> bool {
44 self.rank() >= other.rank()
45 }
46}
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub enum Via {
51 Session,
52 AdminToken,
53 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 #[serde(default)]
66 pub service: bool,
67}
68
69pub const SERVICE_DOMAIN: &str = "service.invalid";
71
72pub 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
84pub 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#[derive(Debug, Clone, Serialize, Deserialize)]
94pub struct NewService {
95 pub name: String,
96 pub role: Role,
97}
98
99#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct ServiceTokenRequest {
102 pub name: String,
103 pub token_name: String,
104 #[serde(default)]
106 pub expires_in_days: Option<u32>,
107 #[serde(default)]
109 pub allowed: Vec<String>,
110}
111
112#[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 pub expires: Option<String>,
136 pub expired: bool,
137 #[serde(default, skip_serializing_if = "Vec::is_empty")]
139 pub allowed: Vec<String>,
140}
141
142#[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#[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 pub next: Option<String>,
165}
166
167#[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#[derive(Debug, Clone, Serialize, Deserialize)]
179pub struct DeviceRequest {
180 pub client: String,
181 pub expires: String,
182}
183
184#[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
197pub const TOKEN_DAYS: u32 = 90;