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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use ring::{hmac::{self, Key}, rand};
use crate::{
Error,
http::{Request, Response},
session::{SessionCreator, Session}
};
use std::collections::HashMap;
use std::time::Duration;
use cookie::Cookie;
use chrono::{DateTime, Utc};
use base64::{Engine, engine::general_purpose};
/// Enum to indicate a same site policy in the cookie builder
#[derive(Clone)]
pub enum SameSite {
Strict,
Lax,
None
}
impl SameSite {
fn to_cookie_same_site(&self) -> cookie::SameSite {
match self {
SameSite::Strict => cookie::SameSite::Strict,
SameSite::Lax => cookie::SameSite::Lax,
SameSite::None => cookie::SameSite::None
}
}
}
/// Session implementation, cookie based.
#[derive(Clone)]
pub struct CookieSession {
key: Key,
cookie_name: String,
path: Option<String>,
domain: Option<String>,
expires: Option<DateTime<Utc>>,
max_age: Option<Duration>,
secure: Option<bool>,
http_only: Option<bool>,
same_site: Option<SameSite>,
force_failure: bool
}
impl CookieSession {
/// Creates a new cookie session creator
pub fn new() -> Self {
let rng = rand::SystemRandom::new();
CookieSession {
key: Key::generate(hmac::HMAC_SHA256, &rng).map_err(Error::Ring).unwrap(),
cookie_name: "cataclysm-session".to_string(),
path: None,
domain: None,
expires: None,
max_age: None,
secure: None,
http_only: None,
same_site: None,
force_failure: false
}
}
/// Sets a custom `name` for the cookie.
///
/// By default, `cataclysm-session` is used.
///
/// ```rust,no_run
/// use cataclysm::session::CookieSession;
///
/// let cookie_session = CookieSession::new()
/// .name("my-app-session");
/// ```
pub fn name<A: Into<String>>(mut self, name: A) -> Self {
self.cookie_name = name.into();
self
}
/// Sets a custom `Key` for cookie signature
///
/// ```rust,no_run
/// use cataclysm::session::CookieSession;
///
/// let cookie_session = CookieSession::new()
/// .secret("really secret!");
/// ```
///
/// If no secret is provided, a random key will be used (generated by ring).
pub fn secret<A: AsRef<[u8]>>(mut self, secret: A) -> Self {
self.key = hmac::Key::new(hmac::HMAC_SHA256, secret.as_ref());
self
}
/// Sets a custom `path` for generated cookies
///
/// ```rust,no_run
/// use cataclysm::session::CookieSession;
///
/// let cookie_session = CookieSession::new()
/// .path("/applies/only/here");
/// ```
pub fn path<A: Into<String>>(mut self, path: A) -> Self {
self.path = Some(path.into());
self
}
/// Sets a custom `domain` for generated cookies
///
/// ```rust,no_run
/// use cataclysm::session::CookieSession;
///
/// let cookie_session = CookieSession::new()
/// .domain("example.com");
/// ```
pub fn domain<A: Into<String>>(mut self, domain: A) -> Self {
self.domain = Some(domain.into());
self
}
/// Sets a duration for the cookie (that is, using the `expires` field)
///
/// You should try to avoid this method, and use `max_age` instead.
///
/// ```rust,no_run
/// use cataclysm::session::CookieSession;
/// use chrono::{DateTime, Utc, Duration};
///
/// let cookie_session = CookieSession::new().expires(Utc::now() + Duration::weeks(52));
/// ```
///
/// Please note that the "expires" field will not update the provided date as time goes by. Use the [max_age](CookieSession::max_age) method instead.
pub fn expires(mut self, expires: DateTime<Utc>) -> Self {
self.expires = Some(expires);
self
}
/// Sets a duration for the cookie (that is, using the `max_age` field)
///
/// ```rust,no_run
/// use cataclysm::session::CookieSession;
/// use std::time::Duration;
///
/// let cookie_session = CookieSession::new().max_age(Duration::from_secs(3_600 * 6));
/// ```
pub fn max_age(mut self, max_age: Duration) -> Self {
self.max_age = Some(max_age);
self
}
/// Sets the `secure` field in the cookie
///
/// ```rust,no_run
/// use cataclysm::session::CookieSession;
///
/// let cookie_session = CookieSession::new().secure(true);
/// ```
pub fn secure(mut self, secure: bool) -> Self {
self.secure = Some(secure);
self
}
/// Sets the `http_only` field in the cookie
///
/// ```rust,no_run
/// use cataclysm::session::CookieSession;
///
/// let cookie_session = CookieSession::new().http_only(true);
/// ```
pub fn http_only(mut self, http_only: bool) -> Self {
self.http_only = Some(http_only);
self
}
/// Sets the `same_site` field in the cookie
///
/// ```rust,no_run
/// use cataclysm::session::{CookieSession, SameSite};
///
/// let cookie_session = CookieSession::new().same_site(SameSite::Lax);
/// ```
pub fn same_site(mut self, same_site: SameSite) -> Self {
self.same_site = Some(same_site);
self
}
/// Helper function to extract a session from a cookie
fn build_from_req(&self, req: &Request) -> Result<Option<Session>, Error> {
// we can have multiple cookies, so we try for each one
let empty = Vec::new();
let cookie_headers = vec![
req.headers.get("Cookie").unwrap_or_else(|| &empty),
req.headers.get("cookie").unwrap_or_else(|| &empty)
].into_iter().flatten();
for cookie_header in cookie_headers {
for single_cookie in cookie_header.split("; ") {
let cookie = Cookie::parse_encoded(single_cookie).map_err(|e| Error::custom(format!("{}", e)))?;
if cookie.name() == self.cookie_name {
let value = cookie.value();
// The hmac value is at least 44 bytes
if value.len() < 44 {
return Err(Error::custom("length of cookie cannot contain even the hmac value"));
} else {
// I know these unwraps look unsafe, but trust me, they are, TODO FIX with let Some
let signature = value.get(0..44).unwrap();
let content = value.get(44..value.len()).unwrap();
// First, we try to decode the content
let values = serde_json::from_str(content).map_err(|e| Error::custom(format!("{}", e)))?;
let tag = general_purpose::STANDARD.decode(signature).map_err(|e| Error::custom(format!("{}", e)))?;
hmac::verify(&self.key, content.as_bytes(), &tag).map_err(|e| Error::custom(format!("{}", e)))?;
return Ok(Some(Session::new_with_values(self.clone(), values)))
}
}
}
}
Ok(None)
}
}
impl SessionCreator for CookieSession {
fn create(&self, req: &Request) -> Result<Session, Error> {
match self.build_from_req(req) {
Ok(Some(session)) => Ok(session),
Ok(None) => {
#[cfg(feature = "full_log")]
log::debug!("cookie not found among request headers");
Ok(Session::new(self.clone()))
},
Err(e) => {
if self.force_failure {
Err(e)
} else {
#[cfg(feature = "full_log")]
log::debug!("error while creating session: {}", e);
return Ok(Session::new(self.clone()))
}
}
}
}
fn apply(&self, values: &HashMap<String, String>, mut res: Response) -> Response {
let content = serde_json::to_string(values).unwrap();
let signature = general_purpose::STANDARD.encode(hmac::sign(&self.key, content.as_bytes()).as_ref());
let cookie_builder = Cookie::build((&self.cookie_name, format!("{}{}", signature, content)));
let cookie_builder = if let Some(path) = &self.path {
cookie_builder.path(path)
} else {
cookie_builder
};
let cookie_builder = if let Some(domain) = &self.domain {
cookie_builder.domain(domain)
} else {
cookie_builder
};
let cookie_builder = if let Some(expires) = &self.expires {
match cookie::time::OffsetDateTime::from_unix_timestamp(expires.timestamp()) {
Ok(v) => cookie_builder.expires(v),
Err(e) => {
log::error!("could not set expires flag to cookie, {}", e);
cookie_builder
}
}
} else {
cookie_builder
};
let cookie_builder = if let Some(max_age) = &self.max_age {
let max_age = match max_age.clone().try_into() {
Ok(v) => v,
Err(e) => {
log::error!("failed to set max-age to cookie ({}), loading safety default 3,600 seconds", e);
cookie::time::Duration::seconds(3_600)
}
};
cookie_builder.max_age(max_age)
} else {
cookie_builder
};
let cookie_builder = if let Some(secure) = &self.secure {
cookie_builder.secure(*secure)
} else {
cookie_builder
};
let cookie_builder = if let Some(http_only) = &self.http_only {
cookie_builder.http_only(*http_only)
} else {
cookie_builder
};
let cookie_builder = if let Some(same_site) = &self.same_site {
cookie_builder.same_site(same_site.to_cookie_same_site())
} else {
cookie_builder
};
let cookie = cookie_builder.build();
res = res.header("Set-Cookie", format!("{}", cookie.encoded()));
res
}
}