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
use std::time::Duration;

use crate::web::cookie::{Cookie, CookieJar, CookieKey, SameSite};

/// Cookie security for session.
pub enum CookieSecurity {
    /// Use the raw cookie value.
    ///
    /// **NOTE: It is not recommended to be used in a production environment.**
    Plain,

    /// Use the key to encrypt the cookie value.
    Private(CookieKey),

    /// Sign the cookie value with the key.
    Signed(CookieKey),
}

/// Cookie configuration for session.
pub struct CookieConfig {
    security: CookieSecurity,
    name: String,
    path: String,
    domain: Option<String>,
    secure: bool,
    http_only: bool,
    max_age: Option<Duration>,
    same_site: Option<SameSite>,
}

impl Default for CookieConfig {
    fn default() -> Self {
        Self {
            security: CookieSecurity::Plain,
            name: "poem-session".to_string(),
            path: "/".to_string(),
            domain: None,
            secure: true,
            http_only: true,
            max_age: None,
            same_site: None,
        }
    }
}

impl CookieConfig {
    /// Create a new `plain` CookieSession.
    pub fn new() -> Self {
        Default::default()
    }

    /// Create a new `private` CookieSession.
    pub fn private(key: CookieKey) -> Self {
        Self {
            security: CookieSecurity::Private(key),
            ..Default::default()
        }
    }

    /// Create a new `signed` CookieSession.
    pub fn signed(key: CookieKey) -> Self {
        Self {
            security: CookieSecurity::Signed(key),
            ..Default::default()
        }
    }

    /// Sets the `name` to the session cookie.
    #[must_use]
    pub fn name(self, value: impl Into<String>) -> Self {
        Self {
            name: value.into(),
            ..self
        }
    }

    /// Sets the `Path` to the session cookie. Default is `/`.
    #[must_use]
    pub fn path(self, value: impl Into<String>) -> Self {
        Self {
            path: value.into(),
            ..self
        }
    }

    /// Sets the `Domain` to the session cookie.
    #[must_use]
    pub fn domain(self, value: impl Into<String>) -> Self {
        Self {
            domain: Some(value.into()),
            ..self
        }
    }

    /// Sets the `Secure` to the session cookie. Default is `true`.
    #[must_use]
    pub fn secure(self, value: bool) -> Self {
        Self {
            secure: value,
            ..self
        }
    }

    /// Sets the `HttpOnly` to the session cookie. Default is `true`.
    #[must_use]
    pub fn http_only(self, value: bool) -> Self {
        Self {
            http_only: value,
            ..self
        }
    }

    /// Sets the `SameSite` to the session cookie.
    #[must_use]
    pub fn same_site(self, value: impl Into<Option<SameSite>>) -> Self {
        Self {
            same_site: value.into(),
            ..self
        }
    }

    /// Sets the `MaxAge` to the session cookie.
    #[must_use]
    pub fn max_age(self, value: impl Into<Option<Duration>>) -> Self {
        Self {
            max_age: value.into(),
            ..self
        }
    }

    /// Returns the TTL(time-to-live) of the cookie.
    #[inline]
    pub(crate) fn ttl(&self) -> Option<Duration> {
        self.max_age
    }

    /// Set the cookie value to `CookieJar`.
    pub fn set_cookie_value(&self, cookie_jar: &CookieJar, value: &str) {
        let mut cookie = Cookie::new_with_str(&self.name, value);

        cookie.set_path(&self.path);

        if let Some(domain) = &self.domain {
            cookie.set_domain(domain);
        }

        cookie.set_secure(self.secure);
        cookie.set_http_only(self.http_only);

        if let Some(max_age) = &self.max_age {
            cookie.set_max_age(*max_age);
        }

        cookie.set_same_site(self.same_site);

        match &self.security {
            CookieSecurity::Plain => cookie_jar.add(cookie),
            CookieSecurity::Private(key) => cookie_jar.private_with_key(key).add(cookie),
            CookieSecurity::Signed(key) => cookie_jar.signed_with_key(key).add(cookie),
        }
    }

    /// Remove the cookie from `CookieJar`.
    pub fn remove_cookie(&self, cookie_jar: &CookieJar) {
        match &self.security {
            CookieSecurity::Plain => cookie_jar.remove(&self.name),
            CookieSecurity::Private(key) => cookie_jar.private_with_key(key).remove(&self.name),
            CookieSecurity::Signed(key) => cookie_jar.signed_with_key(key).remove(&self.name),
        }
    }

    /// Gets the cookie value from `CookieJar`.
    pub fn get_cookie_value(&self, cookie_jar: &CookieJar) -> Option<String> {
        let cookie = match &self.security {
            CookieSecurity::Plain => cookie_jar.get(&self.name),
            CookieSecurity::Private(key) => cookie_jar.private_with_key(key).get(&self.name),
            CookieSecurity::Signed(key) => cookie_jar.signed_with_key(key).get(&self.name),
        };
        cookie.map(|cookie| cookie.value_str().to_string())
    }
}