From 00f90c731468c646fd04fff1751aa6399d0f9fbc Mon Sep 17 00:00:00 2001 From: Kai Jan Kriegel Date: Wed, 8 Feb 2023 18:57:36 +0100 Subject: [PATCH] added support for looking up user by (first)token --- bffhd/users/db.rs | 17 +++++++++++++++++ bffhd/users/mod.rs | 7 +++++++ 2 files changed, 24 insertions(+) diff --git a/bffhd/users/db.rs b/bffhd/users/db.rs index c87449b..98d0005 100644 --- a/bffhd/users/db.rs +++ b/bffhd/users/db.rs @@ -146,6 +146,23 @@ impl UserDB { self.db.get(&txn, &uid.as_bytes()) } + 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() }; + 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) + } else { + None + } + })); + + Ok(None) + } + pub fn put(&self, uid: &str, user: &User) -> Result<(), db::Error> { let mut serializer = AllocSerializer::<1024>::default(); serializer.serialize_value(user).expect("rkyv error"); diff --git a/bffhd/users/mod.rs b/bffhd/users/mod.rs index bd7a7f9..7994274 100644 --- a/bffhd/users/mod.rs +++ b/bffhd/users/mod.rs @@ -99,6 +99,13 @@ 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() + }) + } + pub fn put_user(&self, uid: &str, user: &db::User) -> Result<(), crate::db::Error> { tracing::trace!(uid, ?user, "Updating user"); self.userdb.put(uid, user)