auth_framework/
user_context.rs1use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use uuid::Uuid;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct UserContext {
10 pub user_id: String,
11 pub username: String,
12 pub email: Option<String>,
13 pub scopes: crate::types::Scopes,
14 pub authenticated_at: std::time::SystemTime,
15 pub session_id: String,
16 pub attributes: crate::types::UserAttributesString,
17}
18
19impl UserContext {
20 pub fn new(user_id: String, username: String, email: Option<String>) -> Self {
21 Self {
22 user_id,
23 username,
24 email,
25 scopes: crate::types::Scopes::empty(),
26 authenticated_at: std::time::SystemTime::now(),
27 session_id: Uuid::new_v4().to_string(),
28 attributes: crate::types::UserAttributesString::empty(),
29 }
30 }
31
32 pub fn with_scopes(mut self, scopes: Vec<String>) -> Self {
33 self.scopes = scopes.into();
34 self
35 }
36
37 pub fn add_attribute(&mut self, key: String, value: String) {
38 self.attributes.insert(key, value);
39 }
40
41 pub fn has_scope(&self, scope: &str) -> bool {
42 self.scopes.contains(scope)
43 }
44}
45
46impl UserContext {
48 pub fn builder(user_id: impl Into<String>, username: impl Into<String>) -> UserContextBuilder {
50 UserContextBuilder::new(user_id.into(), username.into())
51 }
52}
53
54pub struct UserContextBuilder {
56 user_id: String,
57 username: String,
58 email: Option<String>,
59 scopes: Vec<String>,
60 attributes: HashMap<String, String>,
61}
62
63impl UserContextBuilder {
64 pub fn new(user_id: String, username: String) -> Self {
65 Self {
66 user_id,
67 username,
68 email: None,
69 scopes: Vec::new(),
70 attributes: HashMap::new(),
71 }
72 }
73
74 pub fn email(mut self, email: impl Into<String>) -> Self {
75 self.email = Some(email.into());
76 self
77 }
78
79 pub fn scope(mut self, scope: impl Into<String>) -> Self {
80 self.scopes.push(scope.into());
81 self
82 }
83
84 pub fn scopes(mut self, scopes: Vec<String>) -> Self {
85 self.scopes = scopes;
86 self
87 }
88
89 pub fn attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
90 self.attributes.insert(key.into(), value.into());
91 self
92 }
93
94 pub fn build(self) -> UserContext {
95 let mut ctx = UserContext::new(self.user_id, self.username, self.email);
96 ctx.scopes = self.scopes.into();
97 ctx.attributes = self.attributes.into();
98 ctx
99 }
100}
101
102#[derive(Debug, Clone)]
103pub struct SessionStore {
104 sessions: std::collections::HashMap<String, UserContext>,
105}
106
107impl SessionStore {
108 pub fn new() -> Self {
109 Self {
110 sessions: HashMap::new(),
111 }
112 }
113
114 pub fn create_session(&mut self, user_context: UserContext) -> String {
115 let session_id = user_context.session_id.clone();
116 self.sessions.insert(session_id.clone(), user_context);
117 session_id
118 }
119
120 pub fn get_session(&self, session_id: &str) -> Option<&UserContext> {
121 self.sessions.get(session_id)
122 }
123
124 pub fn invalidate_session(&mut self, session_id: &str) -> bool {
125 self.sessions.remove(session_id).is_some()
126 }
127
128 pub fn validate_session(&self, session_id: &str) -> bool {
129 self.sessions.contains_key(session_id)
130 }
131}
132
133impl Default for SessionStore {
134 fn default() -> Self {
135 Self::new()
136 }
137}