Merge pull request #479 from kinode-dao/dr/keyfile-serde-json

keyfile: encode/decode as JSON for better cross-lang support, keep bincode as backup
This commit is contained in:
doria 2024-08-09 18:04:06 +03:00 committed by GitHub
commit aacaff6306
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 9 deletions

View File

@ -53,7 +53,7 @@ pub fn encode_keyfile(
let jwtciphertext: Vec<u8> = cipher.encrypt(&jwt_nonce, jwt).unwrap();
let fileciphertext: Vec<u8> = cipher.encrypt(&file_nonce, file_key.as_ref()).unwrap();
bincode::serialize(&(
serde_json::to_vec(&(
username.clone(),
routers.clone(),
salt.to_vec(),
@ -68,8 +68,15 @@ pub fn decode_keyfile(keyfile: &[u8], password: &str) -> Result<Keyfile, &'stati
use generic_array::GenericArray;
let (username, routers, salt, key_enc, jwt_enc, file_enc) =
bincode::deserialize::<(String, Vec<String>, Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)>(keyfile)
.map_err(|_| "failed to deserialize keyfile")?;
serde_json::from_slice::<(String, Vec<String>, Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)>(
keyfile,
)
.or_else(|_| {
bincode::deserialize::<(String, Vec<String>, Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)>(
keyfile,
)
})
.map_err(|_| "failed to deserialize keyfile")?;
// rederive disk key
let mut disk_key: DiskKey = [0u8; CREDENTIAL_LEN];
@ -138,9 +145,16 @@ pub fn generate_jwt(
#[cfg(not(feature = "simulation-mode"))]
pub fn get_username_and_routers(keyfile: &[u8]) -> Result<(String, Vec<String>), &'static str> {
let (username, routers, _salt, _key_enc, _jwt_enc) =
bincode::deserialize::<(String, Vec<String>, Vec<u8>, Vec<u8>, Vec<u8>)>(keyfile)
.map_err(|_| "failed to deserialize keyfile")?;
let (username, routers, _salt, _key_enc, _jwt_enc, _file_enc) =
serde_json::from_slice::<(String, Vec<String>, Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)>(
keyfile,
)
.or_else(|_| {
bincode::deserialize::<(String, Vec<String>, Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)>(
keyfile,
)
})
.map_err(|_| "failed to deserialize keyfile")?;
Ok((username, routers))
}

View File

@ -124,15 +124,24 @@ pub async fn register(
.or(warp::path("our").and(warp::get()).and(keyfile.clone()).map(
move |keyfile: Option<Vec<u8>>| {
if let Some(keyfile) = keyfile {
if let Ok((username, _, _, _, _, _)) = bincode::deserialize::<(
if let Ok((username, _, _, _, _, _)) = serde_json::from_slice::<(
String,
Vec<String>,
Vec<u8>,
Vec<u8>,
Vec<u8>,
Vec<u8>,
)>(keyfile.as_ref())
{
)>(&keyfile)
.or_else(|_| {
bincode::deserialize::<(
String,
Vec<String>,
Vec<u8>,
Vec<u8>,
Vec<u8>,
Vec<u8>,
)>(&keyfile)
}) {
return warp::reply::html(username);
}
}