Skip to main content

cloudillo_types/
auth_adapter.rs

1// SPDX-FileCopyrightText: Szilárd Hajba
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4//! Adapter that manages and stores authentication, authorization and other sensitive data.
5
6use async_trait::async_trait;
7use serde::{Deserialize, Serialize};
8use serde_with::skip_serializing_none;
9use std::fmt::Debug;
10
11use std::collections::HashMap;
12
13use crate::{
14	action_types,
15	prelude::*,
16	types::{serialize_timestamp_iso, serialize_timestamp_iso_opt},
17};
18
19pub const ACCESS_TOKEN_EXPIRY: i64 = 3600;
20
21/// Action tokens represent federated user actions as signed JWTs (ES384/P-384).
22///
23/// Actions are content-addressed: `action_id = "a1~" + SHA256(token)`.
24/// Field names are short (JWT claims) to minimize token size.
25#[skip_serializing_none]
26#[derive(Debug, Clone, Default, Deserialize, Serialize)]
27pub struct ActionToken {
28	/// Issuer - id_tag of the action creator (e.g., "alice.example.com")
29	pub iss: Box<str>,
30
31	/// Key ID - identifier of the signing key used (for key rotation support)
32	pub k: Box<str>,
33
34	/// Type - action type with optional subtype (e.g., "POST", "REACT:LIKE", "CONN:DEL")
35	pub t: Box<str>,
36
37	/// Content - action-specific payload as JSON.
38	pub c: Option<serde_json::Value>,
39
40	/// Parent - action_id of parent action for TRUE HIERARCHY (threading).
41	pub p: Option<Box<str>>,
42
43	/// Attachments - array of file IDs (content-addressed, e.g., "f1~abc123...")
44	pub a: Option<Vec<Box<str>>>,
45
46	/// Audience - id_tag of the target recipient.
47	pub aud: Option<Box<str>>,
48
49	/// Subject - action_id or resource_id being referenced WITHOUT creating hierarchy.
50	pub sub: Option<Box<str>>,
51
52	/// Issued At - Unix timestamp of action creation
53	pub iat: Timestamp,
54
55	/// Expires At - optional Unix timestamp for action expiration
56	pub exp: Option<Timestamp>,
57
58	/// Flags - capability flags for this action
59	pub f: Option<Box<str>>,
60
61	/// Visibility - P=Public, V=Verified, 2=2ndDegree, F=Follower, C=Connected, None=Direct
62	pub v: Option<char>,
63
64	/// Nonce - Proof-of-work nonce for rate limiting (CONN actions only).
65	#[serde(rename = "_", default, skip_serializing_if = "Option::is_none")]
66	pub nonce: Option<Box<str>>,
67}
68
69/// Access tokens are used to authenticate users
70#[skip_serializing_none]
71#[derive(Clone, Debug, Deserialize, Serialize)]
72pub struct AccessToken<S> {
73	pub iss: S,
74	pub sub: Option<S>,
75	pub scope: Option<S>,
76	pub r: Option<S>,
77	pub exp: Timestamp,
78}
79
80/// Represents a profile key
81#[skip_serializing_none]
82#[derive(Debug, Clone, Deserialize, Serialize)]
83pub struct AuthKey {
84	#[serde(rename = "keyId")]
85	pub key_id: Box<str>,
86	#[serde(rename = "publicKey")]
87	pub public_key: Box<str>,
88	#[serde(rename = "expiresAt", serialize_with = "serialize_timestamp_iso_opt")]
89	pub expires_at: Option<Timestamp>,
90}
91
92/// Represents an auth profile.
93///
94/// Adapter-internal: not serialized to clients (handlers project this into
95/// separate wire types). The `Serialize`/`Deserialize` derives are kept for
96/// adapter ergonomics only, so adding `tn_id` does not change any wire shape.
97#[skip_serializing_none]
98#[derive(Debug, Deserialize, Serialize)]
99pub struct AuthProfile {
100	pub tn_id: TnId,
101	pub id_tag: Box<str>,
102	pub email: Option<Box<str>>,
103	pub roles: Option<Box<[Box<str>]>>,
104	/// Tenant status — typically `'A'` (Active) or `'S'` (Suspended).
105	pub status: Option<Box<str>>,
106	pub keys: Vec<AuthKey>,
107}
108
109/// Context struct for an authenticated user
110#[derive(Clone, Debug)]
111pub struct AuthCtx {
112	pub tn_id: TnId,
113	pub id_tag: Box<str>,
114	pub roles: Box<[Box<str>]>,
115	pub scope: Option<Box<str>>,
116}
117
118#[derive(Debug)]
119pub struct AuthLogin {
120	pub tn_id: TnId,
121	pub id_tag: Box<str>,
122	pub roles: Option<Box<[Box<str>]>>,
123	pub token: Box<str>,
124}
125
126/// A private/public key pair
127#[derive(Debug)]
128pub struct KeyPair {
129	pub private_key: Box<str>,
130	pub public_key: Box<str>,
131}
132
133#[derive(Debug)]
134pub struct Webauthn<'a> {
135	pub credential_id: &'a str,
136	pub counter: u32,
137	pub public_key: &'a str,
138	pub description: Option<&'a str>,
139}
140
141/// Data needed to create a new tenant
142#[derive(Debug)]
143pub struct CreateTenantData<'a> {
144	pub vfy_code: Option<&'a str>,
145	pub email: Option<&'a str>,
146	pub password: Option<&'a str>,
147	pub roles: Option<&'a [&'a str]>,
148}
149
150/// Tenant list item from auth adapter
151#[skip_serializing_none]
152#[derive(Debug, Clone, Deserialize, Serialize)]
153#[serde(rename_all = "camelCase")]
154pub struct TenantListItem {
155	pub tn_id: TnId,
156	pub id_tag: Box<str>,
157	pub email: Option<Box<str>>,
158	pub roles: Option<Box<[Box<str>]>>,
159	pub status: Option<Box<str>>,
160	#[serde(serialize_with = "serialize_timestamp_iso")]
161	pub created_at: Timestamp,
162}
163
164/// Options for listing tenants
165#[derive(Debug, Default)]
166pub struct ListTenantsOptions<'a> {
167	pub status: Option<&'a str>,
168	pub q: Option<&'a str>,
169	pub limit: Option<u32>,
170	pub offset: Option<u32>,
171}
172
173/// Certificate associated with a tenant
174#[derive(Debug)]
175pub struct CertData {
176	pub tn_id: TnId,
177	pub id_tag: Box<str>,
178	pub domain: Box<str>,
179	pub cert: Box<str>,
180	pub key: Box<str>,
181	pub expires_at: Timestamp,
182	pub last_renewal_attempt_at: Option<Timestamp>,
183	pub last_renewal_error: Option<Box<str>>,
184	pub failure_count: u32,
185	pub notified_at: Option<Timestamp>,
186}
187
188/// Row returned by `list_tenants_needing_cert_renewal`. Includes failure-tracking
189/// state so the renewal task can decide on notifications and tenant suspension
190/// without an extra read per tenant.
191#[derive(Debug)]
192pub struct TenantCertRenewalRow {
193	pub tn_id: TnId,
194	pub id_tag: Box<str>,
195	/// `None` ⇒ tenant has no cert yet (initial bootstrap)
196	pub expires_at: Option<Timestamp>,
197	pub failure_count: u32,
198	pub last_renewal_error: Option<Box<str>>,
199	pub notified_at: Option<Timestamp>,
200}
201
202/// API key information (without the secret key)
203#[skip_serializing_none]
204#[derive(Debug, Clone, Deserialize, Serialize)]
205#[serde(rename_all = "camelCase")]
206pub struct ApiKeyInfo {
207	pub key_id: i64,
208	pub key_prefix: Box<str>,
209	pub name: Option<Box<str>>,
210	pub scopes: Option<Box<str>>,
211	#[serde(serialize_with = "serialize_timestamp_iso_opt")]
212	pub expires_at: Option<Timestamp>,
213	#[serde(serialize_with = "serialize_timestamp_iso_opt")]
214	pub last_used_at: Option<Timestamp>,
215	#[serde(serialize_with = "serialize_timestamp_iso")]
216	pub created_at: Timestamp,
217}
218
219/// Options for creating an API key
220#[derive(Debug)]
221pub struct CreateApiKeyOptions<'a> {
222	pub name: Option<&'a str>,
223	pub scopes: Option<&'a str>,
224	pub expires_at: Option<Timestamp>,
225}
226
227/// Result of creating an API key (includes plaintext key shown only once)
228#[derive(Debug)]
229pub struct CreatedApiKey {
230	pub info: ApiKeyInfo,
231	pub plaintext_key: Box<str>,
232}
233
234/// Result of validating an API key
235#[derive(Debug)]
236pub struct ApiKeyValidation {
237	pub tn_id: TnId,
238	pub id_tag: Box<str>,
239	pub key_id: i64,
240	pub scopes: Option<Box<str>>,
241	pub roles: Option<Box<str>>,
242}
243
244// Proxy site types
245// =================
246
247/// Configuration for a proxy site (stored as JSON in the config column)
248#[skip_serializing_none]
249#[derive(Debug, Clone, Default, Serialize, Deserialize)]
250#[serde(rename_all = "camelCase")]
251pub struct ProxySiteConfig {
252	pub connect_timeout_secs: Option<u32>,
253	pub read_timeout_secs: Option<u32>,
254	pub preserve_host: Option<bool>,
255	pub proxy_protocol: Option<bool>,
256	pub custom_headers: Option<HashMap<String, String>>,
257	pub forward_headers: Option<bool>,
258	pub websocket: Option<bool>,
259}
260
261/// Proxy site data from the database
262#[skip_serializing_none]
263#[derive(Debug, Clone, Serialize, Deserialize)]
264#[serde(rename_all = "camelCase")]
265pub struct ProxySiteData {
266	pub site_id: i64,
267	pub domain: Box<str>,
268	pub backend_url: Box<str>,
269	pub status: Box<str>,
270	#[serde(rename = "type")]
271	pub proxy_type: Box<str>,
272	#[serde(skip_serializing)]
273	pub cert: Option<Box<str>>,
274	#[serde(skip_serializing)]
275	pub cert_key: Option<Box<str>>,
276	#[serde(serialize_with = "serialize_timestamp_iso_opt")]
277	pub cert_expires_at: Option<Timestamp>,
278	pub config: ProxySiteConfig,
279	pub created_by: Option<i64>,
280	#[serde(serialize_with = "serialize_timestamp_iso")]
281	pub created_at: Timestamp,
282	#[serde(serialize_with = "serialize_timestamp_iso")]
283	pub updated_at: Timestamp,
284}
285
286/// Data needed to create a new proxy site
287#[derive(Debug)]
288pub struct CreateProxySiteData<'a> {
289	pub domain: &'a str,
290	pub backend_url: &'a str,
291	pub proxy_type: &'a str,
292	pub config: &'a ProxySiteConfig,
293	pub created_by: Option<i64>,
294}
295
296/// Data to update an existing proxy site
297#[derive(Debug)]
298pub struct UpdateProxySiteData<'a> {
299	pub backend_url: Option<&'a str>,
300	pub status: Option<&'a str>,
301	pub proxy_type: Option<&'a str>,
302	pub config: Option<&'a ProxySiteConfig>,
303}
304
305/// A `Cloudillo` auth adapter
306///
307/// Every `AuthAdapter` implementation is required to implement this trait.
308/// An `AuthAdapter` is responsible for storing and managing all sensitive data used for
309/// authentication and authorization.
310#[async_trait]
311pub trait AuthAdapter: Debug + Send + Sync {
312	/// Validates an access token and returns the user context
313	async fn validate_access_token(&self, tn_id: TnId, token: &str) -> ClResult<AuthCtx>;
314
315	/// # Profiles
316	/// Reads the ID tag of the given tenant, referenced by its ID
317	async fn read_id_tag(&self, tn_id: TnId) -> ClResult<Box<str>>;
318
319	/// Reads the ID  the given tenant, referenced by its ID tag
320	async fn read_tn_id(&self, id_tag: &str) -> ClResult<TnId>;
321
322	/// Reads a tenant profile
323	async fn read_tenant(&self, id_tag: &str) -> ClResult<AuthProfile>;
324
325	/// Creates a tenant registration
326	async fn create_tenant_registration(&self, email: &str) -> ClResult<()>;
327
328	/// Creates a new tenant
329	async fn create_tenant(&self, id_tag: &str, data: CreateTenantData<'_>) -> ClResult<TnId>;
330
331	/// Deletes a tenant
332	async fn delete_tenant(&self, id_tag: &str) -> ClResult<()>;
333
334	/// Lists all tenants (for admin use)
335	async fn list_tenants(&self, opts: &ListTenantsOptions<'_>) -> ClResult<Vec<TenantListItem>>;
336
337	/// Returns the total number of tenants matching the filter (ignoring limit/offset)
338	async fn count_tenants(&self, opts: &ListTenantsOptions<'_>) -> ClResult<usize>;
339
340	// Password management
341	async fn create_tenant_login(&self, id_tag: &str) -> ClResult<AuthLogin>;
342	async fn check_tenant_password(&self, id_tag: &str, password: &str) -> ClResult<AuthLogin>;
343	async fn update_tenant_password(&self, id_tag: &str, password: &str) -> ClResult<()>;
344
345	// IDP API key management
346	async fn update_idp_api_key(&self, id_tag: &str, api_key: &str) -> ClResult<()>;
347
348	// Certificate management
349	async fn create_cert(&self, cert_data: &CertData) -> ClResult<()>;
350	async fn read_cert_by_tn_id(&self, tn_id: TnId) -> ClResult<CertData>;
351	async fn read_cert_by_id_tag(&self, id_tag: &str) -> ClResult<CertData>;
352	async fn read_cert_by_domain(&self, domain: &str) -> ClResult<CertData>;
353	async fn list_all_certs(&self) -> ClResult<Vec<CertData>>;
354	async fn list_tenants_needing_cert_renewal(
355		&self,
356		renewal_days: u32,
357	) -> ClResult<Vec<TenantCertRenewalRow>>;
358
359	/// Record an ACME renewal failure for the given tenant: increments
360	/// `failure_count`, sets `last_renewal_error`, and stamps
361	/// `last_renewal_attempt_at`. No-op if no cert row exists for the tenant.
362	async fn record_cert_renewal_failure(&self, tn_id: TnId, error: &str) -> ClResult<()>;
363
364	/// Record a successful ACME renewal: clears `last_renewal_error`,
365	/// resets `failure_count` to 0, clears `notified_at`.
366	async fn record_cert_renewal_success(&self, tn_id: TnId) -> ClResult<()>;
367
368	/// Stamp `notified_at` after sending a renewal-failure notification email.
369	async fn record_cert_renewal_notification(&self, tn_id: TnId) -> ClResult<()>;
370
371	/// Update tenant status. Known statuses:
372	/// `'A'` = Active, `'S'` = Suspended (cert-related, set/cleared automatically
373	/// by the ACME renewal task when an expired cert keeps failing renewal),
374	/// `'X'` = Purging (soft-deleted; admin force-purge has begun. Auth is
375	/// blocked and a retry of the purge endpoint resumes destructive cleanup).
376	async fn update_tenant_status(&self, tn_id: TnId, status: char) -> ClResult<()>;
377
378	// Key management
379	async fn list_profile_keys(&self, tn_id: TnId) -> ClResult<Vec<AuthKey>>;
380	async fn read_profile_key(&self, tn_id: TnId, key_id: &str) -> ClResult<AuthKey>;
381	async fn create_profile_key(
382		&self,
383		tn_id: TnId,
384		expires_at: Option<Timestamp>,
385	) -> ClResult<AuthKey>;
386
387	async fn create_access_token(
388		&self,
389		tn_id: TnId,
390		data: &AccessToken<&str>,
391	) -> ClResult<Box<str>>;
392	async fn create_action_token(
393		&self,
394		tn_id: TnId,
395		data: action_types::CreateAction,
396	) -> ClResult<Box<str>>;
397	async fn verify_access_token(&self, token: &str) -> ClResult<()>;
398
399	// Vapid keys
400	async fn read_vapid_key(&self, tn_id: TnId) -> ClResult<KeyPair>;
401	async fn read_vapid_public_key(&self, tn_id: TnId) -> ClResult<Box<str>>;
402	async fn create_vapid_key(&self, tn_id: TnId) -> ClResult<KeyPair>;
403	async fn update_vapid_key(&self, tn_id: TnId, key: &KeyPair) -> ClResult<()>;
404
405	// Variables
406	async fn read_var(&self, tn_id: TnId, var: &str) -> ClResult<Box<str>>;
407	async fn update_var(&self, tn_id: TnId, var: &str, value: &str) -> ClResult<()>;
408
409	// Webauthn
410	async fn list_webauthn_credentials(&self, tn_id: TnId) -> ClResult<Box<[Webauthn]>>;
411	async fn read_webauthn_credential(
412		&self,
413		tn_id: TnId,
414		credential_id: &str,
415	) -> ClResult<Webauthn>;
416	async fn create_webauthn_credential(&self, tn_id: TnId, data: &Webauthn) -> ClResult<()>;
417	async fn update_webauthn_credential_counter(
418		&self,
419		tn_id: TnId,
420		credential_id: &str,
421		counter: u32,
422	) -> ClResult<()>;
423	async fn delete_webauthn_credential(&self, tn_id: TnId, credential_id: &str) -> ClResult<()>;
424
425	// API Key management
426	async fn create_api_key(
427		&self,
428		tn_id: TnId,
429		opts: CreateApiKeyOptions<'_>,
430	) -> ClResult<CreatedApiKey>;
431	async fn validate_api_key(&self, key: &str) -> ClResult<ApiKeyValidation>;
432	async fn list_api_keys(&self, tn_id: TnId) -> ClResult<Vec<ApiKeyInfo>>;
433	async fn read_api_key(&self, tn_id: TnId, key_id: i64) -> ClResult<ApiKeyInfo>;
434	async fn update_api_key(
435		&self,
436		tn_id: TnId,
437		key_id: i64,
438		name: Option<&str>,
439		scopes: Option<&str>,
440		expires_at: Option<Timestamp>,
441	) -> ClResult<ApiKeyInfo>;
442	async fn delete_api_key(&self, tn_id: TnId, key_id: i64) -> ClResult<()>;
443	async fn cleanup_expired_api_keys(&self) -> ClResult<u32>;
444	async fn cleanup_expired_verification_codes(&self) -> ClResult<u32>;
445
446	// Proxy site management
447	async fn create_proxy_site(&self, data: &CreateProxySiteData<'_>) -> ClResult<ProxySiteData>;
448	async fn read_proxy_site(&self, site_id: i64) -> ClResult<ProxySiteData>;
449	async fn read_proxy_site_by_domain(&self, domain: &str) -> ClResult<ProxySiteData>;
450	async fn update_proxy_site(
451		&self,
452		site_id: i64,
453		data: &UpdateProxySiteData<'_>,
454	) -> ClResult<ProxySiteData>;
455	async fn delete_proxy_site(&self, site_id: i64) -> ClResult<()>;
456	async fn list_proxy_sites(&self) -> ClResult<Vec<ProxySiteData>>;
457	async fn update_proxy_site_cert(
458		&self,
459		site_id: i64,
460		cert: &str,
461		key: &str,
462		expires_at: Timestamp,
463	) -> ClResult<()>;
464	async fn list_proxy_sites_needing_cert_renewal(
465		&self,
466		renewal_days: u32,
467	) -> ClResult<Vec<ProxySiteData>>;
468}
469
470#[cfg(test)]
471mod tests {
472	use super::*;
473
474	#[test]
475	pub fn test_access_token() {
476		let token: AccessToken<String> = AccessToken {
477			iss: "a@a".into(),
478			sub: Some("b@b".into()),
479			scope: None,
480			r: None,
481			exp: Timestamp::now(),
482		};
483
484		assert_eq!(token.iss, "a@a");
485		assert_eq!(token.sub.as_ref().unwrap(), "b@b");
486	}
487}
488
489// vim: ts=4