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 3 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,2 @@
pub(super) const DISCOVER_FEATURE: &str = "https://didcomm.org/discover-features/2.0/disclose";

Check warning on line 1 in crates/web-plugins/didcomm-messaging/src/discover_feature/constant.rs

View workflow job for this annotation

GitHub Actions / Build and Test

constant `DISCOVER_FEATURE` is never used

Check warning on line 1 in crates/web-plugins/didcomm-messaging/src/discover_feature/constant.rs

View workflow job for this annotation

GitHub Actions / Build and Test

constant `DISCOVER_FEATURE` is never used
pub(super) const QUERY_FEATURE: &str = "https://didcomm.org/discover-features/2.0/queries";
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use axum::Json;
use serde_json::{json, Value};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum DiscoveryError {
#[error("message body is malformed")]
MalformedBody,
#[error("Repository not set")]
RepostitoryError,
#[error("No query field in body")]
QueryNotFound
}
impl DiscoveryError {
/// Converts the error to an axum JSON representation.
pub fn json(&self) -> Json<Value> {
Json(json!({
"error": self.to_string()
}))
}
}

impl From<DiscoveryError> for Json<Value> {
fn from(error: DiscoveryError) -> Self {
error.json()
}
}
150 changes: 150 additions & 0 deletions crates/web-plugins/didcomm-messaging/src/discover_feature/handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use axum::response::{IntoResponse, Response};
use didcomm::Message;
use serde_json::json;
use uuid::Uuid;

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

// handle discover feature request
// takes a message and a vector of supported messaging protocols as PIURI
// https://didcomm.org/discover-features/2.0/
pub(crate) fn handle_query_request(

Check warning on line 15 in crates/web-plugins/didcomm-messaging/src/discover_feature/handler.rs

View workflow job for this annotation

GitHub Actions / Build and Test

function `handle_query_request` is never used

Check warning on line 15 in crates/web-plugins/didcomm-messaging/src/discover_feature/handler.rs

View workflow job for this annotation

GitHub Actions / Build and Test

function `handle_query_request` is never used
message: Message,
supported_protocols: Vec<String>,
) -> Result<Message, Response> {
let mut disclosed_protocols: Vec<String> = Vec::new();

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

if body.is_none() {
return Err(DiscoveryError::QueryNotFound.json().into_response());
}

let queries = body.unwrap();

for value in queries.iter() {
match value.get("feature-type") {
Some(val) => {
let val = val.as_str().unwrap();
if val == "protocol" {
match value.get("match") {
Some(id) => {
if supported_protocols
.iter()
.find(|&id| id == &id.to_string())
.is_some()
{
disclosed_protocols.push(id.to_owned().to_string());
}
}
None => return Err(DiscoveryError::MalformedBody.json().into_response()),
}

// Only support features of type protocol
} else {
// do nothing
}
}
None => return Err(DiscoveryError::MalformedBody.json().into_response()),
};
}
// build response body
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();

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

use didcomm::Message;
use serde_json::json;
use uuid::Uuid;

use crate::discover_feature::{constant::QUERY_FEATURE, model::Queries};

use super::handle_query_request;
const TRUST: &str = "https://didcomm.org/trust-ping/2.0/ping";
#[tokio::test]
async fn test_get_supported_protocols() {
let queries = json!({"feature-type": "protocol", "match": TRUST});

let supported_protocols = vec![TRUST.to_string()];
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, supported_protocols) {
Ok(result) => {
assert!(result.body.get("disclosures").is_some());
assert!(result.body.get("disclosures").unwrap().is_array());
assert!(
result
.body
.get("disclosures")
.unwrap()
.as_array()
.unwrap()
.len()
== 1
);
let content = result.body.get("disclosures").unwrap().as_array().unwrap()[0]
.as_object()
.unwrap()
.get("id")
.unwrap()
.as_str()
.unwrap();
let content: String = serde_json::from_str(content).unwrap();
assert_eq!(content, TRUST.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 supported_protocols = vec![TRUST.to_string()];
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, supported_protocols) {
Ok(result) => {
assert!(result
.body
.get("disclosures")
.unwrap()
.as_array()
.unwrap()
.is_empty())
}
Err(e) => {
panic!("This should not occur: {:#?}", e)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub(super) mod model;
pub(crate) mod handler;
pub(crate) mod errors;
pub(super) mod constant;
25 changes: 25 additions & 0 deletions crates/web-plugins/didcomm-messaging/src/discover_feature/model.rs
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 {

Check warning on line 14 in crates/web-plugins/didcomm-messaging/src/discover_feature/model.rs

View workflow job for this annotation

GitHub Actions / Build and Test

associated function `new` is never used

Check warning on line 14 in crates/web-plugins/didcomm-messaging/src/discover_feature/model.rs

View workflow job for this annotation

GitHub Actions / Build and Test

associated function `new` is never used
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>>,
}
1 change: 1 addition & 0 deletions crates/web-plugins/didcomm-messaging/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod client;
pub mod web;
pub mod plugin;
pub(crate) mod did_rotation;
pub(crate) mod discover_feature;

mod model;
mod forward;
Expand Down
Loading