Clean up userdevicesessionid_uiaarequest BTreeMap
There is no need to encode or decode anything as we are not
saving to disk
+use std::collections::BTreeMap;
use std::sync::Arc;
use std::sync::RwLock;
-use std::collections::BTreeMap;
use crate::{client_server::SESSION_ID_LENGTH, utils, Error, Result};
use ruma::{
@@ -20,7 +20,8 @@ use super::abstraction::Tree;
pub struct Uiaa {
pub(super) userdevicesessionid_uiaainfo: Arc<dyn Tree>, // User-interactive authentication
- pub(super) userdevicesessionid_uiaarequest: RwLock<BTreeMap<Vec<u8>, Vec<u8>>>, // UiaaRequest = canonical json value
+ pub(super) userdevicesessionid_uiaarequest:
+ RwLock<BTreeMap<(UserId, String, String), CanonicalJsonValue>>,
}
impl Uiaa {
@@ -149,15 +150,16 @@ impl Uiaa {
session: &str,
request: &CanonicalJsonValue,
) -> Result<()> {
- let mut userdevicesessionid = user_id.as_bytes().to_vec();
- userdevicesessionid.push(0xff);
- userdevicesessionid.extend_from_slice(device_id.as_bytes());
- userdevicesessionid.push(0xff);
- userdevicesessionid.extend_from_slice(session.as_bytes());
-
- self.userdevicesessionid_uiaarequest.write().unwrap().insert(
- userdevicesessionid,
- serde_json::to_vec(request).expect("json value to vec always works"),
+ self.userdevicesessionid_uiaarequest
+ .write()
+ .unwrap()
+ .insert(
+ (
+ user_id.to_owned(),
+ device_id.to_string(),
+ session.to_string(),
+ ),
+ request.to_owned(),
);
Ok(())
@@ -169,22 +171,16 @@ impl Uiaa {
device_id: &DeviceId,
session: &str,
) -> Result<Option<CanonicalJsonValue>> {
- let mut userdevicesessionid = user_id.as_bytes().to_vec();
- userdevicesessionid.push(0xff);
- userdevicesessionid.extend_from_slice(device_id.as_bytes());
- userdevicesessionid.push(0xff);
- userdevicesessionid.extend_from_slice(session.as_bytes());
-
- self.userdevicesessionid_uiaarequest.read().unwrap()
- .get(&userdevicesessionid)
- .map(|bytes| {
- serde_json::from_str::<CanonicalJsonValue>(
- &utils::string_from_bytes(&bytes)
- .map_err(|_| Error::bad_database("Invalid uiaa request bytes in db."))?,
- )
- .map_err(|_| Error::bad_database("Invalid uiaa request in db."))
- })
- .transpose()
+ Ok(self
+ .userdevicesessionid_uiaarequest
+ .read()
+ .unwrap()
+ .get(&(
+ user_id.to_owned(),
+ device_id.to_string(),
+ session.to_string(),
+ ))
+ .map(|j| j.to_owned()))
}
fn update_uiaa_session(
