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", "qrcode",
"rand 0.8.5", "rand 0.8.5",
"regex", "regex",
"rmp-serde",
"rqrr", "rqrr",
"ruma", "ruma",
"secular", "secular",

View file

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

View file

@ -230,7 +230,9 @@ impl StoredSession {
} }
}; };
let secret = match item.secret().await { let secret = match item.secret().await {
Ok(secret) => match Secret::from_utf8(&secret) { Ok(secret) => {
if version == 0 {
match Secret::from_utf8(&secret) {
Ok(secret) => secret, Ok(secret) => secret,
Err(error) => { Err(error) => {
error!("Could not parse secret in stored session: {error:?}"); error!("Could not parse secret in stored session: {error:?}");
@ -239,7 +241,20 @@ impl StoredSession {
item, 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) => { Err(error) => {
error!("Could not get secret in stored session: {error}"); error!("Could not get secret in stored session: {error}");
return Err(SecretError::CorruptSession { return Err(SecretError::CorruptSession {
@ -347,7 +362,7 @@ pub async fn store_session(session: &StoredSession) -> Result<(), SecretError> {
let attrs = session.attributes(); let attrs = session.attributes();
let attributes = attrs.iter().map(|(k, v)| (*k, v.as_ref())).collect(); 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 keyring
.create_item( .create_item(