1use 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#[skip_serializing_none]
26#[derive(Debug, Clone, Default, Deserialize, Serialize)]
27pub struct ActionToken {
28 pub iss: Box<str>,
30
31 pub k: Box<str>,
33
34 pub t: Box<str>,
36
37 pub c: Option<serde_json::Value>,
39
40 pub p: Option<Box<str>>,
42
43 pub a: Option<Vec<Box<str>>>,
45
46 pub aud: Option<Box<str>>,
48
49 pub sub: Option<Box<str>>,
51
52 pub iat: Timestamp,
54
55 pub exp: Option<Timestamp>,
57
58 pub f: Option<Box<str>>,
60
61 pub v: Option<char>,
63
64 #[serde(rename = "_", default, skip_serializing_if = "Option::is_none")]
66 pub nonce: Option<Box<str>>,
67}
68
69#[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#[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#[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 pub status: Option<Box<str>>,
106 pub keys: Vec<AuthKey>,
107}
108
109#[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#[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#[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#[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#[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#[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#[derive(Debug)]
192pub struct TenantCertRenewalRow {
193 pub tn_id: TnId,
194 pub id_tag: Box<str>,
195 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#[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#[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#[derive(Debug)]
229pub struct CreatedApiKey {
230 pub info: ApiKeyInfo,
231 pub plaintext_key: Box<str>,
232}
233
234#[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#[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#[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#[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#[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#[async_trait]
311pub trait AuthAdapter: Debug + Send + Sync {
312 async fn validate_access_token(&self, tn_id: TnId, token: &str) -> ClResult<AuthCtx>;
314
315 async fn read_id_tag(&self, tn_id: TnId) -> ClResult<Box<str>>;
318
319 async fn read_tn_id(&self, id_tag: &str) -> ClResult<TnId>;
321
322 async fn read_tenant(&self, id_tag: &str) -> ClResult<AuthProfile>;
324
325 async fn create_tenant_registration(&self, email: &str) -> ClResult<()>;
327
328 async fn create_tenant(&self, id_tag: &str, data: CreateTenantData<'_>) -> ClResult<TnId>;
330
331 async fn delete_tenant(&self, id_tag: &str) -> ClResult<()>;
333
334 async fn list_tenants(&self, opts: &ListTenantsOptions<'_>) -> ClResult<Vec<TenantListItem>>;
336
337 async fn count_tenants(&self, opts: &ListTenantsOptions<'_>) -> ClResult<usize>;
339
340 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 async fn update_idp_api_key(&self, id_tag: &str, api_key: &str) -> ClResult<()>;
347
348 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 async fn record_cert_renewal_failure(&self, tn_id: TnId, error: &str) -> ClResult<()>;
363
364 async fn record_cert_renewal_success(&self, tn_id: TnId) -> ClResult<()>;
367
368 async fn record_cert_renewal_notification(&self, tn_id: TnId) -> ClResult<()>;
370
371 async fn update_tenant_status(&self, tn_id: TnId, status: char) -> ClResult<()>;
377
378 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 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 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 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 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 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