mirror of
https://github.com/uqbar-dao/nectar.git
synced 2024-12-23 00:21:38 +03:00
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:
commit
aacaff6306
@ -53,7 +53,7 @@ pub fn encode_keyfile(
|
|||||||
let jwtciphertext: Vec<u8> = cipher.encrypt(&jwt_nonce, jwt).unwrap();
|
let jwtciphertext: Vec<u8> = cipher.encrypt(&jwt_nonce, jwt).unwrap();
|
||||||
let fileciphertext: Vec<u8> = cipher.encrypt(&file_nonce, file_key.as_ref()).unwrap();
|
let fileciphertext: Vec<u8> = cipher.encrypt(&file_nonce, file_key.as_ref()).unwrap();
|
||||||
|
|
||||||
bincode::serialize(&(
|
serde_json::to_vec(&(
|
||||||
username.clone(),
|
username.clone(),
|
||||||
routers.clone(),
|
routers.clone(),
|
||||||
salt.to_vec(),
|
salt.to_vec(),
|
||||||
@ -68,8 +68,15 @@ pub fn decode_keyfile(keyfile: &[u8], password: &str) -> Result<Keyfile, &'stati
|
|||||||
use generic_array::GenericArray;
|
use generic_array::GenericArray;
|
||||||
|
|
||||||
let (username, routers, salt, key_enc, jwt_enc, file_enc) =
|
let (username, routers, salt, key_enc, jwt_enc, file_enc) =
|
||||||
bincode::deserialize::<(String, Vec<String>, Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)>(keyfile)
|
serde_json::from_slice::<(String, Vec<String>, Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)>(
|
||||||
.map_err(|_| "failed to deserialize keyfile")?;
|
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
|
// rederive disk key
|
||||||
let mut disk_key: DiskKey = [0u8; CREDENTIAL_LEN];
|
let mut disk_key: DiskKey = [0u8; CREDENTIAL_LEN];
|
||||||
@ -138,9 +145,16 @@ pub fn generate_jwt(
|
|||||||
|
|
||||||
#[cfg(not(feature = "simulation-mode"))]
|
#[cfg(not(feature = "simulation-mode"))]
|
||||||
pub fn get_username_and_routers(keyfile: &[u8]) -> Result<(String, Vec<String>), &'static str> {
|
pub fn get_username_and_routers(keyfile: &[u8]) -> Result<(String, Vec<String>), &'static str> {
|
||||||
let (username, routers, _salt, _key_enc, _jwt_enc) =
|
let (username, routers, _salt, _key_enc, _jwt_enc, _file_enc) =
|
||||||
bincode::deserialize::<(String, Vec<String>, Vec<u8>, Vec<u8>, Vec<u8>)>(keyfile)
|
serde_json::from_slice::<(String, Vec<String>, Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)>(
|
||||||
.map_err(|_| "failed to deserialize keyfile")?;
|
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))
|
Ok((username, routers))
|
||||||
}
|
}
|
||||||
|
@ -124,15 +124,24 @@ pub async fn register(
|
|||||||
.or(warp::path("our").and(warp::get()).and(keyfile.clone()).map(
|
.or(warp::path("our").and(warp::get()).and(keyfile.clone()).map(
|
||||||
move |keyfile: Option<Vec<u8>>| {
|
move |keyfile: Option<Vec<u8>>| {
|
||||||
if let Some(keyfile) = keyfile {
|
if let Some(keyfile) = keyfile {
|
||||||
if let Ok((username, _, _, _, _, _)) = bincode::deserialize::<(
|
if let Ok((username, _, _, _, _, _)) = serde_json::from_slice::<(
|
||||||
String,
|
String,
|
||||||
Vec<String>,
|
Vec<String>,
|
||||||
Vec<u8>,
|
Vec<u8>,
|
||||||
Vec<u8>,
|
Vec<u8>,
|
||||||
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);
|
return warp::reply::html(username);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user