Skip to content

Commit

Permalink
Merge pull request #301 from adorsys/300-remove-nested-body-in-mediat…
Browse files Browse the repository at this point in the history
…ion-coordination

feat(mediator-coordination): removed nested bodies
  • Loading branch information
Christiantyemele authored Jan 30, 2025
2 parents b86525d + eaf0941 commit 2d3fa41
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::{
errors::MediationError,
handler::midlw::ensure_jwm_type_is_mediation_request,
model::stateful::coord::{
Keylist, KeylistBody, KeylistEntry, KeylistUpdateAction, KeylistUpdateBody,
KeylistBody, KeylistEntry, KeylistUpdateAction, KeylistUpdateBody,
KeylistUpdateConfirmation, KeylistUpdateResponseBody, KeylistUpdateResult, MediationDeny,
MediationGrant, MediationGrantBody,
MediationGrantBody,
},
};
use did_utils::{
Expand Down Expand Up @@ -145,7 +145,7 @@ pub(crate) async fn process_mediate_request(
Ok(Some(
Message::build(
format!("urn:uuid:{}", Uuid::new_v4()),
mediation_grant.message_type.clone(),
MEDIATE_GRANT_2_0.to_string(),
json!(mediation_grant),
)
.to(sender_did.clone())
Expand All @@ -155,13 +155,9 @@ pub(crate) async fn process_mediate_request(
}
}

fn create_mediation_grant(routing_did: &str) -> MediationGrant {
MediationGrant {
id: format!("urn:uuid:{}", Uuid::new_v4()),
message_type: MEDIATE_GRANT_2_0.to_string(),
body: MediationGrantBody {
routing_did: routing_did.to_string(),
},
fn create_mediation_grant(routing_did: &str) -> MediationGrantBody {
MediationGrantBody {
routing_did: routing_did.to_string(),
}
}

Expand Down Expand Up @@ -351,8 +347,6 @@ pub(crate) async fn process_plain_keylist_query_message(
.unwrap()
.ok_or(MediationError::UncoordinatedSender)?;

println!("keylist: {:?}", connection);

let keylist_entries = connection
.keylist
.iter()
Expand All @@ -366,12 +360,7 @@ pub(crate) async fn process_plain_keylist_query_message(
pagination: None,
};

let keylist_object = Keylist {
id: format!("urn:uuid:{}", Uuid::new_v4()),
message_type: KEYLIST_2_0.to_string(),
body,
additional_properties: None,
};
let keylist_object = body;

let mediator_did = &state.diddoc.id;

Expand All @@ -384,8 +373,6 @@ pub(crate) async fn process_plain_keylist_query_message(
.from(mediator_did.clone())
.finalize();

println!("message: {:?}", message);

Ok(Some(message))
}

Expand Down Expand Up @@ -429,14 +416,14 @@ mod tests {
.finalize();

// Process request
let response = process_plain_keylist_query_message(Arc::clone(&state), message)
let message = process_plain_keylist_query_message(Arc::clone(&state), message)
.await
.unwrap()
.expect("Response should not be None");

assert_eq!(response.type_, KEYLIST_2_0);
assert_eq!(response.from.unwrap(), global::_mediator_did(&state));
assert_eq!(response.to.unwrap(), vec![global::_edge_did()]);
assert_eq!(message.type_, KEYLIST_2_0);
assert_eq!(message.from.unwrap(), global::_mediator_did(&state));
assert_eq!(message.to.unwrap(), vec![global::_edge_did()]);
}
#[tokio::test]
async fn test_keylist_query_malformed_request() {
Expand Down Expand Up @@ -499,7 +486,6 @@ mod tests {
assert_eq!(response.type_, KEYLIST_UPDATE_RESPONSE_2_0);
assert_eq!(response.from.unwrap(), global::_mediator_did(&state));
assert_eq!(response.to.unwrap(), vec![global::_edge_did()]);

// Assert updates

assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,26 +226,6 @@ pub struct KeylistQueryPaginate {
pub offset: i32,
}

/// Response to key list query, containing retrieved keys.
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct Keylist {
/// Uniquely identifies a keylist query response message.
pub id: String,

/// References the protocol URI of this concept.
///
/// Typically `https://didcomm.org/coordinate-mediation/2.0/keylist`
#[serde(rename = "type")]
pub message_type: String,

/// Message body
pub body: KeylistBody,

/// Dynamic properties.
#[serde(flatten)]
pub additional_properties: Option<HashMap<String, Value>>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq)]
pub struct KeylistBody {
/// List of retrieved keys.
Expand Down Expand Up @@ -579,25 +559,18 @@ mod tests {

#[test]
fn can_serialize_keylist_message() {
let keylist = Keylist {
id: "id_alice_keylist".to_string(),
message_type: KEYLIST_2_0.to_string(),
body: KeylistBody {
keys: vec![KeylistEntry {
recipient_did: String::from("did:key:alice_identity_pub1@alice_mediator"),
}],
pagination: Some(KeylistPagination {
count: 30,
offset: 30,
remaining: 100,
}),
},
additional_properties: None,
let keylist = KeylistBody {
keys: vec![KeylistEntry {
recipient_did: String::from("did:key:alice_identity_pub1@alice_mediator"),
}],
pagination: Some(KeylistPagination {
count: 30,
offset: 30,
remaining: 100,
}),
};

let keylist = json!({"body": keylist });
let expected = json!({
"id": "id_alice_keylist",
"type": "https://didcomm.org/coordinate-mediation/2.0/keylist",
"body": {
"keys": [
{
Expand All @@ -620,9 +593,15 @@ mod tests {

#[test]
fn can_deserialize_keylist_message() {
/// to structure to avoid multiple serde calls in test
///
#[derive(Deserialize, Serialize)]
pub struct Keylist {
/// Message body
pub body: KeylistBody,
}

let msg = r#"{
"id": "id_alice_keylist",
"type": "https://didcomm.org/coordinate-mediation/2.0/keylist",
"body": {
"keys": [
{
Expand All @@ -637,13 +616,8 @@ mod tests {
}
}"#;

// Assert deserialization

let keylist: Keylist = serde_json::from_str(msg).unwrap();

assert_eq!(&keylist.id, "id_alice_keylist");
assert_eq!(&keylist.message_type, KEYLIST_2_0);

assert_eq!(
keylist.body,
KeylistBody {
Expand Down

0 comments on commit 2d3fa41

Please sign in to comment.