From 3f4e3d3ed132a94ad97b37f2d82d326c7e59933f Mon Sep 17 00:00:00 2001 From: didier amyot Date: Sun, 26 Feb 2023 11:51:52 -0500 Subject: [PATCH] I need to stop working on this shit. --- Cargo.lock | 1 + Cargo.toml | 3 +- .../migrations/sql/create_tables_001.sql | 10 ++++- src/db/_ledger_internal.rs | 39 ++++++++++++++++++ src/db/mod.rs | 1 + src/services/_authentication.rs | 40 +++++++++++++++++++ src/services/mod.rs | 1 + 7 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/db/_ledger_internal.rs create mode 100644 src/services/_authentication.rs diff --git a/Cargo.lock b/Cargo.lock index cb98961..fb00acd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -742,6 +742,7 @@ dependencies = [ "actix-web-lab", "apache-avro", "async-channel", + "base64 0.21.0", "clap", "clap-verbosity-flag", "cookie", diff --git a/Cargo.toml b/Cargo.toml index 4826441..fe75cfa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,10 +30,11 @@ minijinja-autoreload = "0.30.2" futures = "0.3.26" actix-files = "0.6.2" async-channel = "1.8.0" +base64 = "0.21.0" [dependencies.apache-avro] version = "0.14.0" features = ["zstandard"] [dependencies.cookie] -features = ["secure", "percent-encode"] \ No newline at end of file +features = ["secure", "percent-encode"] diff --git a/resources/migrations/sql/create_tables_001.sql b/resources/migrations/sql/create_tables_001.sql index 98517c1..78a6d40 100644 --- a/resources/migrations/sql/create_tables_001.sql +++ b/resources/migrations/sql/create_tables_001.sql @@ -1,4 +1,12 @@ -PRAGMA journal_mode=WAL; +PRAGMA journal_mode= WAL; + +CREATE TABLE IF NOT EXISTS ledger_internal +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + creation_time DATETIME DEFAULT CURRENT_TIMESTAMP, + cookie_secret TEXT NOT NULL + +); CREATE TABLE IF NOT EXISTS users ( diff --git a/src/db/_ledger_internal.rs b/src/db/_ledger_internal.rs new file mode 100644 index 0000000..337a2f1 --- /dev/null +++ b/src/db/_ledger_internal.rs @@ -0,0 +1,39 @@ +use rusqlite::{Connection, params, Row, Transaction, Result}; +use crate::util::{CustomResult, Error}; +use base64::{Engine as _, engine::general_purpose}; + +struct LedgerInternal {} + +impl LedgerInternal { + fn _decode_cookie_secret(row: &Row) -> Result> { + let encoded: String = row.get(0)?; + let ret = general_purpose::STANDARD.decode(encoded).unwrap(); + Ok(ret) + } + + pub fn get_cookie_secret(&self, tx: &Transaction) -> CustomResult> { + let q = "SELECT cookie_secret + FROM ledger_internal + ORDER BY cookie_secret DESC + LIMIT 1"; + tx.query_row(q, params![], Self::_decode_cookie_secret) + .map_err(Error::SqlError) + } + + pub fn set_cookie_secret(&self, tx: &Transaction, secret: &Vec) { + let q = "CREATE TEMPORARY TABLE foo + AS SELECT * FROM ledger_internal + ORDER BY cookie_secret DESC + LIMIT 1"; + } +} + +#[cfg(test)] +mod test_ledger_internal { + use super::*; + + #[test] + fn test_foo() { + + } +} diff --git a/src/db/mod.rs b/src/db/mod.rs index a83d3e8..d36f072 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -5,6 +5,7 @@ mod _interests; mod _securities; mod _test_utils; mod _users; +mod _ledger_internal; pub mod external_id { pub use super::_external_id::*; diff --git a/src/services/_authentication.rs b/src/services/_authentication.rs new file mode 100644 index 0000000..4b6829c --- /dev/null +++ b/src/services/_authentication.rs @@ -0,0 +1,40 @@ +use actix_web::HttpRequest; +use cookie::{CookieJar, Cookie, Key}; +use cookie::time::OffsetDateTime; + +struct SessionService { + key: Key, +} + +impl SessionService { + fn new() -> SessionService{ + let super_secret_not_hardcoded_at_all: [u8; 32] = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]; + SessionService{key: Key::derive_from(&super_secret_not_hardcoded_at_all)} + } + + fn create_login_cookie(&self, user_id: &String ) -> Cookie{ + let mut jar = CookieJar::new(); + jar.private_mut(& self.key).add(Cookie::new("ledger-user-id", user_id.clone())); + jar.get("ledger-user-id").unwrap_or(& Cookie::new("ledger-user-id", "")).clone() + } + + fn _decrypt_cookie(&self, cookie: Cookie<'static>) -> Option{ + let mut jar = CookieJar::new(); + jar.private(& self.key).decrypt(cookie) + } + + // todo: this service should not be aware of http requests. + // basically, it should be easy to migrate of + // web framework + fn get_login(&self, req: &HttpRequest) -> Option { + req.cookie("ledger-user-id") + .and_then(|c|self._decrypt_cookie(c)).map(|c| c.value().to_string()) + } + + fn logout(&self) -> Cookie{ + let mut cookie = Cookie::new("ledger-user-id", ""); + let epoch = OffsetDateTime::from_unix_timestamp_nanos(0).unwrap(); + cookie.set_expires(epoch); + cookie + } +} \ No newline at end of file diff --git a/src/services/mod.rs b/src/services/mod.rs index 97b5920..5612d62 100644 --- a/src/services/mod.rs +++ b/src/services/mod.rs @@ -4,6 +4,7 @@ use crate::services::_users::UserService; mod _transactions; mod _users; +mod _authentication; pub struct ServiceFactory<'a> { conn: &'a Connection,