Skip to main content

trojan_auth/
result.rs

1//! Authentication result types.
2
3/// Result of a successful authentication.
4#[derive(Debug, Clone, Default)]
5pub struct AuthResult {
6    /// Optional user identifier for logging/metrics.
7    pub user_id: Option<String>,
8
9    /// Optional user metadata.
10    pub metadata: Option<AuthMetadata>,
11}
12
13impl AuthResult {
14    /// Create a new auth result with no user ID.
15    #[inline]
16    pub fn anonymous() -> Self {
17        Self::default()
18    }
19
20    /// Create a new auth result with a user ID.
21    #[inline]
22    pub fn with_user_id(user_id: impl Into<String>) -> Self {
23        Self {
24            user_id: Some(user_id.into()),
25            metadata: None,
26        }
27    }
28
29    /// Add metadata to the result.
30    #[inline]
31    pub fn with_metadata(mut self, metadata: AuthMetadata) -> Self {
32        self.metadata = Some(metadata);
33        self
34    }
35}
36
37/// Optional metadata associated with an authenticated user.
38#[derive(Debug, Clone, Default)]
39pub struct AuthMetadata {
40    /// Traffic limit in bytes (0 = unlimited).
41    pub traffic_limit: u64,
42
43    /// Traffic used in bytes.
44    pub traffic_used: u64,
45
46    /// Expiration timestamp (0 = never).
47    pub expires_at: u64,
48
49    /// Whether the user is enabled.
50    pub enabled: bool,
51}
52
53impl AuthMetadata {
54    /// Create new metadata with defaults (unlimited, enabled).
55    pub fn new() -> Self {
56        Self {
57            traffic_limit: 0,
58            traffic_used: 0,
59            expires_at: 0,
60            enabled: true,
61        }
62    }
63
64    /// Check if the user has exceeded their traffic limit.
65    #[inline]
66    pub fn is_over_limit(&self) -> bool {
67        self.traffic_limit > 0 && self.traffic_used >= self.traffic_limit
68    }
69
70    /// Check if the user has expired.
71    #[inline]
72    pub fn is_expired(&self, now: u64) -> bool {
73        self.expires_at > 0 && now >= self.expires_at
74    }
75}