From 6c5c1e84b3540e3a4da81334f34b4e53cc818f4d Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Fri, 4 Nov 2022 00:49:49 -0700 Subject: pds: more progress --- adenosine-pds/src/db.rs | 60 +++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 40 deletions(-) (limited to 'adenosine-pds/src/db.rs') 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 { - 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> { - 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> { - 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 { 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 { + /// 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> { 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 { + 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 { + 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)?) + } } -- cgit v1.2.3