From 58bbc0aaf3e3e7f4b080509e806b2c0c9c2b1664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Pra=C5=BE=C3=A1k?= Date: Wed, 17 Apr 2024 12:17:59 +0200 Subject: [PATCH] refactor: improve mediator routes (#1176) Signed-off-by: Ondrej Prazak Co-authored-by: Ondrej Prazak --- Cargo.lock | 7 -- aries/agents/mediator/Cargo.toml | 1 - aries/agents/mediator/README.md | 9 +-- aries/agents/mediator/src/http_routes/mod.rs | 29 +-------- .../tests/common/agent_and_transport_utils.rs | 6 +- .../tests/mediator-aries-connection.rs | 8 +-- .../mediator/tests/mediator-oob-invitation.rs | 64 +------------------ 7 files changed, 11 insertions(+), 113 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ee133afd4c..97796f4841 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2294,12 +2294,6 @@ dependencies = [ "synstructure", ] -[[package]] -name = "fast_qr" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcb3b08010bdea9b4ab23deabd626509ee241a793b7da9148d9101c677adcf4d" - [[package]] name = "fastrand" version = "1.9.0" @@ -3423,7 +3417,6 @@ dependencies = [ "diddoc_legacy", "dotenvy", "env_logger 0.10.0", - "fast_qr", "futures", "log", "messages", diff --git a/aries/agents/mediator/Cargo.toml b/aries/agents/mediator/Cargo.toml index 8d48336b49..d354ea8fcb 100644 --- a/aries/agents/mediator/Cargo.toml +++ b/aries/agents/mediator/Cargo.toml @@ -21,7 +21,6 @@ axum-macros = "0.3.8" diddoc_legacy = { path = "../../misc/legacy/diddoc_legacy" } dotenvy = "0.15" env_logger = "0.10.0" -fast_qr = { version = "0.10.2", features = ["svg"] } futures = "0.3.28" log = "0.4.20" messages = { path = "../../messages" } diff --git a/aries/agents/mediator/README.md b/aries/agents/mediator/README.md index 20ae684842..8f09f16b79 100644 --- a/aries/agents/mediator/README.md +++ b/aries/agents/mediator/README.md @@ -119,13 +119,10 @@ cargo build --package mediator --no-default-features --release Currently exposed endpoints. ```yaml -`/register`: -- **Description** : | +`/invitation`: +- **Description** : | + Returns OOB invitation in json format. Shows an Aries Out Of Band (OOB) invitation which can be used to connect to the mediator using a conformant Aries Agent. - Use `Accept` header with value "application/json" to receive same in json format. - -`/register.json`: -- **Description** : Returns OOB invitation in json format. ``` ```yaml diff --git a/aries/agents/mediator/src/http_routes/mod.rs b/aries/agents/mediator/src/http_routes/mod.rs index 30289dbeea..29d748fba7 100644 --- a/aries/agents/mediator/src/http_routes/mod.rs +++ b/aries/agents/mediator/src/http_routes/mod.rs @@ -18,32 +18,6 @@ use crate::{ persistence::MediatorPersistence, }; -pub async fn oob_invite_qr( - headers: HeaderMap, - State(agent): State>, -) -> Response { - let Json(oob_json) = oob_invite_json(State(agent)).await; - match detect_mime_type(&headers) { - "application/json" => Json(oob_json).into_response(), - _ => { - let oob_string = serde_json::to_string_pretty(&oob_json).unwrap(); - let qr = fast_qr::QRBuilder::new(oob_string.clone()).build().unwrap(); - let oob_qr_svg = fast_qr::convert::svg::SvgBuilder::default().to_str(&qr); - Html(format!( - " - {oob_qr_svg}
-
{oob_string}
" - )) - .into_response() - } - } -} - fn detect_mime_type(headers: &HeaderMap) -> &str { headers .get(ACCEPT) @@ -89,8 +63,7 @@ pub async fn build_router( ) -> Router { Router::default() .route("/", get(readme)) - .route("/register", get(oob_invite_qr)) - .route("/register.json", get(oob_invite_json)) + .route("/invitation", get(oob_invite_json)) .route("/didcomm", get(handle_didcomm).post(handle_didcomm)) .layer(tower_http::catch_panic::CatchPanicLayer::new()) .with_state(Arc::new(agent)) diff --git a/aries/agents/mediator/tests/common/agent_and_transport_utils.rs b/aries/agents/mediator/tests/common/agent_and_transport_utils.rs index 70ba7691ce..1e0b84e569 100644 --- a/aries/agents/mediator/tests/common/agent_and_transport_utils.rs +++ b/aries/agents/mediator/tests/common/agent_and_transport_utils.rs @@ -23,7 +23,6 @@ use messages::{ }, AriesMessage, }; -use reqwest::header::ACCEPT; use test_utils::mockdata::mock_ledger::MockLedger; use super::prelude::*; @@ -36,17 +35,16 @@ pub async fn didcomm_connection( ) -> Result> { let client = reqwest::Client::new(); let base: Url = ENDPOINT_ROOT.parse().unwrap(); - let endpoint_register = base.join("register").unwrap(); + let endpoint_register = base.join("invitation").unwrap(); let oobi: OOBInvitation = client .get(endpoint_register) - .header(ACCEPT, "application/json") .send() .await? .error_for_status()? .json() .await?; - info!("Got invitation from register endpoint {:?}", oobi); + info!("Got invitation {:?}", oobi); let state: InviteeConnection = agent.establish_connection(oobi, aries_transport).await?; diff --git a/aries/agents/mediator/tests/mediator-aries-connection.rs b/aries/agents/mediator/tests/mediator-aries-connection.rs index 2e4f55758b..1f5ce29f05 100644 --- a/aries/agents/mediator/tests/mediator-aries-connection.rs +++ b/aries/agents/mediator/tests/mediator-aries-connection.rs @@ -2,7 +2,6 @@ mod common; use aries_vcx_wallet::wallet::indy::IndySdkWallet; use messages::msg_fields::protocols::out_of_band::invitation::Invitation as OOBInvitation; -use reqwest::header::ACCEPT; use crate::common::{prelude::*, test_setup::setup_env_logging}; @@ -15,18 +14,17 @@ async fn didcomm_connection_succeeds() -> Result<()> { LOGGING_INIT.call_once(setup_env_logging); let client = reqwest::Client::new(); let base: Url = ENDPOINT_ROOT.parse().unwrap(); - let endpoint_register = base.join("register").unwrap(); + let endpoint_invitation = base.join("invitation").unwrap(); let oobi: OOBInvitation = client - .get(endpoint_register) - .header(ACCEPT, "application/json") + .get(endpoint_invitation) .send() .await? .error_for_status()? .json() .await?; info!( - "Got invitation from register endpoint {}", + "Got invitation {}", serde_json::to_string_pretty(&oobi.clone()).unwrap() ); let agent = mediator::aries_agent::AgentBuilder::::new_demo_agent().await?; diff --git a/aries/agents/mediator/tests/mediator-oob-invitation.rs b/aries/agents/mediator/tests/mediator-oob-invitation.rs index 0ce9a0887a..7e8ff0e886 100644 --- a/aries/agents/mediator/tests/mediator-oob-invitation.rs +++ b/aries/agents/mediator/tests/mediator-oob-invitation.rs @@ -2,7 +2,6 @@ mod common; use anyhow::Result; use messages::msg_fields::protocols::out_of_band::invitation::Invitation as OOBInvitation; -use reqwest::header::{ACCEPT, CONTENT_TYPE}; use url::Url; use crate::common::{prelude::*, test_setup::setup_env_logging}; @@ -12,12 +11,12 @@ static LOGGING_INIT: std::sync::Once = std::sync::Once::new(); const ENDPOINT_ROOT: &str = "http://localhost:8005"; #[test] -fn endpoint_register_json_returns_oob() -> Result<()> { +fn endpoint_invitation_returns_oob() -> Result<()> { LOGGING_INIT.call_once(setup_env_logging); let client = reqwest::blocking::Client::new(); let base: Url = ENDPOINT_ROOT.parse().unwrap(); - let endpoint_register_json = base.join("/register.json").unwrap(); + let endpoint_register_json = base.join("/invitation").unwrap(); let res = client .get(endpoint_register_json) @@ -29,62 +28,3 @@ fn endpoint_register_json_returns_oob() -> Result<()> { Ok(()) } - -#[test] -fn endpoint_register_returns_oob_with_correct_accept_header() -> Result<()> { - LOGGING_INIT.call_once(setup_env_logging); - - let client = reqwest::blocking::Client::new(); - let base: Url = ENDPOINT_ROOT.parse().unwrap(); - let endpoint_register = base.join("/register").unwrap(); - - let res = client - .get(endpoint_register) - .header(ACCEPT, "application/json") - .send()? - .error_for_status()?; - info!("{:?}", res); - - let _oob: OOBInvitation = res.json()?; - - Ok(()) -} - -#[test] -fn endpoint_register_returns_html_page() -> Result<()> { - LOGGING_INIT.call_once(setup_env_logging); - - let client = reqwest::blocking::Client::new(); - let base: Url = ENDPOINT_ROOT.parse().unwrap(); - let endpoint_register = base.join("/register").unwrap(); - - let res = client.get(endpoint_register).send()?.error_for_status()?; - info!("{:?}", res); - - assert!(res - .headers() - .get(CONTENT_TYPE) - .unwrap() - .to_str() - .unwrap() - .contains("text/html")); - - Ok(()) -} - -#[test] -#[ignore] -fn endpoint_register_returns_html_page_with_valid_oob_qr() -> Result<()> { - LOGGING_INIT.call_once(setup_env_logging); - - let client = reqwest::blocking::Client::new(); - let base: Url = ENDPOINT_ROOT.parse().unwrap(); - let endpoint_register = base.join("/register").unwrap(); - - let res = client.get(endpoint_register).send()?.error_for_status()?; - info!("{:?}", res); - - let _html = res.text()?; - // validate qr of html page - unimplemented!("validate oob qr of returned html page"); -}