yaiko-core 0.1.1

A modern, production-ready fullstack web framework for Rust
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use crate::{Middleware, Request, Response};
use async_trait::async_trait;
use cookie::{Cookie, SameSite};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use uuid::Uuid;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Session {
    pub id: String,
    pub data: HashMap<String, serde_json::Value>,
    pub created_at: chrono::DateTime<chrono::Utc>,
    pub expires_at: chrono::DateTime<chrono::Utc>,
}

impl Session {
    pub fn new(duration: chrono::Duration) -> Self {
        let now = chrono::Utc::now();
        Session {
            id: Uuid::new_v4().to_string(),
            data: HashMap::new(),
            created_at: now,
            expires_at: now + duration,
        }
    }

    pub fn get<T: for<'de> Deserialize<'de>>(&self, key: &str) -> Option<T> {
        self.data
            .get(key)
            .and_then(|value| serde_json::from_value(value.clone()).ok())
    }

    pub fn set<T: Serialize>(&mut self, key: &str, value: T) -> Result<(), serde_json::Error> {
        self.data
            .insert(key.to_string(), serde_json::to_value(value)?);
        Ok(())
    }

    pub fn remove(&mut self, key: &str) -> Option<serde_json::Value> {
        self.data.remove(key)
    }

    pub fn clear(&mut self) {
        self.data.clear();
    }

    pub fn is_expired(&self) -> bool {
        chrono::Utc::now() > self.expires_at
    }

    pub fn renew_id(&mut self) -> String {
        let previous = self.id.clone();
        self.id = Uuid::new_v4().to_string();
        previous
    }

    pub fn extend(&mut self, duration: chrono::Duration) {
        self.expires_at = chrono::Utc::now() + duration;
    }
}

#[derive(Debug, Clone)]
pub struct SessionHandle {
    inner: Arc<Mutex<SessionState>>,
}

#[derive(Debug, Clone)]
struct SessionState {
    session: Session,
    destroy: bool,
    previous_ids: Vec<String>,
}

#[derive(Debug, Clone)]
struct PersistedSession {
    session: Session,
    destroy: bool,
    previous_ids: Vec<String>,
}

impl SessionHandle {
    pub fn new(session: Session) -> Self {
        Self {
            inner: Arc::new(Mutex::new(SessionState {
                session,
                destroy: false,
                previous_ids: Vec::new(),
            })),
        }
    }

    pub fn id(&self) -> String {
        self.inner.lock().unwrap().session.id.clone()
    }

    pub fn get<T: for<'de> Deserialize<'de>>(&self, key: &str) -> Option<T> {
        self.inner.lock().unwrap().session.get(key)
    }

    pub fn set<T: Serialize>(&self, key: &str, value: T) -> Result<(), serde_json::Error> {
        self.inner.lock().unwrap().session.set(key, value)
    }

    pub fn remove(&self, key: &str) -> Option<serde_json::Value> {
        self.inner.lock().unwrap().session.remove(key)
    }

    pub fn clear(&self) {
        self.inner.lock().unwrap().session.clear();
    }

    pub fn destroy(&self) {
        let mut state = self.inner.lock().unwrap();
        state.destroy = true;
        state.session.clear();
    }

    pub fn rotate_id(&self) {
        let mut state = self.inner.lock().unwrap();
        let previous = state.session.renew_id();
        state.previous_ids.push(previous);
    }

    pub fn extend(&self, duration: chrono::Duration) {
        self.inner.lock().unwrap().session.extend(duration);
    }

    fn snapshot(&self) -> PersistedSession {
        let state = self.inner.lock().unwrap();
        PersistedSession {
            session: state.session.clone(),
            destroy: state.destroy,
            previous_ids: state.previous_ids.clone(),
        }
    }
}

#[async_trait]
pub trait SessionStore: Send + Sync {
    async fn get(
        &self,
        id: &str,
    ) -> Result<Option<Session>, Box<dyn std::error::Error + Send + Sync>>;
    async fn set(
        &self,
        session: Session,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
    async fn delete(&self, id: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
    async fn cleanup(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
}

pub struct MemorySessionStore {
    sessions: Arc<tokio::sync::RwLock<HashMap<String, Session>>>,
}

impl MemorySessionStore {
    pub fn new() -> Self {
        MemorySessionStore {
            sessions: Arc::new(tokio::sync::RwLock::new(HashMap::new())),
        }
    }
}

#[async_trait]
impl SessionStore for MemorySessionStore {
    async fn get(
        &self,
        id: &str,
    ) -> Result<Option<Session>, Box<dyn std::error::Error + Send + Sync>> {
        let sessions = self.sessions.read().await;
        Ok(sessions.get(id).cloned())
    }

    async fn set(
        &self,
        session: Session,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut sessions = self.sessions.write().await;
        sessions.insert(session.id.clone(), session);
        Ok(())
    }

    async fn delete(&self, id: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut sessions = self.sessions.write().await;
        sessions.remove(id);
        Ok(())
    }

    async fn cleanup(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut sessions = self.sessions.write().await;
        let now = chrono::Utc::now();
        sessions.retain(|_, session| session.expires_at > now);
        Ok(())
    }
}

pub struct SessionMiddleware {
    store: Arc<dyn SessionStore>,
    cookie_name: String,
    session_duration: chrono::Duration,
    secure_cookies: Option<bool>,
    same_site: SameSite,
}

impl SessionMiddleware {
    pub fn new(store: Arc<dyn SessionStore>) -> Self {
        let cleanup_store = store.clone();
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(300));
            loop {
                interval.tick().await;
                if let Err(error) = cleanup_store.cleanup().await {
                    tracing::error!("Session cleanup failed: {}", error);
                }
            }
        });

        SessionMiddleware {
            store,
            cookie_name: "yaiko_session".to_string(),
            session_duration: chrono::Duration::hours(24),
            secure_cookies: None,
            same_site: SameSite::Lax,
        }
    }

    pub fn cookie_name(mut self, name: &str) -> Self {
        self.cookie_name = name.to_string();
        self
    }

    pub fn duration(mut self, duration: chrono::Duration) -> Self {
        self.session_duration = duration;
        self
    }

    pub fn secure(mut self, secure: bool) -> Self {
        self.secure_cookies = Some(secure);
        self
    }

    pub fn same_site(mut self, same_site: SameSite) -> Self {
        self.same_site = same_site;
        self
    }

    fn cookie_is_secure(&self) -> bool {
        self.secure_cookies.unwrap_or_else(|| {
            std::env::var("APP_ENV").unwrap_or_default() == "production"
        })
    }

    fn session_cookie(&self, session: &Session) -> Cookie<'static> {
        Cookie::build(self.cookie_name.clone(), session.id.clone())
            .http_only(true)
            .secure(self.cookie_is_secure())
            .same_site(self.same_site)
            .path("/")
            .max_age(cookie::time::Duration::seconds(
                self.session_duration.num_seconds(),
            ))
            .finish()
    }

    fn expired_cookie(&self) -> Cookie<'static> {
        Cookie::build(self.cookie_name.clone(), "")
            .http_only(true)
            .secure(self.cookie_is_secure())
            .same_site(self.same_site)
            .path("/")
            .max_age(cookie::time::Duration::seconds(0))
            .finish()
    }
}

#[async_trait]
impl Middleware for SessionMiddleware {
    async fn handle(
        &self,
        mut req: Request,
        next: Arc<dyn crate::Handler>,
    ) -> Result<Response, Box<dyn std::error::Error + Send + Sync>> {
        let incoming_session_id = req
            .header("cookie")
            .and_then(|cookie_header| {
                cookie_header.split(';').find_map(|cookie| {
                    Cookie::parse(cookie.trim())
                        .ok()
                        .filter(|parsed| parsed.name() == self.cookie_name)
                        .map(|parsed| parsed.value().to_string())
                })
            });

        let loaded_session = if let Some(id) = incoming_session_id.as_deref() {
            match self.store.get(id).await? {
                Some(session) if !session.is_expired() => session,
                _ => Session::new(self.session_duration),
            }
        } else {
            Session::new(self.session_duration)
        };

        let handle = SessionHandle::new(loaded_session);
        req.session = Some(handle.clone());

        let mut response = next.handle(req).await?;
        let persisted = handle.snapshot();

        for previous_id in &persisted.previous_ids {
            self.store.delete(previous_id).await?;
        }

        if persisted.destroy {
            if let Some(original_id) = incoming_session_id.as_deref() {
                self.store.delete(original_id).await?;
            }
            self.store.delete(&persisted.session.id).await?;
            response = response.set_cookie_raw(&self.expired_cookie().to_string());
            return Ok(response);
        }

        self.store.set(persisted.session.clone()).await?;
        response = response.set_cookie_raw(&self.session_cookie(&persisted.session).to_string());
        Ok(response)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::StatusCode;
    use hyper::Body;

    #[tokio::test]
    async fn session_mutations_persist_across_requests() {
        let store = Arc::new(MemorySessionStore::new());
        let middleware = SessionMiddleware::new(store.clone()).secure(false);

        let login = Arc::new(|req: Request| async move {
            let session = req.session.as_ref().unwrap();
            session.set("user_id", "abc123").unwrap();
            Ok(Response::new().text("ok"))
        });

        let first_req = Request::from_hyper(
            hyper::Request::builder()
                .method("GET")
                .uri("/login")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
        let login_response = middleware.handle(first_req, login).await.unwrap();
        let set_cookie = login_response.headers.get("Set-Cookie").unwrap().clone();

        let me = Arc::new(|req: Request| async move {
            let user_id = req
                .session
                .as_ref()
                .and_then(|session| session.get::<String>("user_id"))
                .unwrap_or_default();
            Ok(Response::new().text(&user_id))
        });

        let second_req = Request::from_hyper(
            hyper::Request::builder()
                .method("GET")
                .uri("/me")
                .header("cookie", set_cookie)
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
        let second_response = middleware.handle(second_req, me).await.unwrap();
        let body = hyper::body::to_bytes(second_response.body).await.unwrap();

        assert_eq!(&body[..], b"abc123");
    }

    #[tokio::test]
    async fn destroyed_sessions_clear_cookie_and_store() {
        let store = Arc::new(MemorySessionStore::new());
        let middleware = SessionMiddleware::new(store.clone()).secure(false);

        let login = Arc::new(|req: Request| async move {
            req.session.as_ref().unwrap().set("user_id", "abc123").unwrap();
            Ok(Response::new().status(StatusCode::OK))
        });
        let first_req = Request::from_hyper(
            hyper::Request::builder()
                .method("GET")
                .uri("/login")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
        let login_response = middleware.handle(first_req, login).await.unwrap();
        let set_cookie = login_response.headers.get("Set-Cookie").unwrap().clone();

        let logout = Arc::new(|req: Request| async move {
            req.session.as_ref().unwrap().destroy();
            Ok(Response::new().status(StatusCode::OK))
        });
        let logout_req = Request::from_hyper(
            hyper::Request::builder()
                .method("POST")
                .uri("/logout")
                .header("cookie", &set_cookie)
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
        let logout_response = middleware.handle(logout_req, logout).await.unwrap();

        let cookie = logout_response.headers.get("Set-Cookie").unwrap();
        assert!(cookie.contains("Max-Age=0"));
    }
}