-
Notifications
You must be signed in to change notification settings - Fork 26
Support OpenID4VP multi-signed request in Rust matcher #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,51 @@ | ||
| use crate::base64url::decode_base64url; | ||
| use crate::credman::CredmanApi; | ||
| use crate::json_value::JsonValue; | ||
| pub use crate::openid4vp_models::*; | ||
| use crate::reporter::report_match_result; | ||
| use nanoserde::DeJson; | ||
| use std::borrow::Cow; | ||
|
|
||
| fn extract_multisigned_payload<'a>( | ||
| pr: &'a ProtocolRequest, | ||
| ) -> Result<String, Box<dyn std::error::Error>> { | ||
| let json_str: &str = if let Some(data) = &pr.data { | ||
| match data { | ||
| ProtocolRequestData::String(s) => s.as_str(), | ||
| ProtocolRequestData::Object(obj) => obj.request.as_str(), | ||
| } | ||
| } else if !pr.request.is_empty() { | ||
| pr.request.as_str() | ||
| } else { | ||
| return Err("Missing multisigned request data".into()); | ||
| }; | ||
|
|
||
| let parsed: JsonValue = DeJson::deserialize_json(json_str)?; | ||
|
|
||
| let payload = match &parsed { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of |
||
| JsonValue::Object(map) => { | ||
| if let Some(JsonValue::Object(req_map)) = map.get("request") { | ||
| if let Some(JsonValue::String(p)) = req_map.get("payload") { | ||
| p.clone() | ||
| } else { | ||
| return Err("Missing 'payload' in 'request' object".into()); | ||
| } | ||
| } else if let Some(JsonValue::String(p)) = map.get("payload") { | ||
| p.clone() | ||
| } else { | ||
| return Err("Missing 'payload' field in multisigned request".into()); | ||
| } | ||
| } | ||
| _ => return Err("Multisigned request must be a JSON object".into()), | ||
| }; | ||
|
QZHelen marked this conversation as resolved.
|
||
|
|
||
| if payload.is_empty() { | ||
| return Err("Empty payload in multisigned request".into()); | ||
| } | ||
|
|
||
| Ok(payload) | ||
| } | ||
|
|
||
| fn parse_protocol_request_data<'a>( | ||
| pr: &'a ProtocolRequest, | ||
| ) -> Result<Cow<'a, OpenId4VpData>, Box<dyn std::error::Error>> { | ||
|
|
@@ -36,7 +77,14 @@ fn parse_protocol_request_data<'a>( | |
| return Ok(Cow::Owned(DeJson::deserialize_json(std::str::from_utf8( | ||
| &decoded, | ||
| )?)?)); | ||
| } | ||
| } else if pr.protocol == "openid4vp-v1-multisigned" { | ||
| log::debug!("Handling multisigned OpenID4VP request"); | ||
| let payload_str = extract_multisigned_payload(pr)?; | ||
| let decoded = decode_base64url(&payload_str)?; | ||
| return Ok(Cow::Owned(DeJson::deserialize_json(std::str::from_utf8( | ||
| &decoded, | ||
| )?)?)); | ||
| } | ||
|
|
||
| log::debug!("Handling unsigned OpenID4VP request"); | ||
| if let Some(data) = &pr.data { | ||
|
|
@@ -110,7 +158,10 @@ pub fn openid4vp_main(credman: &mut impl CredmanApi) -> Result<(), Box<dyn std:: | |
| continue; | ||
| } | ||
| log::debug!("Processing request {}: protocol={}", i, pr.protocol); | ||
| if pr.protocol != "openid4vp-v1-unsigned" && pr.protocol != "openid4vp-v1-signed" { | ||
| if pr.protocol != "openid4vp-v1-unsigned" | ||
| && pr.protocol != "openid4vp-v1-signed" | ||
| && pr.protocol != "openid4vp-v1-multisigned" | ||
| { | ||
| log::warn!("Unsupported protocol: {}", pr.protocol); | ||
| continue; | ||
| } | ||
|
|
@@ -173,6 +224,28 @@ mod tests { | |
| assert!(err.to_string().contains("Missing unsigned request data")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_parse_protocol_request_data_multisigned_valid() { | ||
| let json = r#"{ | ||
| "protocol": "openid4vp-v1-multisigned", | ||
| "data": "{\"request\": {\"payload\": \"eyJkY3FsX3F1ZXJ5Ijp7ImNyZWRlbnRpYWxzIjpbXX19\"}}" | ||
| }"#; | ||
| let pr: ProtocolRequest = DeJson::deserialize_json(json).unwrap(); | ||
| let data = parse_protocol_request_data(&pr).unwrap(); | ||
| assert!(data.dcql_query.is_some()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_parse_protocol_request_data_multisigned_invalid_payload() { | ||
| let json = r#"{ | ||
| "protocol": "openid4vp-v1-multisigned", | ||
| "data": "{\"request\": {}}" | ||
| }"#; | ||
| let pr: ProtocolRequest = DeJson::deserialize_json(json).unwrap(); | ||
| let err = parse_protocol_request_data(&pr).unwrap_err(); | ||
| assert!(err.to_string().contains("Missing 'payload'")); | ||
| } | ||
|
|
||
| use crate::test_utils::*; | ||
|
|
||
| macro_rules! define_test { | ||
|
|
@@ -221,6 +294,7 @@ mod tests { | |
| ); | ||
| define_test!(tc30_parse_v1_unsigned, "TC30_ParseV1Unsigned"); | ||
| define_test!(tc31_parse_v1_signed, "TC31_ParseV1Signed"); | ||
| define_test!(tc42_parse_v1_multisigned, "TC42_ParseV1Multisigned"); | ||
| define_test!(tc32_extract_payment_sca1, "TC32_ExtractPaymentSca1"); | ||
| define_test!(tc33_extract_payment_details, "TC33_ExtractPaymentDetails"); | ||
| define_test!(tc34_extract_payment_generic, "TC34_ExtractPaymentGeneric"); | ||
|
|
||
139 changes: 139 additions & 0 deletions
139
matcher-rs/testdata/TC42_ParseV1Multisigned_expected.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| { | ||
| "entrySets": { | ||
| "req:0;null": { | ||
| "entries": { | ||
| "0": { | ||
| "mdoc_cred_1": { | ||
| "additional_info": "", | ||
| "credId": "mdoc_cred_1", | ||
| "disclaimer": "", | ||
| "fields": [ | ||
| [ | ||
| "Family Name", | ||
| "Doe" | ||
| ], | ||
| [ | ||
| "Given Name", | ||
| "John" | ||
| ], | ||
| [ | ||
| "Age", | ||
| "" | ||
| ], | ||
| [ | ||
| "Over 21", | ||
| "Yes" | ||
| ] | ||
| ], | ||
| "merchant_name": "", | ||
| "metadata_display_text": "", | ||
| "subtitle": "", | ||
| "title": "John's Driving License", | ||
| "transaction_amount": "", | ||
| "type": "Verification", | ||
| "warning": "" | ||
| }, | ||
| "mdoc_cred_3": { | ||
| "additional_info": "", | ||
| "credId": "mdoc_cred_3", | ||
| "disclaimer": "", | ||
| "fields": [ | ||
| [ | ||
| "Family Name", | ||
| "" | ||
| ], | ||
| [ | ||
| "Given Name", | ||
| "" | ||
| ], | ||
| [ | ||
| "Age", | ||
| "" | ||
| ], | ||
| [ | ||
| "Over 21", | ||
| "" | ||
| ] | ||
| ], | ||
| "merchant_name": "", | ||
| "metadata_display_text": "", | ||
| "subtitle": "", | ||
| "title": "Alice's Driving License", | ||
| "transaction_amount": "", | ||
| "type": "Verification", | ||
| "warning": "" | ||
| }, | ||
| "mdoc_cred_4": { | ||
| "additional_info": "", | ||
| "credId": "mdoc_cred_4", | ||
| "disclaimer": "", | ||
| "fields": [ | ||
| [ | ||
| "Family Name", | ||
| "" | ||
| ], | ||
| [ | ||
| "Given Name", | ||
| "" | ||
| ], | ||
| [ | ||
| "Age", | ||
| "" | ||
| ], | ||
| [ | ||
| "Over 21", | ||
| "" | ||
| ] | ||
| ], | ||
| "merchant_name": "", | ||
| "metadata_display_text": "", | ||
| "subtitle": "", | ||
| "title": "Jane's Driving License", | ||
| "transaction_amount": "", | ||
| "type": "Verification", | ||
| "warning": "" | ||
| }, | ||
| "mdoc_cred_underage": { | ||
| "additional_info": "", | ||
| "credId": "mdoc_cred_underage", | ||
| "disclaimer": "", | ||
| "fields": [ | ||
| [ | ||
| "Age", | ||
| "" | ||
| ], | ||
| [ | ||
| "Over 21", | ||
| "Yes" | ||
| ] | ||
| ], | ||
| "merchant_name": "", | ||
| "metadata_display_text": "", | ||
| "subtitle": "", | ||
| "title": "Underage License", | ||
| "transaction_amount": "", | ||
| "type": "Verification", | ||
| "warning": "" | ||
| } | ||
| } | ||
| }, | ||
| "setId": "req:0;null", | ||
| "setLength": 1 | ||
| } | ||
| }, | ||
| "standaloneEntries": [ | ||
| { | ||
| "additional_info": "", | ||
| "credId": "issuance_mdl_1", | ||
| "disclaimer": "", | ||
| "fields": [], | ||
| "merchant_name": "", | ||
| "metadata_display_text": "", | ||
| "subtitle": "From your local DMV", | ||
| "title": "Get a New mDL", | ||
| "transaction_amount": "", | ||
| "type": "InlineIssuance", | ||
| "warning": "" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "requests": [ | ||
| { | ||
| "data": { | ||
| "request": "{\"payload\":\"eyJkY3FsX3F1ZXJ5Ijp7ImNyZWRlbnRpYWxzIjpbeyJmb3JtYXQiOiJtc29fbWRvYyIsImlkIjoibWRsIiwibWV0YSI6eyJkb2N0eXBlX3ZhbHVlIjoib3JnLmlzby4xODAxMy41LjEubURMIn19XX19\"}" | ||
| }, | ||
| "protocol": "openid4vp-v1-multisigned" | ||
| } | ||
| ] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole blocks
let json_str = ...looks a lot like the ones in the beginning ofparse_protocol_request_data. Shall we extract them out into a function?