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
use simple_string_patterns::CharGroupMatch;

/// Authentication options
/// Only the API key is mandatory.
/// By default a uuid is not validated, the random split characters are `%.,` and max age or tolerance is 5 minutes (or 300000 milliseconds)
/// NB: The random split characters (rand_char_str) may not include alphanumeric characters or underscores, as these would break other validation
#[derive(Debug, Clone, Copy)]
pub struct AuthOptions<'a> {
  process_uuid: bool,
  api_key: &'a str,
  rand_char_str: &'a str,
  tolerance: u32,
}

impl<'a> AuthOptions<'a> {
  pub fn new(api_key: &'a str) -> Self {
    AuthOptions {
      process_uuid: false,
      api_key,
      rand_char_str: "%.,",
      tolerance: 300_000 // five minutes
    }
  }

  pub fn set_rand_char_str(&mut self, char_str: &'a str) -> Self {
    let is_invalid = char_str.has_alphanumeric() || char_str.contains("_");
    if !is_invalid {
      self.rand_char_str = char_str;
    }
    self.to_owned()
  }

  pub fn key(&self) -> &'a str {
    self.api_key
  }

  pub fn rand_chars(&self) -> Vec<char> {
    self.rand_char_str.chars().into_iter().collect::<Vec<char>>()
  }

  pub fn tolerance(&self) -> i64 {
    self.tolerance as i64
  }

  pub fn should_check_uuid(&self) -> bool {
    self.process_uuid
  }

  pub fn check_uuid(&mut self, val: bool) -> Self {
    self.process_uuid = val;
    self.to_owned()
  }

  pub fn set_tolerance(&mut self, millis: u32) -> Self {
    self.tolerance = millis;
    self.to_owned()
  }

  pub fn set_tolerance_secs(&mut self, secs: u32) -> Self {
    self.tolerance = secs * 1000;
    self.to_owned()
  }

  pub fn set_tolerance_mins(&mut self, mins: u32) -> Self {
    self.tolerance = mins * 60 * 1000;
    self.to_owned()
  }

}