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
//! REST API authentication
//!
//! Re-exports authentication types from reinhardt-core::auth.
// Re-export core authentication types from reinhardt-auth
pub use reinhardt_auth::{
AllowAny, AuthBackend, AuthIdentity, IsAdminUser, IsAuthenticated, IsAuthenticatedOrReadOnly,
Permission,
};
// Re-export JWT types conditionally from reinhardt-auth
#[cfg(feature = "jwt")]
pub use reinhardt_auth::{Claims, JwtAuth};
/// Authentication result (REST-specific utility)
#[derive(Debug, Clone)]
pub enum AuthResult<U> {
/// Successfully authenticated user
Authenticated(U),
/// Anonymous (unauthenticated) user
Anonymous,
/// Authentication failed
Failed(String),
}
impl<U> AuthResult<U> {
/// Check if authenticated
///
/// # Examples
///
/// ```
/// use reinhardt_rest::authentication::AuthResult;
///
/// let result = AuthResult::<String>::Authenticated("user123".to_string());
/// assert!(result.is_authenticated());
///
/// let anonymous = AuthResult::<String>::Anonymous;
/// assert!(!anonymous.is_authenticated());
/// ```
pub fn is_authenticated(&self) -> bool {
matches!(self, AuthResult::Authenticated(_))
}
/// Get user if authenticated
///
/// # Examples
///
/// ```
/// use reinhardt_rest::authentication::AuthResult;
///
/// let result = AuthResult::Authenticated("user123".to_string());
/// assert_eq!(result.user(), Some("user123".to_string()));
///
/// let anonymous = AuthResult::<String>::Anonymous;
/// assert_eq!(anonymous.user(), None);
/// ```
pub fn user(self) -> Option<U> {
match self {
AuthResult::Authenticated(user) => Some(user),
_ => None,
}
}
/// Get error message if failed
///
/// # Examples
///
/// ```
/// use reinhardt_rest::authentication::AuthResult;
///
/// let result = AuthResult::<String>::Failed("Invalid credentials".to_string());
/// assert_eq!(result.error(), Some("Invalid credentials"));
///
/// let auth = AuthResult::Authenticated("user".to_string());
/// assert_eq!(auth.error(), None);
/// ```
pub fn error(&self) -> Option<&str> {
match self {
AuthResult::Failed(msg) => Some(msg),
_ => None,
}
}
}