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 all 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,28 @@
use axum::{http::StatusCode, response::IntoResponse, Json};
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 IntoResponse for DiscoveryError {
fn into_response(self) -> axum::response::Response {
let status_code = match self {
DiscoveryError::MalformedBody => StatusCode::BAD_REQUEST,
DiscoveryError::QueryNotFound => StatusCode::EXPECTATION_FAILED,
DiscoveryError::FeatureNOTSupported => StatusCode::NOT_ACCEPTABLE,
};

let body = Json(serde_json::json!({
"error": self.to_string(),
}));

(status_code, body).into_response()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
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 supported: &Vec<String>;

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

if let Some(protocols) = &state.supported_protocols {
supported = protocols;
if let Some(queries) = queries {
for value in queries {
match value.get("feature-type") {
Some(val) => {
let val = val.as_str().unwrap_or_default();
if val == "protocol" {
match value.get("match") {
Some(id) => {
let id = id.as_str().unwrap_or_default();

if !id
.ends_with(".*")
.then(|| {
supported
.into_iter()
.find(|protocol| protocol.contains(&id.to_string()))
.is_some()
})
.is_some()
{
disclosed_protocols.insert(id.to_owned());
}
// wildcard scenario
else {
let parts: Vec<&str> = id.split(".*").collect();
// stores the full protocol obtained when we have a match with wildcard
let mut container: String = Default::default();

if let Some(id) = parts.get(0) {
supported
.into_iter()
.find(|protocol| protocol.contains(&id.to_string()))
.is_some_and(|protocol| {
container = protocol.to_string();
return true;
})
.then(|| {
let parts: Vec<&str> =
container.split(id).collect();
// getting the minor version supported by the mediator when we have a request with a wildcard as minor
let minor = parts[1]
.to_string()
.chars()
.nth(1)
.unwrap_or_default();
let id = format!("{id}.{minor}");
disclosed_protocols.insert(id.to_string());
});
}
}
}

None => return Err(DiscoveryError::MalformedBody.into_response()),
}
} else {
return Err(DiscoveryError::FeatureNOTSupported.into_response());
}
}
None => return Err(DiscoveryError::MalformedBody.into_response()),
}
}

// build response body
let msg = build_response(disclosed_protocols);
Ok(Some(msg))
} else {
return Err(DiscoveryError::QueryNotFound.into_response());
}
} else {
let msg = build_response(disclosed_protocols);
Ok(Some(msg))
}
}
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) => {
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.*"});

// test duplicates in queries
let body = Queries {
queries: vec![queries.clone(), 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
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