diff options
author | Joris | 2023-08-12 20:05:09 +0200 |
---|---|---|
committer | Joris | 2023-08-12 20:05:09 +0200 |
commit | 8c689db1c8fa06ddb9119e626e7b1149f3493905 (patch) | |
tree | cb4029776162387a03a7a131ceee3628ed1ba4ef /src/controller/utils.rs | |
parent | 459016e70dd4933a8082d27748097de81a3e53ff (diff) |
Sign cookie with secret key
Diffstat (limited to 'src/controller/utils.rs')
-rw-r--r-- | src/controller/utils.rs | 80 |
1 files changed, 20 insertions, 60 deletions
diff --git a/src/controller/utils.rs b/src/controller/utils.rs index 26db765..fb1d9ad 100644 --- a/src/controller/utils.rs +++ b/src/controller/utils.rs @@ -1,23 +1,13 @@ use hyper::header::{ - HeaderName, HeaderValue, CACHE_CONTROL, CONTENT_TYPE, LOCATION, SET_COOKIE, + HeaderName, HeaderValue, CACHE_CONTROL, CONTENT_TYPE, LOCATION, }; use hyper::{Body, Response, StatusCode}; use std::collections::HashMap; use tera::{Context, Tera}; use tokio::fs::File; use tokio_util::codec::{BytesCodec, FramedRead}; -use uuid::Uuid; use crate::controller::error; -use crate::model::config::Config; - -pub fn with_header( - response: Response<Body>, - name: HeaderName, - value: &str, -) -> Response<Body> { - with_headers(response, vec![(name, value)]) -} pub fn with_headers( response: Response<Body>, @@ -31,40 +21,6 @@ pub fn with_headers( response } -pub fn with_login_cookie( - config: Config, - login_token: Uuid, - response: Response<Body>, -) -> Response<Body> { - let cookie = format!( - "TOKEN={}; SameSite=Strict; HttpOnly; Max-Age=86400{}", - login_token, - if config.secure_cookies { - "; Secure" - } else { - "" - } - ); - - with_header(response, SET_COOKIE, &cookie) -} - -pub fn with_logout_cookie( - config: Config, - response: Response<Body>, -) -> Response<Body> { - let cookie = format!( - "TOKEN=; SameSite=Strict; HttpOnly; Max-Age=0{}", - if config.secure_cookies { - "; Secure" - } else { - "" - } - ); - - with_header(response, SET_COOKIE, &cookie) -} - pub fn template( assets: &HashMap<String, String>, templates: &Tera, @@ -74,24 +30,28 @@ pub fn template( let mut context = context; context.insert("assets", assets); - let response = match templates.render(path, &context) { - Ok(template) => Response::new(template.into()), - Err(err) => Response::new( - error::template( - assets, - templates, - "Erreur serveur", - &format!( - "Erreur lors de la préparation de la page : {:?}", - err - ), - ) - .into(), + match templates.render(path, &context) { + Ok(template) => with_headers( + Response::new(template.into()), + vec![(CONTENT_TYPE, "text/html"), (CACHE_CONTROL, "no-cache")], ), - }; + Err(err) => server_error( + assets, + templates, + &format!("Erreur lors de la préparation de la page : {:?}", err), + ), + } +} +fn server_error( + assets: &HashMap<String, String>, + templates: &Tera, + msg: &str, +) -> Response<Body> { with_headers( - response, + Response::new( + error::template(assets, templates, "Erreur serveur", msg).into(), + ), vec![(CONTENT_TYPE, "text/html"), (CACHE_CONTROL, "no-cache")], ) } |