Skip to content
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

231 discover feature protocol #236

Merged
merged 15 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "discover-features"
version = "0.1.0"
edition = "2021"

[dependencies]
shared.workspace = true
axum.workspace = true
didcomm.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
thiserror.workspace = true
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use axum::Json;
use serde_json::{json, Value};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum DiscoveryError {
#[error("message body is malformed")]
MalformedBody,
#[error("No queries field in body")]
QueryNotFound,
#[error("query feature-type not supported try using `protocol`")]
FeatureNOTSupported
}
impl DiscoveryError {
/// Converts the error to an axum JSON representation.
pub fn json(&self) -> Json<Value> {
Json(json!({
"error": self.to_string()
}))
}
}
Hermann-Core marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
use std::{collections::HashSet, sync::Arc};

use axum::response::{IntoResponse, Response};
use didcomm::Message;
use serde_json::json;
use shared::{constants::DISCOVER_FEATURE, state::AppState};
use uuid::Uuid;

use super::{
errors::DiscoveryError,
model::{Disclosures, DisclosuresContent},
};

// handle discover feature request
// https://didcomm.org/discover-features/2.0/
pub fn handle_query_request(
message: Message,
state: Arc<AppState>,
) -> Result<Option<Message>, Response> {
let mut disclosed_protocols: HashSet<String> = HashSet::new();

let queries = message.body.get("queries").and_then(|val| val.as_array());

if let Some(_) = &state.supported_protocols {
if let Some(queries) = queries {
for value in queries {
match value.get("feature-type") {
Some(val) => {
let val = val.as_str().unwrap();
Christiantyemele marked this conversation as resolved.
Show resolved Hide resolved
if val.to_string() == "protocol" {
Christiantyemele marked this conversation as resolved.
Show resolved Hide resolved
match value.get("match") {
Some(id) => {
let id = id.as_str().unwrap_or_default();

if !id.ends_with(".*") {
if state.as_ref().clone().supported_protocols.is_some_and(
|a| {
a.into_iter()
.find(|protocol| {
protocol.contains(&id.to_string())
})
.is_some()
},
) {
disclosed_protocols.insert(id.to_owned());
}
}
// wildcard scenario
if id.ends_with(".*") {
Christiantyemele marked this conversation as resolved.
Show resolved Hide resolved
let parts: Vec<&str> = id.split(".*").collect();
// container
let mut container: String = Default::default();

if let Some(id) = parts.get(0) {
if state
.as_ref()
.clone()
.supported_protocols
.is_some_and(|a| {
a.into_iter()
.find(|protocol| {
protocol.contains(&id.to_string())
})
.is_some_and(|protocol| {
container = protocol;
return true;
})
})
{
let parts: Vec<&str> =
container.split(id).collect();
let minor = parts[1]
.to_string()
.chars()
.nth(1)
.unwrap_or_default();
let id = format!("{id}.{minor}");
disclosed_protocols.insert(id.to_string());
Christiantyemele marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
None => {
return Err(DiscoveryError::MalformedBody
.json()
.into_response())
}
}
} else {
return Err(DiscoveryError::FeatureNOTSupported.json().into_response());
}
Christiantyemele marked this conversation as resolved.
Show resolved Hide resolved
}
None => return Err(DiscoveryError::MalformedBody.json().into_response()),
}
}

// build response body
let msg = build_response(disclosed_protocols);
Ok(Some(msg))
} else {
return Err(DiscoveryError::QueryNotFound.json().into_response());
}
} else {
let msg = build_response(disclosed_protocols);
Ok(Some(msg))
}
Christiantyemele marked this conversation as resolved.
Show resolved Hide resolved
}
fn build_response(disclosed_protocols: HashSet<String>) -> Message {
let mut body = Disclosures::new();
for id in disclosed_protocols.iter() {
let content = DisclosuresContent {
feature_type: "protocol".to_string(),
id: id.to_owned(),
roles: None,
};
let content = json!(content);

body.disclosures.push(content);
}

let id = Uuid::new_v4().urn().to_string();
let msg = Message::build(id, DISCOVER_FEATURE.to_string(), json!(body)).finalize();

msg
}
#[cfg(test)]
mod test {

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

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 uuid::Uuid;

use crate::model::Queries;

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

pub fn setup() -> Arc<AppState> {
let public_domain = String::from("http://alice-mediator.com");

let diddoc = Document::default();

let repository = AppStateRepository {
connection_repository: Arc::new(MockConnectionRepository::from(vec![])),
message_repository: Arc::new(MockMessagesRepository::from(vec![])),
keystore: Arc::new(MockKeyStore::new(vec![])),
};

let state = Arc::new(AppState::from(
public_domain,
diddoc,
Some(vec![
"https://didcomm.org/coordinate-mediation/2.0/mediate-request".to_string(),
]),
Some(repository),
Christiantyemele marked this conversation as resolved.
Show resolved Hide resolved
));

state
}

#[tokio::test]
async fn test_get_supported_protocols() {
let queries = json!({"feature-type": "protocol", "match": MEDIATION});

let body = Queries {
queries: vec![queries],
};
let id = Uuid::new_v4().urn().to_string();

let message = Message::build(id, QUERY_FEATURE.to_string(), json!(body)).finalize();
let state = setup();
match handle_query_request(message, state) {
Ok(result) => {
println!("{:#?}", &result.clone().unwrap());
assert!(result.clone().unwrap().body.get("disclosures").is_some());
assert!(result
.clone()
.unwrap()
.body
.get("disclosures")
.unwrap()
.is_array());
assert_eq!(
result
.clone()
.unwrap()
.body
.get("disclosures")
.unwrap()
.as_array()
.unwrap()
.len(),
1
);
let id = serde_json::from_str::<String>(
&result
.unwrap()
.body
.get("disclosures")
.unwrap()
.as_array()
.unwrap()[0]
.as_object()
.unwrap()
.get("id")
.unwrap()
.to_string(),
)
.unwrap();

assert_eq!(id, MEDIATION.to_string());
}
Err(e) => {
panic!("This should not occur {:?}", e)
}
}
}
#[tokio::test]
async fn test_get_supported_protocols_with_wildcard() {
let queries = json!({"feature-type": "protocol", "match": "https://didcomm.org/coordinate-mediation/2.*"});

let body = Queries {
queries: vec![queries],
};
let id = Uuid::new_v4().urn().to_string();

let message = Message::build(id, QUERY_FEATURE.to_string(), json!(body)).finalize();
let state = setup();
match handle_query_request(message, state) {
Ok(result) => {
println!("{:#?}", &result.clone().unwrap());
assert!(result.clone().unwrap().body.get("disclosures").is_some());
assert!(result
.clone()
.unwrap()
.body
.get("disclosures")
.unwrap()
.is_array());
assert!(
result
.clone()
.unwrap()
.body
.get("disclosures")
.unwrap()
.as_array()
.unwrap()
.len()
== 1
);
let id = serde_json::from_str::<String>(
&result
.unwrap()
.body
.get("disclosures")
.unwrap()
.as_array()
.unwrap()[0]
.as_object()
.unwrap()
.get("id")
.unwrap()
.to_string(),
)
.unwrap();

assert_eq!(id, MEDIATION.to_string());
}
Err(e) => {
panic!("This should not occur {:?}", e)
}
}
}
#[tokio::test]
async fn test_unsupported_feature_type() {
let queries = json!({"feature-type": "goal-code", "match": "org.didcomm"});
let state = setup();
let body = Queries {
queries: vec![queries],
};
let id = Uuid::new_v4().urn().to_string();

let message = Message::build(id, QUERY_FEATURE.to_string(), json!(body)).finalize();

match handle_query_request(message, state) {
Ok(_) => {
panic!("This should'nt occur");
}
Err(_) => {}
}
}
}
Christiantyemele marked this conversation as resolved.
Show resolved Hide resolved
Christiantyemele marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod errors;
pub mod handler;
mod model;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Deserialize, Serialize)]
pub struct Queries {
pub queries: Vec<Value>,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Disclosures {
pub disclosures: Vec<Value>,
}
impl Disclosures {
pub fn new() -> Self {
Disclosures { disclosures: vec![] }
}
}
#[derive(Deserialize, Serialize)]
pub struct DisclosuresContent {
#[serde(rename = "feature-type")]
pub feature_type: String,
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub roles: Option<Vec<String>>,
}
2 changes: 2 additions & 0 deletions crates/web-plugins/didcomm-messaging/shared/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ pub const TRUST_PING_RESPONSE_2_0: &str = "https://didcomm.org/trust-ping/2.0/pi

pub const DIDCOMM_ENCRYPTED_MIME_TYPE: &str = "application/didcomm-encrypted+json";
pub const DIDCOMM_ENCRYPTED_SHORT_MIME_TYPE: &str = "didcomm-encrypted+json";
pub const DISCOVER_FEATURE: &str = "https://didcomm.org/discover-features/2.0/disclose";
pub const QUERY_FEATURE: &str = "https://didcomm.org/discover-features/2.0/queries";
Loading
Loading