secret: Store secret in MessagePack format

Should be more compact and efficient
This commit is contained in:
Kévin Commaille 2023-04-23 11:34:03 +02:00 committed by Kévin Commaille
parent 0987b89be3
commit e1b5eb56ae
3 changed files with 27 additions and 10 deletions

1
Cargo.lock generated
View file

@ -1315,6 +1315,7 @@ dependencies = [
"qrcode",
"rand 0.8.5",
"regex",
"rmp-serde",
"rqrr",
"ruma",
"secular",

View file

@ -55,6 +55,7 @@ pulldown-cmark = "0.9"
qrcode = "0.12"
rand = "0.8"
regex = "1"
rmp-serde = "1"
rqrr = "0.6"
secular = { version = "1", features = ["bmp", "normalization"] }
serde = "1"

View file

@ -230,16 +230,31 @@ impl StoredSession {
}
};
let secret = match item.secret().await {
Ok(secret) => match Secret::from_utf8(&secret) {
Ok(secret) => secret,
Err(error) => {
error!("Could not parse secret in stored session: {error:?}");
return Err(SecretError::CorruptSession {
error: gettext("Malformed secret in stored session"),
item,
});
Ok(secret) => {
if version == 0 {
match Secret::from_utf8(&secret) {
Ok(secret) => secret,
Err(error) => {
error!("Could not parse secret in stored session: {error:?}");
return Err(SecretError::CorruptSession {
error: gettext("Malformed secret in stored session"),
item,
});
}
}
} else {
match rmp_serde::from_slice::<Secret>(&secret) {
Ok(secret) => secret,
Err(error) => {
error!("Could not parse secret in stored session: {error}");
return Err(SecretError::CorruptSession {
error: gettext("Malformed secret in stored session"),
item,
});
}
}
}
},
}
Err(error) => {
error!("Could not get secret in stored session: {error}");
return Err(SecretError::CorruptSession {
@ -347,7 +362,7 @@ pub async fn store_session(session: &StoredSession) -> Result<(), SecretError> {
let attrs = session.attributes();
let attributes = attrs.iter().map(|(k, v)| (*k, v.as_ref())).collect();
let secret = serde_json::to_string(&session.secret).unwrap();
let secret = rmp_serde::to_vec_named(&session.secret).unwrap();
keyring
.create_item(