Skip to content

Commit

Permalink
Clean-up.
Browse files Browse the repository at this point in the history
Signed-off-by: Sergey Minaev <[email protected]>
  • Loading branch information
jovfer committed Feb 18, 2024
1 parent db153a5 commit 34afcb7
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ cargo test
```

### Interoperability testing tool
Coming soon (planned for v0.0.7)
See [Generate tool README](./generate/README.md) document.

## External Dependencies

Expand Down
2 changes: 0 additions & 2 deletions generate/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
use std::error::Error as StdError;
use std::fmt::{self, Display, Formatter};
use std::result::Result as StdResult;
use serde_json;
use serde_yaml;

pub type Result<T> = std::result::Result<T, Error>;

Expand Down
21 changes: 10 additions & 11 deletions generate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn generate_and_check(

let loaded_sd_jwt = load_sd_jwt(&stored_sd_jwt_file_path)?;

let loaded_sdjwt_paylod = parse_sdjwt_paylod(&loaded_sd_jwt.replace("\n", ""), &serialization_format, decoy)?;
let loaded_sdjwt_paylod = parse_sdjwt_paylod(&loaded_sd_jwt.replace('\n', ""), &serialization_format, decoy)?;
let issued_sdjwt_paylod = parse_sdjwt_paylod(&sd_jwt, &serialization_format, decoy)?;

compare_jwt_payloads(&loaded_sdjwt_paylod, &issued_sdjwt_paylod)?;
Expand All @@ -94,7 +94,7 @@ fn generate_and_check(

compare_verified_claims(&loaded_verified_claims, &verified_claims)?;

return Ok(());
Ok(())
}

fn issue_sd_jwt(
Expand Down Expand Up @@ -144,7 +144,7 @@ fn issue_sd_jwt(
serialization_format)
.unwrap();

return Ok(sd_jwt);
Ok(sd_jwt)
}

fn create_presentation(
Expand All @@ -163,7 +163,7 @@ fn create_presentation(
None
).unwrap();

return Ok(presentation);
Ok(presentation)
}

fn verify_presentation(
Expand All @@ -184,19 +184,19 @@ fn verify_presentation(
serialization_format,
).unwrap();

return Ok(_verified.verified_claims);
Ok(_verified.verified_claims)
}

fn parse_verified_claims(content: &str) -> Result<Value> {
let json_value: Value = serde_json::from_str(content)?;

// TODO: check if the json_value is json object
return Ok(json_value);
Ok(json_value)
}

fn load_sd_jwt(path: &PathBuf) -> Result<String> {
let content = std::fs::read_to_string(path)?;
return Ok(content);
Ok(content)
}

fn compare_jwt_payloads(loaded_payload: &Value, issued_payload: &Value) -> Result<()> {
Expand All @@ -211,7 +211,7 @@ fn compare_jwt_payloads(loaded_payload: &Value, issued_payload: &Value) -> Resul
return Err(Error::from_msg(ErrorKind::DataNotEqual, "JWT payloads are different"));
}

return Ok(());
Ok(())
}

fn compare_verified_claims(loaded_claims: &Value, verified_claims: &Value) -> Result<()> {
Expand All @@ -226,7 +226,7 @@ fn compare_verified_claims(loaded_claims: &Value, verified_claims: &Value) -> Re
return Err(Error::from_msg(ErrorKind::DataNotEqual, "verified claims are different"));
}

return Ok(());
Ok(())
}

fn get_key(path: &PathBuf) -> EncodingKey {
Expand All @@ -238,8 +238,7 @@ fn get_key(path: &PathBuf) -> EncodingKey {
fn get_settings(path: &PathBuf) -> Settings {
println!("settings.yaml - {:?}", path);

let settings = Settings::from(path);
settings
Settings::from(path)
}

fn get_specification_paths(args: &Cli, basedir: PathBuf) -> Result<Vec<PathBuf>> {
Expand Down
14 changes: 7 additions & 7 deletions generate/src/types/specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ impl Specification {
fn replace_empty_items(m: &serde_json::Value) -> serde_json::Value {
match m {
serde_json::Value::Array(arr) if (arr.is_empty()) => {
return serde_json::Value::Bool(false);
serde_json::Value::Bool(false)
}
serde_json::Value::Object(obj) if (obj.is_empty()) => {
return serde_json::Value::Bool(false);
serde_json::Value::Bool(false)
}
serde_json::Value::Array(arr) => {
let mut result = Vec::new();
Expand All @@ -42,7 +42,7 @@ fn replace_empty_items(m: &serde_json::Value) -> serde_json::Value {
result.push(replace_empty_items(value));
}

return serde_json::Value::Array(result);
serde_json::Value::Array(result)
}
serde_json::Value::Object(obj) => {
let mut result = serde_json::Map::new();
Expand All @@ -51,10 +51,10 @@ fn replace_empty_items(m: &serde_json::Value) -> serde_json::Value {
result.insert(key.clone(), replace_empty_items(value));
}

return serde_json::Value::Object(result);
serde_json::Value::Object(result)
}
_ => {
return m.clone();
m.clone()
}
}
}
Expand Down Expand Up @@ -106,7 +106,7 @@ fn _validate(value: &Value) -> Result<()> {
match value {
Value::String(_) | Value::Bool(_) | Value::Number(_) => Ok(()),
Value::Tagged(tag) => {
if tag.tag.to_string() == SD_TAG {
if tag.tag == SD_TAG {
_validate(&tag.value)
} else {
panic!(
Expand Down Expand Up @@ -161,7 +161,7 @@ fn _remove_tags(original: &Value) -> Value {
Value::Mapping(filtered_map)
}
Value::Sequence(seq) => {
let filtered_seq: Vec<Value> = seq.iter().map(|v| _remove_tags(v)).collect();
let filtered_seq: Vec<Value> = seq.iter().map(_remove_tags).collect();

Value::Sequence(filtered_seq)
}
Expand Down
25 changes: 12 additions & 13 deletions generate/src/utils/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@ pub fn parse_sdjwt_paylod(

match serialization_format {
SDJWTSerializationFormat::JSON => {
return parse_payload_json(sd_jwt, remove_decoy);
parse_payload_json(sd_jwt, remove_decoy)
},
SDJWTSerializationFormat::Compact => {
return parse_payload_compact(sd_jwt, remove_decoy);
parse_payload_compact(sd_jwt, remove_decoy)
}
}
}

fn parse_payload_json(sd_jwt: &str, remove_decoy: bool) -> Result<Value> {
let v: serde_json::Value = serde_json::from_str(&sd_jwt).unwrap();
let v: serde_json::Value = serde_json::from_str(sd_jwt).unwrap();

let disclosures = v.as_object().unwrap().get("disclosures").unwrap();

let mut hashes: HashSet<String> = HashSet::new();

for disclosure in disclosures.as_array().unwrap() {
let hash = base64_hash(disclosure.as_str().unwrap().replace(" ", "").as_bytes());
let hash = base64_hash(disclosure.as_str().unwrap().replace(' ', "").as_bytes());
hashes.insert(hash.clone());
}

let ddd = v.as_object().unwrap().get("payload").unwrap().as_str().unwrap().replace(" ", "");
let ddd = v.as_object().unwrap().get("payload").unwrap().as_str().unwrap().replace(' ', "");
let payload = base64url_decode(&ddd).unwrap();

let payload: serde_json::Value = serde_json::from_slice(&payload).unwrap();
Expand All @@ -45,19 +45,19 @@ fn parse_payload_json(sd_jwt: &str, remove_decoy: bool) -> Result<Value> {
return Ok(remove_decoy_items(&payload, &hashes));
}

return Ok(payload);
Ok(payload)
}

fn parse_payload_compact(sd_jwt: &str, remove_decoy: bool) -> Result<Value> {
let mut disclosures: Vec<String> = sd_jwt
.split("~")
.split('~')
.filter(|s| !s.is_empty())
.map(|s| String::from(s))
.map(String::from)
.collect();

let payload = disclosures.remove(0);

let payload: Vec<_> = payload.split(".").collect();
let payload: Vec<_> = payload.split('.').collect();
let payload = String::from(payload[1]);

let mut hashes: HashSet<String> = HashSet::new();
Expand All @@ -75,7 +75,7 @@ fn parse_payload_compact(sd_jwt: &str, remove_decoy: bool) -> Result<Value> {
return Ok(remove_decoy_items(&payload, &hashes));
}

return Ok(payload);
Ok(payload)
}

fn remove_decoy_items(payload: &Value, hashes: &HashSet<String>) -> Value {
Expand All @@ -84,8 +84,7 @@ fn remove_decoy_items(payload: &Value, hashes: &HashSet<String>) -> Value {
for (key, val) in payload.as_object().unwrap() {
if key == "_sd" {
let v1: Vec<_> = val.as_array().unwrap().iter()
.filter(|item| hashes.contains(item.as_str().unwrap()))
.map(|item| item.clone())
.filter(|item| hashes.contains(item.as_str().unwrap())).cloned()
.collect();

let filtered_array = serde_json::Value::Array(v1);
Expand All @@ -98,7 +97,7 @@ fn remove_decoy_items(payload: &Value, hashes: &HashSet<String>) -> Value {
}
}

return Value::Object(map);
Value::Object(map)
}

pub fn load_salts(path: &PathBuf) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion generate/src/utils/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn generate_jsonpath_from_tagged_values(
) -> Result<()> {

if path.is_empty() {
path.push_str("$");
path.push('$');
}

match yaml {
Expand Down
23 changes: 13 additions & 10 deletions src/disclosure.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::utils::{base64_hash, base64url_encode, generate_salt};
use crate::utils::{base64_hash, base64url_encode};
#[cfg(not(feature = "mock_salts"))]
use crate::utils::generate_salt;
#[cfg(feature = "mock_salts")]
use crate::utils::generate_salt_mock;
use serde_json::Value;
Expand All @@ -12,18 +14,19 @@ pub(crate) struct SDJWTDisclosure {

impl SDJWTDisclosure {
pub(crate) fn new<V>(key: Option<String>, value: V) -> Self where V: ToString {
let mut salt = generate_salt();
#[cfg(not(feature = "mock_salts"))]
let salt = generate_salt();
let mut value_str = value.to_string();

#[cfg(feature = "mock_salts")]
{
salt = generate_salt_mock();
let salt = {
value_str = value_str
.replace(":[", ": [")
.replace(',', ", ")
.replace("\":", "\": ")
.replace("\": ", "\": ");
}
generate_salt_mock()
};

if !value_str.is_ascii() {
value_str = escape_unicode_chars(&value_str);
Expand Down Expand Up @@ -55,21 +58,21 @@ fn escape_unicode_chars(s: &str) -> String {
let esc_c = c.escape_unicode().to_string();

let esc_c_new = match esc_c.chars().count() {
6 => esc_c.replace("\\u{", "\\u00").replace("}", ""), // example: \u{de}
7 => esc_c.replace("\\u{", "\\u0").replace("}", ""), // example: \u{980}
8 => esc_c.replace("\\u{", "\\u").replace("}", ""), // example: \u{23f0}
6 => esc_c.replace("\\u{", "\\u00").replace('}', ""), // example: \u{de}
7 => esc_c.replace("\\u{", "\\u0").replace('}', ""), // example: \u{980}
8 => esc_c.replace("\\u{", "\\u").replace('}', ""), // example: \u{23f0}
_ => {panic!("unexpected value")}
};

result.push_str(&esc_c_new);
}
}

return result;
result
}

fn escape_json(s: &str) -> String {
return Value::String(String::from(s)).to_string();
Value::String(String::from(s)).to_string()
}

#[cfg(test)]
Expand Down
14 changes: 7 additions & 7 deletions src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,15 @@ impl SDJWTVerifier {
fn unpack_disclosed_claims(&mut self, sd_jwt_claims: &Value) -> Result<Value> {
match sd_jwt_claims {
Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => {
return Ok(sd_jwt_claims.to_owned());
Ok(sd_jwt_claims.to_owned())
}
Value::Array(arr) => {
return self.unpack_disclosed_claims_in_array(arr);
self.unpack_disclosed_claims_in_array(arr)
}
Value::Object(obj) => {
return self.unpack_disclosed_claims_in_object(obj);
self.unpack_disclosed_claims_in_object(obj)
}
};
}
}

fn unpack_disclosed_claims_in_array(&mut self, arr: &Vec<Value>) -> Result<Value> {
Expand All @@ -263,8 +263,8 @@ impl SDJWTVerifier {

let digest = obj.get(SD_LIST_PREFIX).unwrap();
let disclosed_claim = self.unpack_from_digest(digest)?;
if disclosed_claim.is_some() {
claims.push(disclosed_claim.unwrap());
if let Some(disclosed_claim) = disclosed_claim {
claims.push(disclosed_claim);
}
},
_ => {
Expand All @@ -273,7 +273,7 @@ impl SDJWTVerifier {
},
}
}
return Ok(Value::Array(claims));
Ok(Value::Array(claims))
}

fn unpack_disclosed_claims_in_object(&mut self, nested_sd_jwt_claims: &Map<String, Value>) -> Result<Value> {
Expand Down

0 comments on commit 34afcb7

Please sign in to comment.