diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2022-11-04 00:49:49 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2022-11-04 00:49:49 -0700 |
commit | 6c5c1e84b3540e3a4da81334f34b4e53cc818f4d (patch) | |
tree | 140cd3963067c2e529dbee0fc3d2869adb9204e4 /adenosine-pds/src/db.rs | |
parent | ed47a0bc0d1d6692c7c365bf33a69d8017129f96 (diff) | |
download | adenosine-6c5c1e84b3540e3a4da81334f34b4e53cc818f4d.tar.gz adenosine-6c5c1e84b3540e3a4da81334f34b4e53cc818f4d.zip |
pds: more progress
Diffstat (limited to 'adenosine-pds/src/db.rs')
-rw-r--r-- | adenosine-pds/src/db.rs | 60 |
1 files changed, 20 insertions, 40 deletions
diff --git a/adenosine-pds/src/db.rs b/adenosine-pds/src/db.rs index 72f2a8d..17006cb 100644 --- a/adenosine-pds/src/db.rs +++ b/adenosine-pds/src/db.rs @@ -3,7 +3,7 @@ use crate::{AtpSession, KeyPair}; use anyhow::{anyhow, Result}; use lazy_static::lazy_static; use log::debug; -use rusqlite::{params, Connection}; +use rusqlite::{params, Connection, OptionalExtension}; use rusqlite_migration::{Migrations, M}; use serde_json::Value; use std::path::PathBuf; @@ -60,41 +60,6 @@ impl AtpDatabase { Ok(AtpDatabase { conn }) } - pub fn get_record(&mut self, did: &str, collection: &str, tid: &str) -> Result<Value> { - let mut stmt = self.conn.prepare_cached( - "SELECT record_json FROM record WHERE did = ?1 AND collection = ?2 AND tid = ?3", - )?; - Ok(stmt.query_row(params!(did, collection, tid), |row| { - row.get(0).map(|v: String| Value::from_str(&v)) - })??) - } - - pub fn get_record_list(&mut self, did: &str, collection: &str) -> Result<Vec<String>> { - let mut stmt = self - .conn - .prepare_cached("SELECT tid FROM record WHERE did = ?1 AND collection = ?2")?; - let ret = stmt - .query_and_then(params!(did, collection), |row| { - let v: String = row.get(0)?; - Ok(v) - })? - .collect(); - ret - } - - pub fn get_collection_list(&mut self, did: &str) -> Result<Vec<String>> { - let mut stmt = self - .conn - .prepare_cached("SELECT collection FROM record WHERE did = ?1 GROUP BY collection")?; - let ret = stmt - .query_and_then(params!(did), |row| { - let v: String = row.get(0)?; - Ok(v) - })? - .collect(); - ret - } - /// Quick check if an account already exists for given username or email pub fn account_exists(&mut self, username: &str, email: &str) -> Result<bool> { let mut stmt = self @@ -155,13 +120,21 @@ impl AtpDatabase { }) } - /// Returns the DID that a token is valid for - pub fn check_auth_token(&mut self, jwt: &str) -> Result<String> { + /// Returns the DID that a token is valid for, or None if session not found + pub fn check_auth_token(&mut self, jwt: &str) -> Result<Option<String>> { let mut stmt = self .conn .prepare_cached("SELECT did FROM session WHERE jwt = $1")?; - let did = stmt.query_row(params!(jwt), |row| row.get(0))?; - Ok(did) + let did_maybe = stmt.query_row(params!(jwt), |row| row.get(0)).optional()?; + Ok(did_maybe) + } + + pub fn delete_session(&mut self, jwt: &str) -> Result<bool> { + let mut stmt = self + .conn + .prepare_cached("DELETE FROM session WHERE jwt = $1")?; + let count = stmt.execute(params!(jwt))?; + Ok(count >= 1) } pub fn put_did_doc(&mut self, did: &str, did_doc: &Value) -> Result<()> { @@ -171,4 +144,11 @@ impl AtpDatabase { stmt.execute(params!(did, did_doc.to_string()))?; Ok(()) } + pub fn get_did_doc(&mut self, did: &str) -> Result<Value> { + let mut stmt = self + .conn + .prepare_cached("SELECT doc_json FROM did_doc WHERE did = $1")?; + let doc_json: String = stmt.query_row(params!(did), |row| row.get(0))?; + Ok(Value::from_str(&doc_json)?) + } } |