Compare commits

...

2 Commits

Author SHA1 Message Date
Camerin Figueroa e3a2e9d632 Cargo Updates 2025-01-24 16:32:54 -05:00
Camerin Figueroa a408e753a8 Added Cookie expiration option/environment variable
The default expiration is 52 weeks.
2025-01-24 15:53:39 -05:00
5 changed files with 386 additions and 254 deletions

581
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,21 +6,22 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = "0.4.34"
diesel = { version = "2.1.4", features = ["postgres", "chrono"] }
chrono = "0.4.39"
diesel = { version = "2.2.6", features = ["postgres", "chrono"] }
dotenvy = "0.15.7"
regex = "1.10.5"
regex = "1.11.1"
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"] }
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.137"
tokio = { version = "1.43.0", features = ["full"] }
time = "0.3.37"
[dependencies.rocket_dyn_templates]
version = "0.2.0"
features = ["handlebars"]
[dependencies.uuid]
version = "1.7.0"
version = "1.12.1"
features = [
"v4", # Lets you generate random UUIDs
"fast-rng", # Use a faster (but still sufficiently random) RNG

View File

@ -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

View File

@ -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::<i64>() {
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:

View File

@ -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()