logo
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
194
195
196
197
198
199
200
//! Cookie helper.

use std::time::Duration;

use crate::types::{Cookie, Cookies, SameSite};

/// Cookie's Options
#[derive(Debug)]
pub struct CookieOptions {
    /// Cookie's name, `viz.sid` by defaults
    pub name: &'static str,
    /// Cookie's path, `/` by defaults
    pub path: &'static str,
    /// Cookie's secure, `true` by defaults
    pub secure: bool,
    /// Cookie's http_only, `true` by defaults
    pub http_only: bool,
    /// Cookie's maximum age, `24H` by defaults
    pub max_age: Option<Duration>,
    /// Cookie's domain
    pub domain: Option<&'static str>,
    /// Cookie's same_site, `Lax` by defaults
    pub same_site: Option<SameSite>,
}

impl CookieOptions {
    /// By default 24h for cookie.
    pub const MAX_AGE: u64 = 3600 * 24;

    /// Creates new `CookieOptions`
    pub fn new(name: &'static str) -> Self {
        Self::default().name(name)
    }

    /// Creates new `CookieOptions` with `name`
    pub fn name(mut self, name: &'static str) -> Self {
        self.name = name;
        self
    }

    /// Creates new `CookieOptions` with `max_age`
    pub fn max_age(mut self, max_age: Duration) -> Self {
        self.max_age.replace(max_age);
        self
    }

    /// Creates new `CookieOptions` with `domain`
    pub fn domain(mut self, domain: &'static str) -> Self {
        self.domain.replace(domain);
        self
    }

    /// Creates new `CookieOptions` with `path`
    pub fn path(mut self, path: &'static str) -> Self {
        self.path = path;
        self
    }

    /// Creates new `CookieOptions` with `secure`
    pub fn secure(mut self, secure: bool) -> Self {
        self.secure = secure;
        self
    }

    /// Creates new `CookieOptions` with `http_only`
    pub fn http_only(mut self, http_only: bool) -> Self {
        self.http_only = http_only;
        self
    }

    /// Creates new `CookieOptions` with `same_site`
    pub fn same_site(mut self, same_site: SameSite) -> Self {
        self.same_site.replace(same_site);
        self
    }

    /// Converts self into a [Cookie].
    pub fn into_cookie(&self, value: impl Into<String>) -> Cookie<'_> {
        let mut cookie = Cookie::new(self.name, value.into());

        cookie.set_path(self.path);
        cookie.set_secure(self.secure);
        cookie.set_http_only(self.http_only);
        cookie.set_same_site(self.same_site);

        if let Some(domain) = self.domain {
            cookie.set_domain(domain);
        }
        if let Some(max_age) = self.max_age {
            cookie.set_max_age(
                libcookie::time::Duration::try_from(max_age)
                    .expect("cant convert std Duration into time::Duration"),
            );
        }

        cookie
    }
}

impl Default for CookieOptions {
    fn default() -> Self {
        Self {
            domain: None,
            secure: true,
            http_only: true,
            path: "/",
            name: "viz.sid",
            same_site: Some(SameSite::Lax),
            max_age: Some(Duration::from_secs(Self::MAX_AGE)),
        }
    }
}

#[cfg(not(any(feature = "cookie-signed", feature = "cookie-private")))]
/// An interface for managing the cookies.
pub trait Cookieable {
    /// Gets the options of the cookie.
    fn options(&self) -> &CookieOptions;

    /// Gets a cookie from the cookies.
    fn get_cookie<'a>(&'a self, cookies: &'a Cookies) -> Option<Cookie<'a>> {
        cookies.get(self.options().name)
    }

    /// Deletes a cookie from the cookies.
    fn remove_cookie<'a>(&'a self, cookies: &'a Cookies) {
        cookies.remove(self.options().name)
    }

    /// Sets a cookie from the cookies.
    fn set_cookie<'a>(&'a self, cookies: &'a Cookies, value: impl Into<String>) {
        cookies.add(self.options().into_cookie(value))
    }
}

#[cfg(all(feature = "cookie-signed", not(feature = "cookie-private")))]
/// An interface for managing the `signed` cookies.
pub trait Cookieable {
    /// Gets the options of the cookie.
    fn options(&self) -> &CookieOptions;

    /// Gets a cookie from the cookies.
    fn get_cookie<'a>(&'a self, cookies: &'a Cookies) -> Option<Cookie<'a>> {
        cookies.signed_get(self.options().name)
    }

    /// Deletes a cookie from the cookies.
    fn remove_cookie<'a>(&'a self, cookies: &'a Cookies) {
        cookies.signed_remove(self.options().name)
    }

    /// Sets a cookie from the cookies.
    fn set_cookie<'a>(&'a self, cookies: &'a Cookies, value: impl Into<String>) {
        cookies.signed_add(self.options().into_cookie(value))
    }
}

#[cfg(all(feature = "cookie-private", not(feature = "cookie-signed")))]
/// An interface for managing the `private` cookies.
pub trait Cookieable {
    /// Gets the options of the cookie.
    fn options(&self) -> &CookieOptions;

    /// Gets a cookie from the cookies.
    fn get_cookie<'a>(&self, cookies: &'a Cookies) -> Option<Cookie<'a>> {
        cookies.private_get(self.options().name)
    }

    /// Deletes a cookie from the cookies.
    fn remove_cookie<'a>(&self, cookies: &'a Cookies) {
        cookies.private_remove(self.options().name)
    }

    /// Sets a cookie from the cookies.
    fn set_cookie<'a>(&'a self, cookies: &'a Cookies, value: impl Into<String>) {
        cookies.private_add(self.options().into_cookie(value))
    }
}

#[cfg(all(feature = "cookie-private", feature = "cookie-signed"))]
/// An interface for managing the cookies.
pub trait Cookieable {
    /// Gets the options of the cookie.
    fn options(&self) -> &CookieOptions;

    /// Gets a cookie from the cookies.
    fn get_cookie<'a>(&'a self, _: &'a Cookies) -> Option<Cookie<'a>> {
        panic!("Please choose a secure option, `cookie-signed` or `cookie-private`")
    }

    /// Deletes a cookie from the cookies.
    fn remove_cookie<'a>(&'a self, _: &'a Cookies) {
        panic!("Please choose a secure option, `cookie-signed` or `cookie-private`")
    }

    /// Sets a cookie from the cookies.
    fn set_cookie<'a>(&'a self, _: &'a Cookies, _: impl Into<String>) {
        panic!("Please choose a secure option, `cookie-signed` or `cookie-private`")
    }
}