I need to stop working on this shit.
This commit is contained in:
1 parent
5cd6d6f424
commit
3f4e3d3ed1
7 files changed
+93
-2
No files matched your search
@@ -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 _test_utils;
|
||||
mod _users;
|
||||
mod _ledger_internal;
|
||||
|
||||
pub mod 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 _users;
|
||||
mod _authentication;
|
||||
|
||||
pub struct ServiceFactory<'a> {
|
||||
conn: &'a Connection,
|
||||
|
||||
Reference in new issue
Block a user