From 795c9870e8d81bb9f56b3ea2039c5ae0f5f2e5e7 Mon Sep 17 00:00:00 2001 From: Kai Jan Kriegel Date: Fri, 24 Feb 2023 05:06:45 +0100 Subject: [PATCH] improved user lookup by token --- bffhd/users/db.rs | 17 ++++++++--------- bffhd/users/mod.rs | 4 +--- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/bffhd/users/db.rs b/bffhd/users/db.rs index 98d0005..c595e5f 100644 --- a/bffhd/users/db.rs +++ b/bffhd/users/db.rs @@ -146,21 +146,20 @@ impl UserDB { self.db.get(&txn, &uid.as_bytes()) } - pub fn get_by_token(&self, token: &str) -> Result>, db::Error> { + pub fn get_by_token(&self, token: &str) -> Result, db::Error> { let txn = self.env.begin_ro_txn()?; let mut iter = self.db.get_all(&txn)?.into_iter(); - return Ok(iter.find_map(|(uid, archived_user)| { - let uid = unsafe { std::str::from_utf8_unchecked(uid).to_string() }; + + Ok(iter.find_map(|(_, user)| { let user: User = - Deserialize::::deserialize(archived_user.as_ref(), &mut Infallible).unwrap(); - if user.userdata.kv.get("cardtoken") == Some(&token.to_string()) { - Some(archived_user) + Deserialize::::deserialize(user.as_ref(), &mut Infallible).unwrap(); + let stored_token = user.userdata.kv.get("cardtoken")?; + if stored_token == &token.to_string() { + Some(user) } else { None } - })); - - Ok(None) + })) } pub fn put(&self, uid: &str, user: &User) -> Result<(), db::Error> { diff --git a/bffhd/users/mod.rs b/bffhd/users/mod.rs index 7994274..1a4a1fd 100644 --- a/bffhd/users/mod.rs +++ b/bffhd/users/mod.rs @@ -101,9 +101,7 @@ impl Users { pub fn get_user_by_token(&self, token: &str) -> Option { tracing::trace!(token, "Looking up user by token"); - self.userdb.get_by_token(token).unwrap().map(|user| { - Deserialize::::deserialize(user.as_ref(), &mut Infallible).unwrap() - }) + self.userdb.get_by_token(token).unwrap() } pub fn put_user(&self, uid: &str, user: &db::User) -> Result<(), crate::db::Error> {