reinhardt-auth 0.1.2

Authentication and authorization system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Cache-based session backend
//!
//! This module provides session storage using cache backends like Redis or in-memory cache.
//!
//! ## Example
//!
//! ```rust
//! use reinhardt_auth::sessions::backends::{InMemorySessionBackend, SessionBackend};
//! use serde_json::json;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Create an in-memory session backend
//! let backend = InMemorySessionBackend::new();
//!
//! // Store user session with login data
//! let session_data = json!({
//!     "user_id": 123,
//!     "username": "bob",
//!     "last_login": "2024-01-15T10:30:00Z",
//! });
//!
//! backend.save("session_xyz", &session_data, Some(3600)).await?;
//!
//! // Load session data
//! let loaded: Option<serde_json::Value> = backend.load("session_xyz").await?;
//! assert!(loaded.is_some());
//! assert_eq!(loaded.unwrap()["user_id"], 123);
//! # Ok(())
//! # }
//! ```

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use reinhardt_utils::cache::{Cache, InMemoryCache};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use thiserror::Error;

use crate::sessions::cleanup::{CleanupableBackend, SessionMetadata};

/// Session backend errors
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum SessionError {
	/// An error occurred in the cache backend.
	#[error("Cache error: {0}")]
	CacheError(String),
	/// Session data could not be serialized or deserialized.
	#[error("Serialization error: {0}")]
	SerializationError(String),
	/// The session has expired due to inactivity.
	#[error("Session has expired due to inactivity")]
	SessionExpired,
}

/// Session backend trait
#[async_trait]
pub trait SessionBackend: Send + Sync + Clone {
	/// Load session data by key
	async fn load<T>(&self, session_key: &str) -> Result<Option<T>, SessionError>
	where
		T: for<'de> Deserialize<'de> + Serialize + Send + Sync;

	/// Save session data with optional TTL (in seconds)
	async fn save<T>(
		&self,
		session_key: &str,
		data: &T,
		ttl: Option<u64>,
	) -> Result<(), SessionError>
	where
		T: Serialize + Send + Sync;

	/// Delete session by key
	async fn delete(&self, session_key: &str) -> Result<(), SessionError>;

	/// Check if session exists
	async fn exists(&self, session_key: &str) -> Result<bool, SessionError>;
}

/// In-memory session backend
///
/// Stores sessions in memory using the InMemoryCache backend.
/// Sessions are lost when the application restarts.
///
/// ## Example
///
/// ```rust
/// use reinhardt_auth::sessions::backends::{InMemorySessionBackend, SessionBackend};
/// use serde_json::json;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let backend = InMemorySessionBackend::new();
///
/// // Store shopping cart in session
/// let cart_data = json!({
///     "items": ["item1", "item2"],
///     "total": 59.99,
/// });
///
/// backend.save("cart_session_456", &cart_data, Some(1800)).await?;
///
/// // Check if session exists
/// assert!(backend.exists("cart_session_456").await?);
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct InMemorySessionBackend {
	cache: Arc<InMemoryCache>,
}

impl InMemorySessionBackend {
	/// Create a new in-memory session backend
	pub fn new() -> Self {
		Self {
			cache: Arc::new(InMemoryCache::new()),
		}
	}
}

impl Default for InMemorySessionBackend {
	fn default() -> Self {
		Self::new()
	}
}

#[async_trait]
impl SessionBackend for InMemorySessionBackend {
	async fn load<T>(&self, session_key: &str) -> Result<Option<T>, SessionError>
	where
		T: for<'de> Deserialize<'de> + Serialize + Send + Sync,
	{
		self.cache
			.get(session_key)
			.await
			.map_err(|e| SessionError::CacheError(e.to_string()))
	}

	async fn save<T>(
		&self,
		session_key: &str,
		data: &T,
		ttl: Option<u64>,
	) -> Result<(), SessionError>
	where
		T: Serialize + Send + Sync,
	{
		let duration = ttl.map(std::time::Duration::from_secs);
		self.cache
			.set(session_key, data, duration)
			.await
			.map_err(|e| SessionError::CacheError(e.to_string()))
	}

	async fn delete(&self, session_key: &str) -> Result<(), SessionError> {
		self.cache
			.delete(session_key)
			.await
			.map_err(|e| SessionError::CacheError(e.to_string()))
	}

	async fn exists(&self, session_key: &str) -> Result<bool, SessionError> {
		self.cache
			.has_key(session_key)
			.await
			.map_err(|e| SessionError::CacheError(e.to_string()))
	}
}

#[async_trait]
impl CleanupableBackend for InMemorySessionBackend {
	/// Get all session keys
	///
	/// Returns all session keys stored in the backend,
	/// including expired sessions that haven't been cleaned up.
	async fn get_all_keys(&self) -> Result<Vec<String>, SessionError> {
		Ok(self.cache.list_keys().await)
	}

	/// Get session metadata
	///
	/// Returns metadata for the specified session.
	/// Returns `None` if the session does not exist.
	async fn get_metadata(
		&self,
		session_key: &str,
	) -> Result<Option<SessionMetadata>, SessionError> {
		match self.cache.inspect_entry_with_timestamps(session_key).await {
			Ok(Some((created, accessed))) => Ok(Some(SessionMetadata {
				created_at: DateTime::<Utc>::from(created),
				last_accessed: accessed.map(DateTime::<Utc>::from),
			})),
			Ok(None) => Ok(None),
			Err(e) => Err(SessionError::CacheError(e.to_string())),
		}
	}
}

/// Cache-based session backend
///
/// Generic session backend that works with any cache implementation.
///
/// ## Example
///
/// ```rust
/// use reinhardt_auth::sessions::backends::{CacheSessionBackend, SessionBackend};
/// use reinhardt_utils::cache::InMemoryCache;
/// use serde_json::json;
/// use std::sync::Arc;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let cache = Arc::new(InMemoryCache::new());
/// let backend = CacheSessionBackend::new(cache);
///
/// // Store user preferences in session
/// let preferences = json!({
///     "theme": "dark",
///     "language": "en",
///     "notifications": true,
/// });
///
/// backend.save("pref_session_789", &preferences, Some(7200)).await?;
///
/// // Load preferences
/// let loaded: Option<serde_json::Value> = backend.load("pref_session_789").await?;
/// assert_eq!(loaded.unwrap()["theme"], "dark");
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct CacheSessionBackend<C: Cache + Clone> {
	cache: Arc<C>,
}

impl<C: Cache + Clone> CacheSessionBackend<C> {
	/// Create a new cache-based session backend
	pub fn new(cache: Arc<C>) -> Self {
		Self { cache }
	}
}

#[async_trait]
impl<C: Cache + Clone + 'static> SessionBackend for CacheSessionBackend<C> {
	async fn load<T>(&self, session_key: &str) -> Result<Option<T>, SessionError>
	where
		T: for<'de> Deserialize<'de> + Serialize + Send + Sync,
	{
		self.cache
			.get(session_key)
			.await
			.map_err(|e| SessionError::CacheError(e.to_string()))
	}

	async fn save<T>(
		&self,
		session_key: &str,
		data: &T,
		ttl: Option<u64>,
	) -> Result<(), SessionError>
	where
		T: Serialize + Send + Sync,
	{
		let duration = ttl.map(std::time::Duration::from_secs);
		self.cache
			.set(session_key, data, duration)
			.await
			.map_err(|e| SessionError::CacheError(e.to_string()))
	}

	async fn delete(&self, session_key: &str) -> Result<(), SessionError> {
		self.cache
			.delete(session_key)
			.await
			.map_err(|e| SessionError::CacheError(e.to_string()))
	}

	async fn exists(&self, session_key: &str) -> Result<bool, SessionError> {
		self.cache
			.has_key(session_key)
			.await
			.map_err(|e| SessionError::CacheError(e.to_string()))
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use rstest::rstest;
	use serde_json::json;
	use std::collections::HashMap;

	#[rstest]
	#[tokio::test]
	async fn test_in_memory_save_and_load_roundtrip() {
		// Arrange
		let backend = InMemorySessionBackend::new();
		let mut data = HashMap::new();
		data.insert("user_id".to_string(), json!(42));
		data.insert("username".to_string(), json!("alice"));

		// Act
		backend.save("sess_1", &data, Some(3600)).await.unwrap();
		let loaded: Option<HashMap<String, serde_json::Value>> =
			backend.load("sess_1").await.unwrap();

		// Assert
		let loaded = loaded.unwrap();
		assert_eq!(loaded["user_id"], json!(42));
		assert_eq!(loaded["username"], json!("alice"));
	}

	#[rstest]
	#[tokio::test]
	async fn test_in_memory_load_nonexistent_key() {
		// Arrange
		let backend = InMemorySessionBackend::new();

		// Act
		let loaded: Option<serde_json::Value> = backend.load("nonexistent").await.unwrap();

		// Assert
		assert!(loaded.is_none());
	}

	#[rstest]
	#[tokio::test]
	async fn test_in_memory_delete_removes_session() {
		// Arrange
		let backend = InMemorySessionBackend::new();
		let data = json!({"key": "value"});
		backend.save("sess_del", &data, Some(3600)).await.unwrap();

		// Act
		backend.delete("sess_del").await.unwrap();
		let loaded: Option<serde_json::Value> = backend.load("sess_del").await.unwrap();

		// Assert
		assert!(loaded.is_none());
	}

	#[rstest]
	#[tokio::test]
	async fn test_in_memory_exists_reflects_state() {
		// Arrange
		let backend = InMemorySessionBackend::new();
		let data = json!({"active": true});

		// Assert - initially does not exist
		assert!(!backend.exists("sess_ex").await.unwrap());

		// Act - save
		backend.save("sess_ex", &data, Some(3600)).await.unwrap();

		// Assert - exists after save
		assert!(backend.exists("sess_ex").await.unwrap());

		// Act - delete
		backend.delete("sess_ex").await.unwrap();

		// Assert - does not exist after delete
		assert!(!backend.exists("sess_ex").await.unwrap());
	}

	#[rstest]
	#[tokio::test]
	async fn test_in_memory_save_overwrites_existing() {
		// Arrange
		let backend = InMemorySessionBackend::new();
		let data_v1 = json!({"version": 1});
		let data_v2 = json!({"version": 2});

		// Act
		backend.save("sess_ow", &data_v1, Some(3600)).await.unwrap();
		backend.save("sess_ow", &data_v2, Some(3600)).await.unwrap();
		let loaded: Option<serde_json::Value> = backend.load("sess_ow").await.unwrap();

		// Assert
		assert_eq!(loaded.unwrap()["version"], 2);
	}

	#[rstest]
	#[tokio::test]
	async fn test_in_memory_save_with_ttl() {
		// Arrange
		let backend = InMemorySessionBackend::new();
		let data = json!({"ttl_test": true});

		// Act
		backend.save("sess_ttl", &data, Some(60)).await.unwrap();
		let loaded: Option<serde_json::Value> = backend.load("sess_ttl").await.unwrap();

		// Assert
		assert_eq!(loaded.unwrap()["ttl_test"], true);
	}

	#[rstest]
	#[tokio::test]
	async fn test_cache_backend_wrapper_save_and_load() {
		// Arrange
		let cache = Arc::new(InMemoryCache::new());
		let backend = CacheSessionBackend::new(cache);
		let data = json!({"wrapped": "value", "count": 99});

		// Act
		backend
			.save("wrapped_sess", &data, Some(3600))
			.await
			.unwrap();
		let loaded: Option<serde_json::Value> = backend.load("wrapped_sess").await.unwrap();

		// Assert
		let loaded = loaded.unwrap();
		assert_eq!(loaded["wrapped"], "value");
		assert_eq!(loaded["count"], 99);
	}

	#[rstest]
	#[tokio::test]
	async fn test_cache_backend_wrapper_delete_and_exists() {
		// Arrange
		let cache = Arc::new(InMemoryCache::new());
		let backend = CacheSessionBackend::new(cache);
		let data = json!({"item": "to_delete"});

		// Act - save and verify exists
		backend.save("wrap_del", &data, Some(3600)).await.unwrap();
		assert!(backend.exists("wrap_del").await.unwrap());

		// Act - delete
		backend.delete("wrap_del").await.unwrap();

		// Assert
		assert!(!backend.exists("wrap_del").await.unwrap());
		let loaded: Option<serde_json::Value> = backend.load("wrap_del").await.unwrap();
		assert!(loaded.is_none());
	}
}