Added Cookie expiration option/environment variable
The default expiration is 52 weeks.
This commit is contained in:
parent
5eda5d0604
commit
a408e753a8
|
|
@ -1287,6 +1287,7 @@ dependencies = [
|
||||||
"rocket_dyn_templates",
|
"rocket_dyn_templates",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"time",
|
||||||
"tokio",
|
"tokio",
|
||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
@ -1675,9 +1676,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "time"
|
name = "time"
|
||||||
version = "0.3.36"
|
version = "0.3.37"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885"
|
checksum = "35e7868883861bd0e56d9ac6efcaaca0d6d5d82a2a7ec8209ff492c07cf37b21"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"deranged",
|
"deranged",
|
||||||
"itoa",
|
"itoa",
|
||||||
|
|
@ -1696,9 +1697,9 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "time-macros"
|
name = "time-macros"
|
||||||
version = "0.2.18"
|
version = "0.2.19"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf"
|
checksum = "2834e6017e3e5e4b9834939793b282bc03b37a3336245fa820e35e233e2a85de"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"num-conv",
|
"num-conv",
|
||||||
"time-core",
|
"time-core",
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ rocket = { version = "0.5.1", features = ["tls"] }
|
||||||
serde = { version = "1.0.197", features = ["derive"] }
|
serde = { version = "1.0.197", features = ["derive"] }
|
||||||
serde_json = "1.0.114"
|
serde_json = "1.0.114"
|
||||||
tokio = { version = "1.36.0", features = ["full"] }
|
tokio = { version = "1.36.0", features = ["full"] }
|
||||||
|
time = "0.3.37"
|
||||||
|
|
||||||
[dependencies.rocket_dyn_templates]
|
[dependencies.rocket_dyn_templates]
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ services:
|
||||||
- 80:80
|
- 80:80
|
||||||
environment:
|
environment:
|
||||||
- DATABASE_URL=postgres://postgres:Password123@postgres/request_mirror_db
|
- DATABASE_URL=postgres://postgres:Password123@postgres/request_mirror_db
|
||||||
|
# Set COOKIE_EXPIRATION in weeks
|
||||||
|
# - COOKIE_EXPIRATION=52
|
||||||
depends_on:
|
depends_on:
|
||||||
postgres:
|
postgres:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
|
|
||||||
32
src/lib.rs
32
src/lib.rs
|
|
@ -47,6 +47,38 @@ pub fn establish_connection() -> PgConnection {
|
||||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
.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
|
/// Used to create a new client in the database. You need to pass a connection, the ip and client_id
|
||||||
///
|
///
|
||||||
/// Example:
|
/// Example:
|
||||||
|
|
|
||||||
10
src/main.rs
10
src/main.rs
|
|
@ -9,6 +9,7 @@ use rocket::outcome::Outcome;
|
||||||
use rocket_dyn_templates::Template;
|
use rocket_dyn_templates::Template;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use rocket::http::{Cookie, CookieJar, Status};
|
use rocket::http::{Cookie, CookieJar, Status};
|
||||||
|
use time::{Duration, OffsetDateTime};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use request_mirror::models::*;
|
use request_mirror::models::*;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
|
@ -61,7 +62,14 @@ impl<'r> FromRequest<'r> for RequestInfo {
|
||||||
let new_uuid = Uuid::new_v4().to_string();
|
let new_uuid = Uuid::new_v4().to_string();
|
||||||
println!("Creating new cookie");
|
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() {
|
let address = if req.client_ip().is_some() {
|
||||||
req.client_ip().unwrap().to_string()
|
req.client_ip().unwrap().to_string()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue