Skip to content

Commit

Permalink
clean up the dynamic didcomm messages dispatch implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Hermann-Core committed Nov 27, 2024
1 parent f8faee6 commit f111f96
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use axum::{http::StatusCode, response::IntoResponse, Json};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum DiscoveryError {
pub(crate) enum DiscoveryError {
#[error("message body is malformed")]
MalformedBody,
#[error("No queries field in body")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use uuid::Uuid;

// handle discover feature request
// https://didcomm.org/discover-features/2.0/
pub async fn handle_query_request(
pub(crate) async fn handle_query_request(
state: Arc<AppState>,
message: Message,
) -> Result<Option<Message>, DiscoveryError> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
mod constants;
mod errors;
mod handler;
mod model;

pub mod handler;
pub mod plugin;

// Re-exports
pub use errors::DiscoveryError;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use hyper::StatusCode;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum ForwardError {
pub(crate) enum ForwardError {
#[error("message body is malformed")]
MalformedBody,
#[error("Uncoordinated sender")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ForwardError;
use crate::error::ForwardError;
use database::Repository;
use didcomm::{AttachmentData, Message};
use mongodb::bson::doc;
Expand All @@ -11,7 +11,7 @@ use std::sync::Arc;

/// Mediator receives forwarded messages, extract the next field in the message body, and the attachments in the message
/// then stores the attachment with the next field as key for pickup
pub async fn mediator_forward_process(
pub(crate) async fn mediator_forward_process(
state: Arc<AppState>,
message: Message,
) -> Result<Option<Message>, ForwardError> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
mod constants;
mod error;
mod handler;

pub mod handler;
pub mod plugin;

// Re-exports
pub use error::ForwardError;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use thiserror::Error;

/// Represents errors that can occur during mediation.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum MediationError {
pub(crate) enum MediationError {
#[error("No return route all decoration")]
NoReturnRouteAllDecoration,
#[error("invalid message type")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::sync::Arc;
use uuid::Uuid;

/// Process a DIDComm mediate request
pub async fn process_mediate_request(
pub(crate) async fn process_mediate_request(
state: Arc<AppState>,
plain_message: Message,
) -> Result<Option<Message>, MediationError> {
Expand Down Expand Up @@ -202,7 +202,7 @@ fn generate_did_peer(service_endpoint: String) -> (String, Ed25519KeyPair, X2551
)
}

pub async fn process_plain_keylist_update_message(
pub(crate) async fn process_plain_keylist_update_message(
state: Arc<AppState>,
message: Message,
) -> Result<Option<Message>, MediationError> {
Expand Down Expand Up @@ -330,7 +330,7 @@ pub async fn process_plain_keylist_update_message(
))
}

pub async fn process_plain_keylist_query_message(
pub(crate) async fn process_plain_keylist_query_message(
state: Arc<AppState>,
message: Message,
) -> Result<Option<Message>, MediationError> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
mod constants;
mod errors;
mod handler;
mod jose;
mod model;

pub mod client;
pub mod handler;
pub mod plugin;

// Re-exports
pub use errors::MediationError;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use axum::{http::StatusCode, response::IntoResponse, Json};
use thiserror::Error;

#[derive(Debug, Error, PartialEq)]
pub enum PickupError {
pub(crate) enum PickupError {
#[error("Missing sender DID")]
MissingSenderDID,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{str::FromStr, sync::Arc};
use uuid::Uuid;

// Process pickup status request
pub async fn handle_status_request(
pub(crate) async fn handle_status_request(
state: Arc<AppState>,
message: Message,
) -> Result<Option<Message>, PickupError> {
Expand Down Expand Up @@ -60,7 +60,7 @@ pub async fn handle_status_request(
}

// Process pickup delivery request
pub async fn handle_delivery_request(
pub(crate) async fn handle_delivery_request(
state: Arc<AppState>,
message: Message,
) -> Result<Option<Message>, PickupError> {
Expand Down Expand Up @@ -136,7 +136,7 @@ pub async fn handle_delivery_request(
}

// Process pickup messages acknowledgement
pub async fn handle_message_acknowledgement(
pub(crate) async fn handle_message_acknowledgement(
state: Arc<AppState>,
message: Message,
) -> Result<Option<Message>, PickupError> {
Expand Down Expand Up @@ -200,7 +200,7 @@ pub async fn handle_message_acknowledgement(
}

// Process live delivery change request
pub async fn handle_live_delivery_change(
pub(crate) async fn handle_live_delivery_change(
state: Arc<AppState>,
message: Message,
) -> Result<Option<Message>, PickupError> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
mod constants;
mod error;
mod handler;
mod model;

pub mod handler;
pub mod plugin;

// Re-exports
pub use error::PickupError;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::Serialize;
use thiserror::Error;

#[derive(Debug, Serialize, Error, PartialEq, Eq)]
pub enum TrustPingError {
pub(crate) enum TrustPingError {
#[error("Missing sender DID")]
MissingSenderDID,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use shared::state::AppState;

use crate::{constants::TRUST_PING_RESPONSE_2_0, error::TrustPingError, model::TrustPingResponse};

pub async fn handle_trust_ping(
pub(crate) async fn handle_trust_ping(
state: Arc<AppState>,
message: Message,
) -> Result<Option<Message>, TrustPingError> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
mod constants;
mod error;
mod handler;
mod model;

pub mod handler;
pub mod plugin;

// Re-exports
pub use error::TrustPingError;
4 changes: 2 additions & 2 deletions crates/web-plugins/didcomm-messaging/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Plugin for DidcommMessaging {
did_endpoint::validate_diddoc(env.storage_dirpath.as_ref(), &keystore, &mut filesystem)
{
return Err(PluginError::InitError(format!(
"diddoc validation failed: {:?}",
"DID document validation failed: {:?}",
err
)));
}
Expand All @@ -70,7 +70,7 @@ impl Plugin for DidcommMessaging {
let mut container = MessagePluginContainer::new();
if let Err(err) = container.load() {
return Err(PluginError::InitError(format!(
"Error loading message container: {:?}",
"Error loading didcomm messages container: {:?}",
err
)));
}
Expand Down
67 changes: 27 additions & 40 deletions crates/web-plugins/didcomm-messaging/src/web/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,55 +59,20 @@ async fn process_response(

#[cfg(test)]
mod tests {
use std::vec;

use super::*;
use crate::manager::MessagePluginContainer;
use axum::Router;
use hyper::{Body, Method, Request};
use mediator_coordination::handler;
use message_api::{MessageHandler, MessagePlugin, MessageRouter};
use once_cell::sync::Lazy;
use serde_json::{json, Value};
use shared::{
repository::tests::MockConnectionRepository, state::AppStateRepository,
utils::tests_utils::tests as global,
};
use shared::utils::tests_utils::tests as global;
use tokio::sync::RwLock;
use tower::ServiceExt;

#[allow(clippy::needless_update)]
pub fn setup() -> (Router, Arc<AppState>) {
let state = global::setup();

let mut state = match Arc::try_unwrap(state) {
Ok(state) => state,
Err(_) => panic!(),
};

state.repository = Some(AppStateRepository {
connection_repository: Arc::new(MockConnectionRepository::from(
serde_json::from_str(
r##"[
{
"_id": {
"$oid": "6580701fd2d92bb3cd291b2a"
},
"client_did": "did:key:z6MkfyTREjTxQ8hUwSwBPeDHf3uPL3qCjSSuNPwsyMpWUGH7",
"mediator_did": "did:web:alice-mediator.com:alice_mediator_pub",
"routing_did": "did:key:generated",
"keylist": [
"did:key:alice_identity_pub1@alice_mediator"
]
}
]"##,
)
.unwrap(),
)),
..state.repository.unwrap()
});

let state = Arc::new(state);
let app = crate::web::routes(state.clone());

(app, state)
Expand All @@ -121,12 +86,34 @@ mod tests {
impl MessageHandler for MockKeylistUpdateHandler {
async fn handle(
&self,
state: Arc<AppState>,
_state: Arc<AppState>,
message: Message,
) -> Result<Option<Message>, Response> {
handler::stateful::process_plain_keylist_update_message(state, message)
.await
.map_err(|e| e.into_response())
let response_body = json!({
"updated": [
{
"recipient_did": "did:key:alice_identity_pub1@alice_mediator",
"action": "remove",
"result": "success"
},
{
"recipient_did": "did:key:alice_identity_pub2@alice_mediator",
"action": "add",
"result": "success"
},
]
});

let response = Message::build(
message.id.clone(),
"https://didcomm.org/coordinate-mediation/2.0/keylist-update-response".to_owned(),
response_body,
)
.to(message.from.unwrap())
.from(message.to.unwrap()[0].clone())
.finalize();

Ok(Some(response))
}
}

Expand Down

0 comments on commit f111f96

Please sign in to comment.