I need to stop working on this shit.
This commit is contained in:
1 parent
5cd6d6f424
commit
3f4e3d3ed1
7 files changed
+91
No files matched your search
Generated
+1
@@ -742,6 +742,7 @@ dependencies = [
|
|||||||
"actix-web-lab",
|
"actix-web-lab",
|
||||||
"apache-avro",
|
"apache-avro",
|
||||||
"async-channel",
|
"async-channel",
|
||||||
|
"base64 0.21.0",
|
||||||
"clap",
|
"clap",
|
||||||
"clap-verbosity-flag",
|
"clap-verbosity-flag",
|
||||||
"cookie",
|
"cookie",
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ minijinja-autoreload = "0.30.2"
|
|||||||
futures = "0.3.26"
|
futures = "0.3.26"
|
||||||
actix-files = "0.6.2"
|
actix-files = "0.6.2"
|
||||||
async-channel = "1.8.0"
|
async-channel = "1.8.0"
|
||||||
|
base64 = "0.21.0"
|
||||||
|
|
||||||
[dependencies.apache-avro]
|
[dependencies.apache-avro]
|
||||||
version = "0.14.0"
|
version = "0.14.0"
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
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
|
CREATE TABLE IF NOT EXISTS users
|
||||||
(
|
(
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
|||||||
@@ -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<Vec<u8>> {
|
||||||
|
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<Vec<u8>> {
|
||||||
|
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<u8>) {
|
||||||
|
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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ mod _interests;
|
|||||||
mod _securities;
|
mod _securities;
|
||||||
mod _test_utils;
|
mod _test_utils;
|
||||||
mod _users;
|
mod _users;
|
||||||
|
mod _ledger_internal;
|
||||||
|
|
||||||
pub mod external_id {
|
pub mod external_id {
|
||||||
pub use super::_external_id::*;
|
pub use super::_external_id::*;
|
||||||
|
|||||||
@@ -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<Cookie>{
|
||||||
|
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<String> {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ use crate::services::_users::UserService;
|
|||||||
|
|
||||||
mod _transactions;
|
mod _transactions;
|
||||||
mod _users;
|
mod _users;
|
||||||
|
mod _authentication;
|
||||||
|
|
||||||
pub struct ServiceFactory<'a> {
|
pub struct ServiceFactory<'a> {
|
||||||
conn: &'a Connection,
|
conn: &'a Connection,
|
||||||
|
|||||||
Reference in new issue
Block a user