dynamic_token/
valid_auth_token.rs

1use crate::auth_status::AuthStatus;
2
3/// Return type for the from_dyaamic_key() method
4/// This is a simple tuple struct with getter methods for
5/// age() milliseconds since the decoded timestamp
6/// uuid embedded UUID if available
7/// has_user() indicates whether valid UUID-like string is embedded
8/// status() The outcome showing where or if validation failed
9/// status_key() a human-readable snake-case key indicating the status
10#[derive(Debug, Clone)]
11pub struct ValidAuthToken(pub bool, pub Option<String>, pub Option<i64>, pub AuthStatus);
12
13impl ValidAuthToken {
14  // Always return a 64bit integer for milliseconds irrespective of decoded initial values
15  // The age is the milliseconds between the timestamp encoded in the token and the current universal timestamp
16  // Negative values indicate the encoded timestamp is the future
17  pub fn age(&self) -> i64 {
18    if let Some(age_int) = self.2 {
19      age_int
20    } else {
21      0i64
22    }
23  }
24
25  pub fn valid(&self) -> bool {
26    self.0
27  }
28
29  pub fn uuid(&self) -> String {
30    self.1.clone().unwrap_or("".to_string())
31  }
32
33  pub fn has_user(&self) -> bool {
34    self.valid() && self.1.is_some()
35  }
36
37  pub fn status(&self) -> AuthStatus {
38    self.3.to_owned()
39  }
40
41  pub fn status_key(&self) -> String {
42    self.3.to_key()
43  }
44
45}