added more shit

This commit is contained in:
didier amyot committed 2023-02-27 12:25:02 -05:00
1 parent 3f4e3d3ed1
commit 9cc566c9d5
2 files changed
+48 -5

No files matched your search

@@ -2,12 +2,14 @@ PRAGMA journal_mode= WAL;
CREATE TABLE IF NOT EXISTS ledger_internal CREATE TABLE IF NOT EXISTS ledger_internal
( (
-- th
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
creation_time DATETIME DEFAULT CURRENT_TIMESTAMP, creation_time DATETIME DEFAULT CURRENT_TIMESTAMP,
cookie_secret TEXT NOT NULL cookie_secret TEXT NOT NULL
); );
INSERT INTO ledger_internal(cookie_secret) VALUES ('aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkk');
CREATE TABLE IF NOT EXISTS users CREATE TABLE IF NOT EXISTS users
( (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
+45 -4
View File
@@ -5,6 +5,10 @@ use base64::{Engine as _, engine::general_purpose};
struct LedgerInternal {} struct LedgerInternal {}
impl LedgerInternal { impl LedgerInternal {
pub fn new() -> LedgerInternal {
LedgerInternal {}
}
fn _decode_cookie_secret(row: &Row) -> Result<Vec<u8>> { fn _decode_cookie_secret(row: &Row) -> Result<Vec<u8>> {
let encoded: String = row.get(0)?; let encoded: String = row.get(0)?;
let ret = general_purpose::STANDARD.decode(encoded).unwrap(); let ret = general_purpose::STANDARD.decode(encoded).unwrap();
@@ -14,26 +18,63 @@ impl LedgerInternal {
pub fn get_cookie_secret(&self, tx: &Transaction) -> CustomResult<Vec<u8>> { pub fn get_cookie_secret(&self, tx: &Transaction) -> CustomResult<Vec<u8>> {
let q = "SELECT cookie_secret let q = "SELECT cookie_secret
FROM ledger_internal FROM ledger_internal
ORDER BY cookie_secret DESC ORDER BY id DESC
LIMIT 1"; LIMIT 1";
tx.query_row(q, params![], Self::_decode_cookie_secret) tx.query_row(q, params![], Self::_decode_cookie_secret)
.map_err(Error::SqlError) .map_err(Error::SqlError)
} }
pub fn set_cookie_secret(&self, tx: &Transaction, secret: &Vec<u8>) { pub fn set_cookie_secret(&self, tx: &mut Transaction, secret: &Vec<u8>) -> CustomResult<()> {
let encoded = general_purpose::STANDARD.encode(&secret);
let q = "CREATE TEMPORARY TABLE foo let q = "CREATE TEMPORARY TABLE foo
AS SELECT * FROM ledger_internal AS SELECT * FROM ledger_internal
ORDER BY cookie_secret DESC ORDER BY id DESC
LIMIT 1"; LIMIT 1";
tx.execute(q, params![]).map_err(Error::SqlError)?;
let q = "UPDATE foo SET id = NULL, cookie_secret = ?1";
tx.execute(q, params![encoded]).map_err(Error::SqlError)?;
let q = "INSERT INTO ledger_internal SELECT * FROM foo";
tx.execute(q, params![]).map_err(Error::SqlError)?;
let q = "DROP TABLE foo";
tx.execute(q, params![]).map_err(Error::SqlError)?;
Ok(())
} }
} }
#[cfg(test)] #[cfg(test)]
mod test_ledger_internal { mod test_ledger_internal {
use crate::migration;
use super::*; use super::*;
#[test] #[test]
fn test_foo() { fn test_initial_cookie_secret() -> CustomResult<()>{
let mut conn = Connection::open_in_memory().unwrap();
migration::apply_migrations(&mut conn).unwrap();
let tx = conn.transaction().unwrap();
let service = LedgerInternal::new();
let foo = service.get_cookie_secret(&tx)?;
assert!(foo.len() >= 32);
Ok(())
}
#[test]
fn test_cookie_secret() -> CustomResult<()> {
let mut conn = Connection::open_in_memory().unwrap();
migration::apply_migrations(&mut conn).unwrap();
let mut tx = conn.transaction().unwrap();
let service = LedgerInternal::new();
service.set_cookie_secret(&mut tx, &vec![3u8])?;
let foo = service.get_cookie_secret(&tx)?;
assert_eq!(foo, vec![3u8]);
service.set_cookie_secret(&mut tx, &vec![7u8, 1u8, 4u8])?;
let foo = service.get_cookie_secret(&tx)?;
assert_eq!(foo, vec![7u8, 1u8, 4u8]);
Ok(())
} }
} }