dynamic_token/
auth_options.rs

1use simple_string_patterns::CharGroupMatch;
2
3/// Authentication options
4/// Only the API key is mandatory.
5/// By default a uuid is not validated, the random split characters are `%.,` and max age or tolerance is 5 minutes (or 300000 milliseconds)
6/// NB: The random split characters (rand_char_str) may not include alphanumeric characters or underscores, as these would break other validation
7#[derive(Debug, Clone, Copy)]
8pub struct AuthOptions<'a> {
9  process_uuid: bool,
10  api_key: &'a str,
11  rand_char_str: &'a str,
12  tolerance: u32,
13}
14
15impl<'a> AuthOptions<'a> {
16  pub fn new(api_key: &'a str) -> Self {
17    AuthOptions {
18      process_uuid: false,
19      api_key,
20      rand_char_str: "%.,",
21      tolerance: 300_000 // five minutes
22    }
23  }
24
25  pub fn set_rand_char_str(&mut self, char_str: &'a str) -> Self {
26    let is_invalid = char_str.has_alphanumeric() || char_str.contains("_");
27    if !is_invalid {
28      self.rand_char_str = char_str;
29    }
30    self.to_owned()
31  }
32
33  /// Return the shared API key, for internal use in your app only
34  pub fn key(&self) -> &'a str {
35    self.api_key
36  }
37
38  /// Return the random split characters as an array
39  pub fn rand_chars(&self) -> Vec<char> {
40    self.rand_char_str.chars().into_iter().collect::<Vec<char>>()
41  }
42
43  /// Timestamp tolerance in milliseconds
44  pub fn tolerance(&self) -> i64 {
45    self.tolerance as i64
46  }
47
48  /// Only validate embedded UUIDs if this flag is true
49  pub fn should_check_uuid(&self) -> bool {
50    self.process_uuid
51  }
52
53  /// Set check UUID status. True means it must be present in a valid hexadecimal format once decoded
54  pub fn check_uuid(&mut self, val: bool) -> Self {
55    self.process_uuid = val;
56    self.to_owned()
57  }
58
59  /// Set timestamp tolerance in milliseconds
60  pub fn set_tolerance(&mut self, millis: u32) -> Self {
61    self.tolerance = millis;
62    self.to_owned()
63  }
64
65  /// Set timestamp tolerance in seconds
66  pub fn set_tolerance_secs(&mut self, secs: u32) -> Self {
67    self.tolerance = secs * 1000;
68    self.to_owned()
69  }
70
71  /// Set timestamp tolerance in minutes
72  pub fn set_tolerance_mins(&mut self, mins: u32) -> Self {
73    self.tolerance = mins * 60 * 1000;
74    self.to_owned()
75  }
76
77}
78
79