diff options
Diffstat (limited to 'src/data.rs')
-rw-r--r-- | src/data.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/data.rs b/src/data.rs index 5674f92..a5d7afb 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,5 +1,6 @@ use rusqlite::Connection; use error::{Result, LinoError}; +use rand::{OsRng, Rng}; #[derive(Serialize, Debug, Clone)] pub struct Quote { @@ -31,8 +32,27 @@ pub fn init(c: &Connection) -> Result<()> { score INTEGER NOT NULL, FOREIGN KEY(quote_id) REFERENCES quotes(id) ); + + CREATE TABLE IF NOT EXISTS keys ( + id TEXT PRIMARY KEY, + value BLOB NOT NULL + ); "#, )?; + + let key = { + let mut v = vec![0u8; 32]; + (OsRng::new()?).fill_bytes(v.as_mut()); + v + }; + c.execute( + r#" + INSERT OR IGNORE INTO keys (id, value) VALUES + ('session', ?1); + "#, + &[&key], + )?; + Ok(()) } @@ -56,6 +76,17 @@ pub fn populate_test_db(c: &Connection) -> Result<()> { Ok(()) } +pub fn get_key(c: &Connection, id: &str) -> Result<Vec<u8>> { + let key = c.query_row( + r#" + SELECT value FROM keys WHERE id = ?1 + "#, + &[&id], + |row| row.get(0), + )?; + Ok(key) +} + pub fn new_quote(c: &Connection, date: &str, author: &str, content: &str) -> Result<()> { c.execute( r#" |