improved user lookup by token

This commit is contained in:
Kai Jan Kriegel 2023-02-24 05:06:45 +01:00
parent d8a7415e24
commit 795c9870e8
2 changed files with 9 additions and 12 deletions

View File

@ -146,21 +146,20 @@ impl UserDB {
self.db.get(&txn, &uid.as_bytes()) self.db.get(&txn, &uid.as_bytes())
} }
pub fn get_by_token(&self, token: &str) -> Result<Option<ArchivedValue<User>>, db::Error> { pub fn get_by_token(&self, token: &str) -> Result<Option<User>, db::Error> {
let txn = self.env.begin_ro_txn()?; let txn = self.env.begin_ro_txn()?;
let mut iter = self.db.get_all(&txn)?.into_iter(); 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 = let user: User =
Deserialize::<User, _>::deserialize(archived_user.as_ref(), &mut Infallible).unwrap(); Deserialize::<User, _>::deserialize(user.as_ref(), &mut Infallible).unwrap();
if user.userdata.kv.get("cardtoken") == Some(&token.to_string()) { let stored_token = user.userdata.kv.get("cardtoken")?;
Some(archived_user) if stored_token == &token.to_string() {
Some(user)
} else { } else {
None None
} }
})); }))
Ok(None)
} }
pub fn put(&self, uid: &str, user: &User) -> Result<(), db::Error> { pub fn put(&self, uid: &str, user: &User) -> Result<(), db::Error> {

View File

@ -101,9 +101,7 @@ impl Users {
pub fn get_user_by_token(&self, token: &str) -> Option<db::User> { pub fn get_user_by_token(&self, token: &str) -> Option<db::User> {
tracing::trace!(token, "Looking up user by token"); tracing::trace!(token, "Looking up user by token");
self.userdb.get_by_token(token).unwrap().map(|user| { self.userdb.get_by_token(token).unwrap()
Deserialize::<db::User, _>::deserialize(user.as_ref(), &mut Infallible).unwrap()
})
} }
pub fn put_user(&self, uid: &str, user: &db::User) -> Result<(), crate::db::Error> { pub fn put_user(&self, uid: &str, user: &db::User) -> Result<(), crate::db::Error> {