reinhardt-auth 0.1.1

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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Token Blacklist
//!
//! Provides token blacklisting and refresh token management for JWT authentication.

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Mutex;

/// Reason for blacklisting a token
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum BlacklistReason {
	/// User logged out
	Logout,
	/// Token was compromised
	Compromised,
	/// Manual revocation by admin
	ManualRevoke,
	/// Token rotation
	Rotated,
}

/// Blacklisted token entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlacklistedToken {
	/// Token JTI (JWT ID)
	pub jti: String,
	/// When the token was blacklisted
	pub blacklisted_at: DateTime<Utc>,
	/// Reason for blacklisting
	pub reason: BlacklistReason,
	/// When the original token expires
	pub expires_at: DateTime<Utc>,
}

/// Statistics for blacklisted tokens
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlacklistStats {
	/// Total number of blacklisted tokens.
	pub total_blacklisted: usize,
	/// Number of tokens blacklisted due to logout.
	pub by_logout: usize,
	/// Number of tokens blacklisted due to compromise.
	pub by_compromise: usize,
	/// Number of tokens blacklisted by manual revocation.
	pub by_manual_revoke: usize,
	/// Number of tokens blacklisted due to rotation.
	pub by_rotation: usize,
}

/// Token blacklist trait for different storage backends
#[async_trait]
pub trait TokenBlacklist: Send + Sync {
	/// Add a token to the blacklist
	async fn blacklist(
		&self,
		jti: &str,
		expires_at: DateTime<Utc>,
		reason: BlacklistReason,
	) -> Result<(), String>;

	/// Check if a token is blacklisted
	async fn is_blacklisted(&self, jti: &str) -> Result<bool, String>;

	/// Remove expired tokens from blacklist
	async fn cleanup_expired(&self) -> Result<usize, String>;

	/// Get blacklist statistics
	async fn get_stats(&self) -> Result<BlacklistStats, String>;
}

/// In-memory token blacklist implementation
///
/// # Examples
///
/// ```
/// use reinhardt_auth::{InMemoryTokenBlacklist, TokenBlacklist, BlacklistReason};
/// use chrono::{Utc, Duration};
///
/// #[tokio::main]
/// async fn main() {
///     let blacklist = InMemoryTokenBlacklist::new();
///
///     let jti = "token_123";
///     let expires_at = Utc::now() + Duration::hours(1);
///
///     blacklist.blacklist(jti, expires_at, BlacklistReason::Logout).await.unwrap();
///     assert!(blacklist.is_blacklisted(jti).await.unwrap());
/// }
/// ```
pub struct InMemoryTokenBlacklist {
	tokens: Arc<Mutex<HashMap<String, BlacklistedToken>>>,
}

impl InMemoryTokenBlacklist {
	/// Create a new in-memory token blacklist
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_auth::InMemoryTokenBlacklist;
	///
	/// let blacklist = InMemoryTokenBlacklist::new();
	/// ```
	pub fn new() -> Self {
		Self {
			tokens: Arc::new(Mutex::new(HashMap::new())),
		}
	}
}

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

#[async_trait]
impl TokenBlacklist for InMemoryTokenBlacklist {
	async fn blacklist(
		&self,
		jti: &str,
		expires_at: DateTime<Utc>,
		reason: BlacklistReason,
	) -> Result<(), String> {
		let mut tokens = self.tokens.lock().await;
		tokens.insert(
			jti.to_string(),
			BlacklistedToken {
				jti: jti.to_string(),
				blacklisted_at: Utc::now(),
				reason,
				expires_at,
			},
		);
		Ok(())
	}

	async fn is_blacklisted(&self, jti: &str) -> Result<bool, String> {
		let tokens = self.tokens.lock().await;
		Ok(tokens.contains_key(jti))
	}

	async fn cleanup_expired(&self) -> Result<usize, String> {
		let mut tokens = self.tokens.lock().await;
		let now = Utc::now();
		let before_count = tokens.len();

		tokens.retain(|_, token| token.expires_at > now);

		Ok(before_count - tokens.len())
	}

	async fn get_stats(&self) -> Result<BlacklistStats, String> {
		let tokens = self.tokens.lock().await;

		let mut stats = BlacklistStats {
			total_blacklisted: tokens.len(),
			by_logout: 0,
			by_compromise: 0,
			by_manual_revoke: 0,
			by_rotation: 0,
		};

		for token in tokens.values() {
			match token.reason {
				BlacklistReason::Logout => stats.by_logout += 1,
				BlacklistReason::Compromised => stats.by_compromise += 1,
				BlacklistReason::ManualRevoke => stats.by_manual_revoke += 1,
				BlacklistReason::Rotated => stats.by_rotation += 1,
			}
		}

		Ok(stats)
	}
}

/// Refresh token
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefreshToken {
	/// Token JTI
	pub jti: String,
	/// User ID
	pub user_id: String,
	/// Created at
	pub created_at: DateTime<Utc>,
	/// Expires at
	pub expires_at: DateTime<Utc>,
	/// Whether token has been used
	pub is_used: bool,
}

/// Refresh token store trait
#[async_trait]
pub trait RefreshTokenStore: Send + Sync {
	/// Store a refresh token
	async fn store(&self, token: RefreshToken) -> Result<(), String>;

	/// Get a refresh token by JTI
	async fn get(&self, jti: &str) -> Result<Option<RefreshToken>, String>;

	/// Mark token as used
	async fn mark_used(&self, jti: &str) -> Result<(), String>;

	/// Delete a refresh token
	async fn delete(&self, jti: &str) -> Result<(), String>;

	/// Cleanup expired tokens
	async fn cleanup_expired(&self) -> Result<usize, String>;
}

/// In-memory refresh token store
///
/// # Examples
///
/// ```
/// use reinhardt_auth::{InMemoryRefreshTokenStore, RefreshTokenStore, RefreshToken};
/// use chrono::{Utc, Duration};
///
/// #[tokio::main]
/// async fn main() {
///     let store = InMemoryRefreshTokenStore::new();
///
///     let token = RefreshToken {
///         jti: "refresh_123".to_string(),
///         user_id: "user_456".to_string(),
///         created_at: Utc::now(),
///         expires_at: Utc::now() + Duration::days(7),
///         is_used: false,
///     };
///
///     store.store(token.clone()).await.unwrap();
///     let retrieved = store.get(&token.jti).await.unwrap();
///     assert!(retrieved.is_some());
/// }
/// ```
pub struct InMemoryRefreshTokenStore {
	tokens: Arc<Mutex<HashMap<String, RefreshToken>>>,
}

impl InMemoryRefreshTokenStore {
	/// Create a new in-memory refresh token store
	pub fn new() -> Self {
		Self {
			tokens: Arc::new(Mutex::new(HashMap::new())),
		}
	}
}

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

#[async_trait]
impl RefreshTokenStore for InMemoryRefreshTokenStore {
	async fn store(&self, token: RefreshToken) -> Result<(), String> {
		let mut tokens = self.tokens.lock().await;
		tokens.insert(token.jti.clone(), token);
		Ok(())
	}

	async fn get(&self, jti: &str) -> Result<Option<RefreshToken>, String> {
		let tokens = self.tokens.lock().await;
		Ok(tokens.get(jti).cloned())
	}

	async fn mark_used(&self, jti: &str) -> Result<(), String> {
		let mut tokens = self.tokens.lock().await;
		if let Some(token) = tokens.get_mut(jti) {
			token.is_used = true;
			Ok(())
		} else {
			Err("Token not found".to_string())
		}
	}

	async fn delete(&self, jti: &str) -> Result<(), String> {
		let mut tokens = self.tokens.lock().await;
		tokens.remove(jti);
		Ok(())
	}

	async fn cleanup_expired(&self) -> Result<usize, String> {
		let mut tokens = self.tokens.lock().await;
		let now = Utc::now();
		let before_count = tokens.len();

		tokens.retain(|_, token| token.expires_at > now);

		Ok(before_count - tokens.len())
	}
}

/// Token rotation manager
///
/// Manages automatic token rotation for enhanced security.
pub struct TokenRotationManager {
	blacklist: Arc<dyn TokenBlacklist>,
	refresh_store: Arc<dyn RefreshTokenStore>,
}

impl TokenRotationManager {
	/// Create a new token rotation manager
	pub fn new(
		blacklist: Arc<dyn TokenBlacklist>,
		refresh_store: Arc<dyn RefreshTokenStore>,
	) -> Self {
		Self {
			blacklist,
			refresh_store,
		}
	}

	/// Rotate a refresh token
	///
	/// Marks the old token as used and blacklists it, then stores the new token.
	pub async fn rotate_token(
		&self,
		old_jti: &str,
		new_token: RefreshToken,
		old_expires_at: DateTime<Utc>,
	) -> Result<(), String> {
		// Mark old token as used
		self.refresh_store.mark_used(old_jti).await?;

		// Blacklist old token
		self.blacklist
			.blacklist(old_jti, old_expires_at, BlacklistReason::Rotated)
			.await?;

		// Store new token
		self.refresh_store.store(new_token).await?;

		Ok(())
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use chrono::Duration;

	#[tokio::test]
	async fn test_blacklist_token() {
		let blacklist = InMemoryTokenBlacklist::new();
		let jti = "token_123";
		let expires_at = Utc::now() + Duration::hours(1);

		blacklist
			.blacklist(jti, expires_at, BlacklistReason::Logout)
			.await
			.unwrap();

		assert!(blacklist.is_blacklisted(jti).await.unwrap());
	}

	#[tokio::test]
	async fn test_blacklist_cleanup() {
		let blacklist = InMemoryTokenBlacklist::new();

		// Add expired token
		let expired_jti = "expired_token";
		let expired_at = Utc::now() - Duration::hours(1);
		blacklist
			.blacklist(expired_jti, expired_at, BlacklistReason::Logout)
			.await
			.unwrap();

		// Add valid token
		let valid_jti = "valid_token";
		let valid_expires = Utc::now() + Duration::hours(1);
		blacklist
			.blacklist(valid_jti, valid_expires, BlacklistReason::Logout)
			.await
			.unwrap();

		// Cleanup
		let removed = blacklist.cleanup_expired().await.unwrap();
		assert_eq!(removed, 1);

		assert!(!blacklist.is_blacklisted(expired_jti).await.unwrap());
		assert!(blacklist.is_blacklisted(valid_jti).await.unwrap());
	}

	#[tokio::test]
	async fn test_blacklist_stats() {
		let blacklist = InMemoryTokenBlacklist::new();
		let expires_at = Utc::now() + Duration::hours(1);

		blacklist
			.blacklist("token1", expires_at, BlacklistReason::Logout)
			.await
			.unwrap();
		blacklist
			.blacklist("token2", expires_at, BlacklistReason::Compromised)
			.await
			.unwrap();
		blacklist
			.blacklist("token3", expires_at, BlacklistReason::Logout)
			.await
			.unwrap();

		let stats = blacklist.get_stats().await.unwrap();
		assert_eq!(stats.total_blacklisted, 3);
		assert_eq!(stats.by_logout, 2);
		assert_eq!(stats.by_compromise, 1);
	}

	#[tokio::test]
	async fn test_refresh_token_store() {
		let store = InMemoryRefreshTokenStore::new();

		let token = RefreshToken {
			jti: "refresh_123".to_string(),
			user_id: "user_456".to_string(),
			created_at: Utc::now(),
			expires_at: Utc::now() + Duration::days(7),
			is_used: false,
		};

		store.store(token.clone()).await.unwrap();

		let retrieved = store.get(&token.jti).await.unwrap();
		assert!(retrieved.is_some());
		assert!(!retrieved.unwrap().is_used);

		store.mark_used(&token.jti).await.unwrap();
		let used_token = store.get(&token.jti).await.unwrap().unwrap();
		assert!(used_token.is_used);
	}

	#[tokio::test]
	async fn test_token_rotation() {
		let blacklist = Arc::new(InMemoryTokenBlacklist::new());
		let refresh_store = Arc::new(InMemoryRefreshTokenStore::new());
		let manager = TokenRotationManager::new(blacklist.clone(), refresh_store.clone());

		let old_token = RefreshToken {
			jti: "old_token".to_string(),
			user_id: "user_123".to_string(),
			created_at: Utc::now(),
			expires_at: Utc::now() + Duration::days(7),
			is_used: false,
		};

		refresh_store.store(old_token.clone()).await.unwrap();

		let new_token = RefreshToken {
			jti: "new_token".to_string(),
			user_id: "user_123".to_string(),
			created_at: Utc::now(),
			expires_at: Utc::now() + Duration::days(7),
			is_used: false,
		};

		manager
			.rotate_token(&old_token.jti, new_token.clone(), old_token.expires_at)
			.await
			.unwrap();

		// Check old token is blacklisted and marked as used
		assert!(blacklist.is_blacklisted(&old_token.jti).await.unwrap());
		let old_from_store = refresh_store.get(&old_token.jti).await.unwrap().unwrap();
		assert!(old_from_store.is_used);

		// Check new token is stored
		let new_from_store = refresh_store.get(&new_token.jti).await.unwrap();
		assert!(new_from_store.is_some());
	}
}