Skip to content

Commit

Permalink
Merge pull request #219 from adorsys/203-add-a-messaging-plugin-conta…
Browse files Browse the repository at this point in the history
…iner-with-the-various-messaging-plugins-add-the-plugin-architecture-to-the-didcomm-messaging-route

adding messaging plugin container with the various messaging plugins
  • Loading branch information
Hermann-Core authored Nov 27, 2024
2 parents 09a8fbd + f111f96 commit 8095d32
Show file tree
Hide file tree
Showing 52 changed files with 831 additions and 226 deletions.
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ members = [
"crates/web-plugins/didcomm-messaging/protocols/*",
]


[workspace.dependencies]
database = { path = "./crates/database", version = "0.1.0" }
filesystem = { path = "./crates/filesystem", version = "0.1.0" }
Expand All @@ -37,13 +36,15 @@ oob-messages = { path = "./crates/web-plugins/oob-messages", version = "0.1.0" }
didcomm-messaging = { path = "./crates/web-plugins/didcomm-messaging", version = "0.1.0" }
did-utils = { path = "./crates/web-plugins/didcomm-messaging/did-utils", version = "0.1.0" }
shared = { path = "./crates/web-plugins/didcomm-messaging/shared", version = "0.1.0" }
message-api = { path = "./crates/web-plugins/didcomm-messaging/message-api", version = "0.1.0" }
pickup = { path = "./crates/web-plugins/didcomm-messaging/protocols/pickup", version = "0.1.0" }
forward = { path = "./crates/web-plugins/didcomm-messaging/protocols/forward", version = "0.1.0" }
trust-ping = { path = "./crates/web-plugins/didcomm-messaging/protocols/trust-ping", version = "0.1.0" }
basic-message = { path = "./crates/web-plugins/didcomm-messaging/protocols/basic-message", version = "0.1.0" }
discover-features = { path = "./crates/web-plugins/didcomm-messaging/protocols/discover-features", version = "0.1.0" }
mediator-coordination = { path = "./crates/web-plugins/didcomm-messaging/protocols/mediator-coordination", version = "0.1.0" }


# Other common dependencies
serde = "1.0"
sha2 = "0.10"
Expand Down Expand Up @@ -126,6 +127,12 @@ plugin-did_endpoint = ["dep:did-endpoint"]
plugin-oob_messages = ["dep:oob-messages"]
plugin-didcomm_messaging = ["dep:didcomm-messaging"]

routing = ["plugin-didcomm_messaging", "didcomm-messaging/routing"]
pickup = ["plugin-didcomm_messaging", "didcomm-messaging/pickup"]
trust-ping = ["plugin-didcomm_messaging", "didcomm-messaging/trust-ping"]
discover-features = ["plugin-didcomm_messaging", "didcomm-messaging/discover-features"]
mediator-coordination = ["plugin-didcomm_messaging", "didcomm-messaging/mediator-coordination"]


[dev-dependencies]
tower = { version = "0.4.13", features = ["util"] }
2 changes: 1 addition & 1 deletion crates/keystore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ serde_json.workspace = true
tokio = { workspace = true, features = ["full"] }

[features]
test-utils = []
test-utils = []
32 changes: 27 additions & 5 deletions crates/web-plugins/didcomm-messaging/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,45 @@ keystore.workspace = true
shared.workspace = true
plugin-api.workspace = true
filesystem.workspace = true
forward.workspace = true
pickup.workspace = true
trust-ping.workspace = true
discover-features.workspace = true
mediator-coordination.workspace = true
message-api.workspace = true

# optional dependencies
forward = { workspace = true, optional = true }
pickup = { workspace = true, optional = true }
trust-ping = { workspace = true, optional = true }
discover-features = { workspace = true, optional = true }
mediator-coordination = { workspace = true, optional = true }

mongodb.workspace = true
didcomm.workspace = true
tracing.workspace = true
once_cell.workspace = true
serde_json.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["full"] }
hyper = { workspace = true, features = ["full"] }
axum = { workspace = true, features = ["macros"] }
serde = { version = "1.0", features = ["derive"] }


[features]
default = [
"routing",
"pickup",
"trust-ping",
"discover-features",
"mediator-coordination",
]

routing = ["dep:forward"]
pickup = ["dep:pickup"]
trust-ping = ["dep:trust-ping"]
discover-features = ["dep:discover-features"]
mediator-coordination = ["dep:mediator-coordination"]


[dev-dependencies]
async-trait.workspace = true
mockall = "0.13.0"
uuid = { workspace = true, features = ["v4"] }
json-canon = "0.1.3"
Expand Down
27 changes: 27 additions & 0 deletions crates/web-plugins/didcomm-messaging/message-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "message-api"
version = "0.1.0"
edition = "2021"

[dependencies]
keystore.workspace = true
shared.workspace = true
database.workspace = true
filesystem.workspace = true

async-trait.workspace = true
mongodb.workspace = true
anyhow.workspace = true
tracing.workspace = true
serde_json.workspace = true
thiserror.workspace = true
didcomm = { workspace = true, features = ["uniffi"] }
hyper = { workspace = true, features = ["full"] }
axum = { workspace = true, features = ["macros"] }

[dev-dependencies]
keystore = { workspace = true, features = ["test-utils"] }
shared = { workspace = true, features = ["test-utils"] }
did-utils.workspace = true
uuid = { workspace = true, features = ["v4"] }
tokio = { version = "1.27.0", default-features = false, features = ["macros", "rt"] }
53 changes: 53 additions & 0 deletions crates/web-plugins/didcomm-messaging/message-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use async_trait::async_trait;
use axum::response::Response;
use didcomm::Message;
use shared::state::AppState;
use std::{collections::HashMap, sync::Arc};

#[async_trait]
pub trait MessageHandler: Send + Sync {
async fn handle(&self, state: Arc<AppState>, msg: Message)
-> Result<Option<Message>, Response>;
}

#[derive(Default, Clone)]
pub struct MessageRouter {
handlers: HashMap<String, Arc<dyn MessageHandler>>,
}

impl MessageRouter {
pub fn new() -> Self {
Self {
handlers: HashMap::new(),
}
}

pub fn register<F>(mut self, msg: &str, f: F) -> Self
where
F: MessageHandler + 'static,
{
self.handlers.insert(msg.to_string(), Arc::new(f));
self
}

pub fn merge(mut self, other: Self) -> Self {
self.handlers.extend(other.handlers);
self
}

pub fn get_handler(&self, msg: &str) -> Option<&Arc<dyn MessageHandler>> {
self.handlers.get(msg)
}

pub fn messages_types(&self) -> Vec<String> {
self.handlers.keys().cloned().collect()
}
}

pub trait MessagePlugin: Send + Sync {
/// Define a unique identifier
fn name(&self) -> &'static str;

/// Return a mapping of message types to handlers
fn didcomm_routes(&self) -> MessageRouter;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ edition = "2021"

[dependencies]
shared.workspace = true
keystore.workspace = true
message-api.workspace = true

axum.workspace = true
didcomm.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
uuid.workspace = true
thiserror.workspace = true
serde_json.workspace = true
async-trait.workspace = true
serde = { workspace = true, features = ["derive"] }
tokio = { workspace = true, features = ["full", "rt"] }
uuid.workspace = true
keystore.workspace = true

[dev-dependencies]
shared = { workspace = true, features = ["test-utils"] }
keystore = { workspace = true, features = ["test-utils"] }
did-utils.workspace = true
did-utils.workspace = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub(crate) const DISCOVER_FEATURE: &str = "https://didcomm.org/discover-features/2.0/disclose";
pub(crate) const QUERY_FEATURE: &str = "https://didcomm.org/discover-features/2.0/queries";
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
@@ -1,16 +1,17 @@
use crate::{
constants::DISCOVER_FEATURE,
errors::DiscoveryError,
model::{Disclosures, DisclosuresContent},
};
use didcomm::Message;
use serde_json::json;
use shared::{constants::DISCOVER_FEATURE, state::AppState};
use shared::state::AppState;
use std::{collections::HashSet, sync::Arc};
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 Expand Up @@ -117,21 +118,18 @@ fn build_response(disclosed_protocols: HashSet<String>) -> Message {
#[cfg(test)]
mod test {

use std::{sync::Arc, vec};

use crate::{constants::QUERY_FEATURE, model::Queries};
use did_utils::didcore::Document;
use didcomm::Message;
use keystore::tests::MockKeyStore;
use serde_json::json;
use shared::{
constants::QUERY_FEATURE,
repository::tests::{MockConnectionRepository, MockMessagesRepository},
state::{AppState, AppStateRepository},
};
use std::{sync::Arc, vec};
use uuid::Uuid;

use crate::model::Queries;

use super::handle_query_request;
const MEDIATION: &str = "https://didcomm.org/coordinate-mediation/2.0";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod constants;
mod errors;
mod handler;
mod model;

pub mod handler;
pub mod plugin;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::constants::QUERY_FEATURE;
use async_trait::async_trait;
use axum::response::{IntoResponse, Response};
use didcomm::Message;
use message_api::{MessageHandler, MessagePlugin, MessageRouter};
use shared::state::AppState;
use std::sync::Arc;

pub struct DiscoverFeaturesProtocol;

struct DiscoverFeaturesHandler;

#[async_trait]
impl MessageHandler for DiscoverFeaturesHandler {
async fn handle(
&self,
state: Arc<AppState>,
msg: Message,
) -> Result<Option<Message>, Response> {
crate::handler::handle_query_request(state, msg)
.await
.map_err(|e| e.into_response())
}
}

impl MessagePlugin for DiscoverFeaturesProtocol {
fn name(&self) -> &'static str {
"discover-features"
}

fn didcomm_routes(&self) -> MessageRouter {
MessageRouter::new().register(QUERY_FEATURE, DiscoverFeaturesHandler)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ edition = "2021"
keystore.workspace = true
shared.workspace = true
database.workspace = true
filesystem.workspace = true
message-api.workspace = true

mongodb.workspace = true
async-trait.workspace = true
serde_json.workspace = true
thiserror.workspace = true
didcomm = { workspace = true, features = ["uniffi"] }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) const MEDIATE_FORWARD_2_0: &str = "https://didcomm.org/routing/2.0/forward";
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,5 +1,5 @@
mod constants;
mod error;
pub mod handler;
mod handler;

// Re-exports
pub use error::ForwardError;
pub mod plugin;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::constants::MEDIATE_FORWARD_2_0;
use async_trait::async_trait;
use axum::response::{IntoResponse, Response};
use didcomm::Message;
use message_api::{MessageHandler, MessagePlugin, MessageRouter};
use shared::state::AppState;
use std::sync::Arc;

pub struct RoutingProtocol;

struct ForwardHandler;

#[async_trait]
impl MessageHandler for ForwardHandler {
async fn handle(
&self,
state: Arc<AppState>,
msg: Message,
) -> Result<Option<Message>, Response> {
crate::handler::mediator_forward_process(state, msg)
.await
.map_err(|e| e.into_response())
}
}

impl MessagePlugin for RoutingProtocol {
fn name(&self) -> &'static str {
"routing"
}

fn didcomm_routes(&self) -> MessageRouter {
MessageRouter::new().register(MEDIATE_FORWARD_2_0, ForwardHandler)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ shared.workspace = true
did-utils.workspace = true
database.workspace = true
keystore.workspace = true
filesystem.workspace = true
message-api.workspace = true

mongodb.workspace = true
multibase.workspace = true
serde.workspace = true
async-trait.workspace = true
serde_json.workspace = true
thiserror.workspace = true
tracing.workspace = true
Expand Down
Loading

0 comments on commit 8095d32

Please sign in to comment.