diff options
author | BlackDex <[email protected]> | 2022-09-24 18:27:13 +0200 |
---|---|---|
committer | BlackDex <[email protected]> | 2022-09-24 18:27:13 +0200 |
commit | ae59472d9a7ba78587f1fa6aa2ab76688e73649e (patch) | |
tree | 46f2eb22669c4494c1298a132a8d94031f990da4 /src/util.rs | |
parent | 9c891baad1838eb3446a90b2d6d71ebace9b347c (diff) | |
download | vaultwarden-ae59472d9a7ba78587f1fa6aa2ab76688e73649e.tar.gz vaultwarden-ae59472d9a7ba78587f1fa6aa2ab76688e73649e.zip |
Fix organization vault export
Since v2022.9.x it seems they changed the export endpoint and way of working.
This PR fixes this by adding the export endpoint.
Also, it looks like the clients can't handle uppercase first JSON key's.
Because of this there now is a function which converts all the key's to lowercase first.
I have an issue reported at Bitwarden if this is expected behavior: https://github.com/bitwarden/clients/issues/3606
Fixes #2760
Fixes #2764
Diffstat (limited to 'src/util.rs')
-rw-r--r-- | src/util.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs index f5645d78..bdbb564e 100644 --- a/src/util.rs +++ b/src/util.rs @@ -357,6 +357,7 @@ pub fn get_uuid() -> String { use std::str::FromStr; +#[inline] pub fn upcase_first(s: &str) -> String { let mut c = s.chars(); match c.next() { @@ -365,6 +366,15 @@ pub fn upcase_first(s: &str) -> String { } } +#[inline] +pub fn lcase_first(s: &str) -> String { + let mut c = s.chars(); + match c.next() { + None => String::new(), + Some(f) => f.to_lowercase().collect::<String>() + c.as_str(), + } +} + pub fn try_parse_string<S, T>(string: Option<S>) -> Option<T> where S: AsRef<str>, @@ -650,3 +660,46 @@ pub fn get_reqwest_client_builder() -> ClientBuilder { headers.insert(header::USER_AGENT, header::HeaderValue::from_static("Vaultwarden")); Client::builder().default_headers(headers).timeout(Duration::from_secs(10)) } + +pub fn convert_json_key_lcase_first(src_json: Value) -> Value { + match src_json { + Value::Array(elm) => { + let mut new_array: Vec<Value> = Vec::with_capacity(elm.len()); + + for obj in elm { + new_array.push(convert_json_key_lcase_first(obj)); + } + Value::Array(new_array) + } + + Value::Object(obj) => { + let mut json_map = JsonMap::new(); + for (key, value) in obj.iter() { + match (key, value) { + (key, Value::Object(elm)) => { + let inner_value = convert_json_key_lcase_first(Value::Object(elm.clone())); + json_map.insert(lcase_first(key), inner_value); + } + + (key, Value::Array(elm)) => { + let mut inner_array: Vec<Value> = Vec::with_capacity(elm.len()); + + for inner_obj in elm { + inner_array.push(convert_json_key_lcase_first(inner_obj.clone())); + } + + json_map.insert(lcase_first(key), Value::Array(inner_array)); + } + + (key, value) => { + json_map.insert(lcase_first(key), value.clone()); + } + } + } + + Value::Object(json_map) + } + + value => value, + } +} |