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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use jwt::{SignWithKey, VerifyWithKey};

use super::http_parser::Request;
pub use expedite::datetime::period::Period;
pub use expedite::datetime::time::Time;
use hmac::Hmac;
use sha2::Sha256;
use std::collections::{BTreeMap, HashMap};
use std::str::FromStr;
use std::sync::Arc;

use chrono::TimeZone;

use chrono_tz::GMT;
use chrono_tz::US::Pacific;

pub struct Cookie {
    name: String,
    path: String,
    domain: String,
    max_age: Option<Time>,
    http_only: bool,
    data: BTreeMap<String, String>,
    secret_key: Arc<Hmac<Sha256>>,
}

fn cookie_str_to_map(s: &str) -> HashMap<&str, &str> {
    let mut map = HashMap::new();
    for e in s.split(";") {
        match e.split_once("=") {
            Some((k, v)) => {
                map.insert(k.trim(), v.trim());
            }
            None => {
                continue;
            }
        }
    }
    map
}

impl Cookie {
    /// > Create a cookie object from Request
    /// >> - return a exist cookie if the client provides that
    /// >> - Otherwise, return a new cookie
    pub fn new(name: String, req: &Request) -> Self {
        let key = &*req.get_secret_key();
        let path = req.url_to_path();
        match req.get_header("Cookie") {
            Some(s) => match cookie_str_to_map(s).get(name.as_str()) {
                Some(&token) => {
                    match token.verify_with_key(key) {
                        Ok(x) => {
                            // let borrow:& mut BTreeMap<String, String> = & mut x;
                            return Cookie {
                                name,
                                path: String::from(path),
                                domain: String::new(),
                                max_age: None,
                                http_only: false,
                                data: x,
                                secret_key: req.get_secret_key(),
                            };
                        }
                        Err(_) => {}
                    }
                }
                None => {}
            },
            None => {}
        }
        Cookie {
            name,
            path: String::from(path),
            domain: String::new(),
            max_age: None,
            http_only: false,
            data: BTreeMap::new(),
            secret_key: req.get_secret_key(),
        }
    }

    /// > Set the cookie name
    pub fn set_name(&mut self, name: String) {
        self.name = name;
    }
    /// > Get the cookie name
    pub fn get_name(&self) -> &String {
        &self.name
    }
    /// > Set the cookie path
    pub fn set_path(&mut self, path: String) {
        self.path = path
    }
    /// > Get the cookie path
    pub fn get_path(&self) -> &String {
        &self.path
    }
    /// > Set the cookie domain
    pub fn set_domain(&mut self, domain: String) {
        self.domain = domain;
    }
    /// > Get the cookie domain
    pub fn get_domain(&self) -> &String {
        &self.domain
    }

    /// > Set the cookie max_age
    pub fn set_max_age(&mut self, time: Time) {
        self.max_age = Some(time);
    }

    /// > Get the cookie max_age
    pub fn get_max_age(&self) -> &Option<Time> {
        &self.max_age
    }

    /// > Set the cookie http-only
    pub fn set_http_only(&mut self, v: bool) {
        self.http_only = v;
    }
    /// > Get the cookie http-only
    pub fn get_http_only(&self) -> bool {
        self.http_only
    }

	/// > Get data from a valid cookie
    pub fn get_data<T: FromStr>(&self, k: String) -> Option<T> {
        match self.data.get(&k) {
            Some(v) => match v.parse() {
                Ok(v) => {
                    return Some(v);
                }
                Err(_) => {
                    return None;
                }
            },
            None => {
                return None;
            }
        }
    }

	/// > Insert data to cookie
    pub fn insert<T: ToString>(&mut self, k: String, v: T) {
        self.data.insert(k, v.to_string());
    }

	/// > Generate a token from the cookie
    pub fn gen_token(&self) -> Option<String> {
        match self.data.clone().sign_with_key(&*self.secret_key) {
            Ok(s) => Some(s),
            Err(_) => None,
        }
    }

    pub(crate) fn to_string(&self) -> Option<String> {
        let time = if let Some(ref t) = self.max_age {
            let time = Pacific
                .ymd(t.date.year as i32, t.date.month, t.date.day)
                .and_hms(t.hours, t.minutes, t.seconds);
            let gmt = time.with_timezone(&GMT);
            gmt.to_rfc2822()
        } else {
            String::from("Session")
        };
        let domain = {
            if self.domain == "" {
                "".to_string()
            } else {
                format!("domain={};", self.domain)
            }
        };
        let http_only = {
            if self.http_only {
				"HttpOnly;"
            } else {
				""
            }
        };
        if let Some(token) = self.gen_token() {
            let r = format!(
                "{name}={token}; path={path}; {domain} Expires={time}; {http_only}",
                name = self.name,
                path = self.path,
                time = time.to_string()
            );
            Some(r)
        } else {
            None
        }
    }
}