diff --git a/Cargo.lock b/Cargo.lock index 63e3e32..e23a141 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1287,6 +1287,7 @@ dependencies = [ "rocket_dyn_templates", "serde", "serde_json", + "time", "tokio", "uuid", ] @@ -1675,9 +1676,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.36" +version = "0.3.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" +checksum = "35e7868883861bd0e56d9ac6efcaaca0d6d5d82a2a7ec8209ff492c07cf37b21" dependencies = [ "deranged", "itoa", @@ -1696,9 +1697,9 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" +checksum = "2834e6017e3e5e4b9834939793b282bc03b37a3336245fa820e35e233e2a85de" dependencies = [ "num-conv", "time-core", diff --git a/Cargo.toml b/Cargo.toml index d0837c0..55522af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ rocket = { version = "0.5.1", features = ["tls"] } serde = { version = "1.0.197", features = ["derive"] } serde_json = "1.0.114" tokio = { version = "1.36.0", features = ["full"] } +time = "0.3.37" [dependencies.rocket_dyn_templates] version = "0.2.0" diff --git a/docker-compose.yml b/docker-compose.yml index 87fe2f8..c885a7f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,6 +17,8 @@ services: - 80:80 environment: - DATABASE_URL=postgres://postgres:Password123@postgres/request_mirror_db + # Set COOKIE_EXPIRATION in weeks + # - COOKIE_EXPIRATION=52 depends_on: postgres: condition: service_healthy diff --git a/src/lib.rs b/src/lib.rs index 65fbbd8..22d663e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,6 +47,38 @@ pub fn establish_connection() -> PgConnection { .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) } +/// returns default cookie expiration in weeks +/// +/// This value can be set by modifying an environment variable or setting in the .env file. +/// +/// Example: +/// +/// ```rust,ignore +/// use request_mirror::establish_connection; +/// +/// let connection = establish_connection(); +/// ``` +pub fn cookie_expiration() -> i64 { + + let key: &str = "COOKIE_EXPIRATION"; + let expiration: i64; + match env::var(key) { + Ok(val) => { + match val.parse::() { + Ok(v) => expiration = v, + Err(e) => expiration = 52 + } + }, + Err(_e) => { + dotenv().ok(); + + expiration = 52; + } + } + + expiration +} + /// Used to create a new client in the database. You need to pass a connection, the ip and client_id /// /// Example: diff --git a/src/main.rs b/src/main.rs index c34b5d1..05a083f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ use rocket::outcome::Outcome; use rocket_dyn_templates::Template; use serde::Serialize; use rocket::http::{Cookie, CookieJar, Status}; +use time::{Duration, OffsetDateTime}; use uuid::Uuid; use request_mirror::models::*; use diesel::prelude::*; @@ -61,7 +62,14 @@ impl<'r> FromRequest<'r> for RequestInfo { let new_uuid = Uuid::new_v4().to_string(); println!("Creating new cookie"); - req_cookies.add(Cookie::new("mirror-id", new_uuid.clone())); + let mut cookie = Cookie::new("mirror-id", new_uuid.clone()); + let mut now = OffsetDateTime::now_utc(); + + now += Duration::weeks(cookie_expiration()); // Default expiration is 52 weeks + + cookie.set_expires(now); + + req_cookies.add(cookie); let address = if req.client_ip().is_some() { req.client_ip().unwrap().to_string()