uvb_storage_api/
session.rs1use async_trait::async_trait;
2use serde::{Deserialize, Serialize};
3use std::time::{Duration, SystemTime};
4use thiserror::Error;
5use uvb_core::TenantId;
6
7#[derive(Debug, Error)]
8pub enum SessionError {
9 #[error("session not found")]
10 NotFound,
11 #[error("session expired")]
12 Expired,
13 #[error("storage error: {0}")]
14 Storage(String),
15}
16
17#[derive(Clone, Debug, Serialize, Deserialize)]
19pub struct SessionRecord {
20 pub id: String,
21 pub user_id: String,
22 pub tenant_id: TenantId,
23 pub transaction_id: String,
24 pub factors_used: Vec<String>,
25 pub assurance_level: i32,
26 pub metadata: serde_json::Value,
27 pub created_at: SystemTime,
28 pub expires_at: SystemTime,
29 pub last_activity_at: SystemTime,
30}
31
32#[async_trait]
40pub trait SessionStore: Send + Sync {
41 async fn create(&self, record: SessionRecord) -> Result<String, SessionError>;
43
44 async fn get(&self, id: &str) -> Result<Option<SessionRecord>, SessionError>;
46
47 async fn update(&self, record: SessionRecord) -> Result<(), SessionError>;
49
50 async fn delete(&self, id: &str) -> Result<(), SessionError>;
52
53 async fn delete_by_user(
55 &self,
56 user_id: &str,
57 tenant_id: &TenantId,
58 ) -> Result<usize, SessionError>;
59
60 async fn extend(&self, id: &str, duration: Duration) -> Result<(), SessionError>;
62
63 async fn touch(&self, id: &str) -> Result<(), SessionError>;
65
66 async fn cleanup_expired(&self) -> Result<usize, SessionError>;
68
69 async fn list_by_user(
71 &self,
72 user_id: &str,
73 tenant_id: &TenantId,
74 ) -> Result<Vec<SessionRecord>, SessionError>;
75}