From 042617d792f1432b7883be6744242e7b774fd4fa Mon Sep 17 00:00:00 2001 From: James Ebert Date: Thu, 8 Aug 2024 19:01:23 -0700 Subject: [PATCH] chore: adjust function/method names, added no invitation padding parsing test Signed-off-by: James Ebert --- .../src/handlers/out_of_band.rs | 2 +- .../src/handlers/out_of_band/receiver.rs | 85 ++++++++++++------- .../src/handlers/out_of_band/sender.rs | 20 ++--- 3 files changed, 66 insertions(+), 41 deletions(-) diff --git a/aries/agents/aries-vcx-agent/src/handlers/out_of_band.rs b/aries/agents/aries-vcx-agent/src/handlers/out_of_band.rs index 6d01745a55..9aa8558fe7 100644 --- a/aries/agents/aries-vcx-agent/src/handlers/out_of_band.rs +++ b/aries/agents/aries-vcx-agent/src/handlers/out_of_band.rs @@ -52,7 +52,7 @@ impl ServiceOutOfBand { GenericOutOfBand::Sender(sender.to_owned()), )?; - Ok(sender.to_aries_message()) + Ok(sender.invitation_to_aries_message()) } pub fn receive_invitation(&self, invitation: AriesMessage) -> AgentResult { diff --git a/aries/aries_vcx/src/handlers/out_of_band/receiver.rs b/aries/aries_vcx/src/handlers/out_of_band/receiver.rs index 77b5ea23b8..6ae138f57e 100644 --- a/aries/aries_vcx/src/handlers/out_of_band/receiver.rs +++ b/aries/aries_vcx/src/handlers/out_of_band/receiver.rs @@ -41,22 +41,28 @@ impl OutOfBandReceiver { } } - pub fn from_json_string(oob_json: &str) -> VcxResult { + pub fn create_from_json_encoded_oob(oob_json: &str) -> VcxResult { Ok(Self { - oob: from_json_string(oob_json)?, + oob: extract_encoded_invitation_from_json_string(oob_json)?, }) } - pub fn from_base64_url(base64_url_encoded_oob: &str) -> VcxResult { + fn create_from_base64_url_encoded_oob(base64_url_encoded_oob: &str) -> VcxResult { Ok(Self { - oob: from_json_string(&from_base64_url(base64_url_encoded_oob)?)?, + oob: extract_encoded_invitation_from_json_string( + &extract_encoded_invitation_from_base64_url(base64_url_encoded_oob)?, + )?, }) } - pub fn from_url(oob_url_string: &str) -> VcxResult { + pub fn create_from_url_encoded_oob(oob_url_string: &str) -> VcxResult { // TODO - URL Shortening Ok(Self { - oob: from_json_string(&from_base64_url(&from_url(oob_url_string)?)?)?, + oob: extract_encoded_invitation_from_json_string( + &extract_encoded_invitation_from_base64_url(&extract_encoded_invitation_from_url( + oob_url_string, + )?)?, + )?, }) } @@ -80,37 +86,37 @@ impl OutOfBandReceiver { } } - pub fn to_aries_message(&self) -> AriesMessage { + pub fn invitation_to_aries_message(&self) -> AriesMessage { self.oob.clone().into() } - pub fn to_json_string(&self) -> String { - self.to_aries_message().to_string() + pub fn invitation_to_json_string(&self) -> String { + self.invitation_to_aries_message().to_string() } - pub fn to_base64_url(&self) -> String { - BASE64_URL_SAFE.encode(self.to_json_string()) + fn invitation_to_base64_url(&self) -> String { + BASE64_URL_SAFE.encode(self.invitation_to_json_string()) } - pub fn to_url(&self, domain_path: &str) -> VcxResult { + pub fn invitation_to_url(&self, domain_path: &str) -> VcxResult { let mut oob_url = Url::parse(domain_path)?; - let oob_query = "oob=".to_owned() + &self.to_base64_url(); + let oob_query = "oob=".to_owned() + &self.invitation_to_base64_url(); oob_url.set_query(Some(&oob_query)); Ok(oob_url) } } -fn from_json_string(oob_json: &str) -> VcxResult { +fn extract_encoded_invitation_from_json_string(oob_json: &str) -> VcxResult { Ok(serde_json::from_str(oob_json)?) } -fn from_base64_url(base64_url_encoded_oob: &str) -> VcxResult { +fn extract_encoded_invitation_from_base64_url(base64_url_encoded_oob: &str) -> VcxResult { Ok(String::from_utf8( URL_SAFE_LENIENT.decode(base64_url_encoded_oob)?, )?) } -fn from_url(oob_url_string: &str) -> VcxResult { +fn extract_encoded_invitation_from_url(oob_url_string: &str) -> VcxResult { let oob_url = Url::parse(oob_url_string)?; let (_oob_query, base64_url_encoded_oob) = oob_url .query_pairs() @@ -222,6 +228,7 @@ mod tests { }"#; const JSON_OOB_INVITE_NO_WHITESPACE: &str = r#"{"@type":"https://didcomm.org/out-of-band/1.1/invitation","@id":"69212a3a-d068-4f9d-a2dd-4741bca89af3","label":"Faber College","goal_code":"issue-vc","goal":"To issue a Faber College Graduate credential","handshake_protocols":["https://didcomm.org/didexchange/1.0","https://didcomm.org/connections/1.0"],"services":["did:sov:LjgpST2rjsoxYegQDRm7EL"]}"#; const OOB_BASE64_URL_ENCODED: &str = "eyJAdHlwZSI6Imh0dHBzOi8vZGlkY29tbS5vcmcvb3V0LW9mLWJhbmQvMS4xL2ludml0YXRpb24iLCJAaWQiOiI2OTIxMmEzYS1kMDY4LTRmOWQtYTJkZC00NzQxYmNhODlhZjMiLCJsYWJlbCI6IkZhYmVyIENvbGxlZ2UiLCJnb2FsX2NvZGUiOiJpc3N1ZS12YyIsImdvYWwiOiJUbyBpc3N1ZSBhIEZhYmVyIENvbGxlZ2UgR3JhZHVhdGUgY3JlZGVudGlhbCIsImhhbmRzaGFrZV9wcm90b2NvbHMiOlsiaHR0cHM6Ly9kaWRjb21tLm9yZy9kaWRleGNoYW5nZS8xLjAiLCJodHRwczovL2RpZGNvbW0ub3JnL2Nvbm5lY3Rpb25zLzEuMCJdLCJzZXJ2aWNlcyI6WyJkaWQ6c292OkxqZ3BTVDJyanNveFllZ1FEUm03RUwiXX0="; + const OOB_BASE64_URL_ENCODED_NO_PADDING: &str = "eyJAdHlwZSI6Imh0dHBzOi8vZGlkY29tbS5vcmcvb3V0LW9mLWJhbmQvMS4xL2ludml0YXRpb24iLCJAaWQiOiI2OTIxMmEzYS1kMDY4LTRmOWQtYTJkZC00NzQxYmNhODlhZjMiLCJsYWJlbCI6IkZhYmVyIENvbGxlZ2UiLCJnb2FsX2NvZGUiOiJpc3N1ZS12YyIsImdvYWwiOiJUbyBpc3N1ZSBhIEZhYmVyIENvbGxlZ2UgR3JhZHVhdGUgY3JlZGVudGlhbCIsImhhbmRzaGFrZV9wcm90b2NvbHMiOlsiaHR0cHM6Ly9kaWRjb21tLm9yZy9kaWRleGNoYW5nZS8xLjAiLCJodHRwczovL2RpZGNvbW0ub3JnL2Nvbm5lY3Rpb25zLzEuMCJdLCJzZXJ2aWNlcyI6WyJkaWQ6c292OkxqZ3BTVDJyanNveFllZ1FEUm03RUwiXX0"; const OOB_URL: &str = "http://example.com/ssi?oob=eyJAdHlwZSI6Imh0dHBzOi8vZGlkY29tbS5vcmcvb3V0LW9mLWJhbmQvMS4xL2ludml0YXRpb24iLCJAaWQiOiI2OTIxMmEzYS1kMDY4LTRmOWQtYTJkZC00NzQxYmNhODlhZjMiLCJsYWJlbCI6IkZhYmVyIENvbGxlZ2UiLCJnb2FsX2NvZGUiOiJpc3N1ZS12YyIsImdvYWwiOiJUbyBpc3N1ZSBhIEZhYmVyIENvbGxlZ2UgR3JhZHVhdGUgY3JlZGVudGlhbCIsImhhbmRzaGFrZV9wcm90b2NvbHMiOlsiaHR0cHM6Ly9kaWRjb21tLm9yZy9kaWRleGNoYW5nZS8xLjAiLCJodHRwczovL2RpZGNvbW0ub3JnL2Nvbm5lY3Rpb25zLzEuMCJdLCJzZXJ2aWNlcyI6WyJkaWQ6c292OkxqZ3BTVDJyanNveFllZ1FEUm03RUwiXX0="; // Params mimic example invitation in RFC 0434 - https://github.com/hyperledger/aries-rfcs/tree/main/features/0434-outofband @@ -258,7 +265,7 @@ mod tests { #[test] fn receive_invitation_by_json() { let base_invite = _create_invitation(); - let parsed_invite = OutOfBandReceiver::from_json_string(JSON_OOB_INVITE) + let parsed_invite = OutOfBandReceiver::create_from_json_encoded_oob(JSON_OOB_INVITE) .unwrap() .oob; assert_eq!(base_invite, parsed_invite); @@ -267,52 +274,70 @@ mod tests { #[test] fn receive_invitation_by_json_no_whitespace() { let base_invite = _create_invitation(); - let parsed_invite = OutOfBandReceiver::from_json_string(JSON_OOB_INVITE_NO_WHITESPACE) - .unwrap() - .oob; + let parsed_invite = + OutOfBandReceiver::create_from_json_encoded_oob(JSON_OOB_INVITE_NO_WHITESPACE) + .unwrap() + .oob; assert_eq!(base_invite, parsed_invite); } #[test] fn receive_invitation_by_base64_url() { let base_invite = _create_invitation(); - let parsed_invite = OutOfBandReceiver::from_base64_url(OOB_BASE64_URL_ENCODED) - .unwrap() - .oob; + let parsed_invite = + OutOfBandReceiver::create_from_base64_url_encoded_oob(OOB_BASE64_URL_ENCODED) + .unwrap() + .oob; + assert_eq!(base_invite, parsed_invite); + } + + #[test] + fn receive_invitation_by_base64_url_no_padding() { + let base_invite = _create_invitation(); + let parsed_invite = OutOfBandReceiver::create_from_base64_url_encoded_oob( + OOB_BASE64_URL_ENCODED_NO_PADDING, + ) + .unwrap() + .oob; assert_eq!(base_invite, parsed_invite); } #[test] fn receive_invitation_by_url() { let base_invite = _create_invitation(); - let parsed_invite = OutOfBandReceiver::from_url(OOB_URL).unwrap().oob; + let parsed_invite = OutOfBandReceiver::create_from_url_encoded_oob(OOB_URL) + .unwrap() + .oob; assert_eq!(base_invite, parsed_invite); } #[test] fn invitation_to_json() { - let out_of_band_receiver = OutOfBandReceiver::from_json_string(JSON_OOB_INVITE).unwrap(); + let out_of_band_receiver = + OutOfBandReceiver::create_from_json_encoded_oob(JSON_OOB_INVITE).unwrap(); - let json_invite = out_of_band_receiver.to_json_string(); + let json_invite = out_of_band_receiver.invitation_to_json_string(); assert_eq!(JSON_OOB_INVITE_NO_WHITESPACE, json_invite); } #[test] fn invitation_to_base64_url() { - let out_of_band_receiver = OutOfBandReceiver::from_json_string(JSON_OOB_INVITE).unwrap(); + let out_of_band_receiver = + OutOfBandReceiver::create_from_json_encoded_oob(JSON_OOB_INVITE).unwrap(); - let base64_url_invite = out_of_band_receiver.to_base64_url(); + let base64_url_invite = out_of_band_receiver.invitation_to_base64_url(); assert_eq!(OOB_BASE64_URL_ENCODED, base64_url_invite); } #[test] fn invitation_to_url() { - let out_of_band_receiver = OutOfBandReceiver::from_json_string(JSON_OOB_INVITE).unwrap(); + let out_of_band_receiver = + OutOfBandReceiver::create_from_json_encoded_oob(JSON_OOB_INVITE).unwrap(); let oob_url = out_of_band_receiver - .to_url("http://example.com/ssi") + .invitation_to_url("http://example.com/ssi") .unwrap() .to_string(); diff --git a/aries/aries_vcx/src/handlers/out_of_band/sender.rs b/aries/aries_vcx/src/handlers/out_of_band/sender.rs index bbb43af22c..a058c11ad2 100644 --- a/aries/aries_vcx/src/handlers/out_of_band/sender.rs +++ b/aries/aries_vcx/src/handlers/out_of_band/sender.rs @@ -130,21 +130,21 @@ impl OutOfBandSender { Ok(self) } - pub fn to_aries_message(&self) -> AriesMessage { + pub fn invitation_to_aries_message(&self) -> AriesMessage { self.oob.clone().into() } - pub fn to_json_string(&self) -> String { - self.to_aries_message().to_string() + pub fn invitation_to_json_string(&self) -> String { + self.invitation_to_aries_message().to_string() } - pub fn to_base64_url(&self) -> String { - BASE64_URL_SAFE.encode(self.to_json_string()) + fn invitation_to_base64_url(&self) -> String { + BASE64_URL_SAFE.encode(self.invitation_to_json_string()) } - pub fn to_url(&self, domain_path: &str) -> VcxResult { + pub fn invitation_to_url(&self, domain_path: &str) -> VcxResult { let mut oob_url = Url::parse(domain_path)?; - let oob_query = "oob=".to_owned() + &self.to_base64_url(); + let oob_query = "oob=".to_owned() + &self.invitation_to_base64_url(); oob_url.set_query(Some(&oob_query)); Ok(oob_url) } @@ -213,7 +213,7 @@ mod tests { fn invitation_to_json() { let out_of_band_sender = OutOfBandSender::create_from_invitation(_create_invitation()); - let json_invite = out_of_band_sender.to_json_string(); + let json_invite = out_of_band_sender.invitation_to_json_string(); assert_eq!(JSON_OOB_INVITE_NO_WHITESPACE, json_invite); } @@ -222,7 +222,7 @@ mod tests { fn invitation_to_base64_url() { let out_of_band_sender = OutOfBandSender::create_from_invitation(_create_invitation()); - let base64_url_invite = out_of_band_sender.to_base64_url(); + let base64_url_invite = out_of_band_sender.invitation_to_base64_url(); assert_eq!(OOB_BASE64_URL_ENCODED, base64_url_invite); } @@ -232,7 +232,7 @@ mod tests { let out_of_band_sender = OutOfBandSender::create_from_invitation(_create_invitation()); let oob_url = out_of_band_sender - .to_url("http://example.com/ssi") + .invitation_to_url("http://example.com/ssi") .unwrap() .to_string();