Skip to main content

auth_framework/
user_context.rs

1//! User context and authentication state management
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use uuid::Uuid;
6
7/// Represents authenticated user context
8#[derive(Clone, Serialize, Deserialize)]
9pub struct UserContext {
10    pub user_id: String,
11    pub username: String,
12    pub email: Option<String>,
13    pub scopes: Vec<String>,
14    pub authenticated_at: std::time::SystemTime,
15    pub session_id: String,
16    pub attributes: HashMap<String, String>,
17}
18
19impl std::fmt::Debug for UserContext {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        let mut d = f.debug_struct("UserContext");
22        d.field("user_id", &self.user_id);
23        d.field("username", &self.username);
24        d.field("email", &self.email);
25        d.field("scopes", &self.scopes);
26        d.field("authenticated_at", &self.authenticated_at);
27        d.field(
28            "session_id",
29            &crate::utils::redact::redacted(&self.session_id),
30        );
31        d.field("attributes", &self.attributes);
32        d.finish()
33    }
34}
35
36impl UserContext {
37    pub fn new(user_id: String, username: String, email: Option<String>) -> Self {
38        Self {
39            user_id,
40            username,
41            email,
42            scopes: Vec::new(),
43            authenticated_at: std::time::SystemTime::now(),
44            session_id: Uuid::new_v4().to_string(),
45            attributes: HashMap::new(),
46        }
47    }
48
49    pub fn with_scopes(mut self, scopes: Vec<String>) -> Self {
50        self.scopes = scopes;
51        self
52    }
53
54    pub fn add_attribute(&mut self, key: String, value: String) {
55        self.attributes.insert(key, value);
56    }
57
58    pub fn has_scope(&self, scope: &str) -> bool {
59        self.scopes.contains(&scope.to_string())
60    }
61}
62
63/// Session store for managing user authentication state
64#[derive(Clone)]
65pub struct SessionStore {
66    sessions: std::collections::HashMap<String, UserContext>,
67}
68
69impl std::fmt::Debug for SessionStore {
70    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71        let mut d = f.debug_struct("SessionStore");
72        d.field("sessions", &crate::utils::redact::redacted(&self.sessions));
73        d.finish()
74    }
75}
76
77impl SessionStore {
78    pub fn new() -> Self {
79        Self {
80            sessions: HashMap::new(),
81        }
82    }
83
84    pub fn create_session(&mut self, user_context: UserContext) -> String {
85        let session_id = user_context.session_id.clone();
86        self.sessions.insert(session_id.clone(), user_context);
87        session_id
88    }
89
90    pub fn get_session(&self, session_id: &str) -> Option<&UserContext> {
91        self.sessions.get(session_id)
92    }
93
94    pub fn invalidate_session(&mut self, session_id: &str) -> bool {
95        self.sessions.remove(session_id).is_some()
96    }
97
98    pub fn validate_session(&self, session_id: &str) -> bool {
99        self.sessions.contains_key(session_id)
100    }
101}
102
103impl Default for SessionStore {
104    fn default() -> Self {
105        Self::new()
106    }
107}