Skip to content

Commit

Permalink
refactor: clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
nanderstabel committed Aug 30, 2024
1 parent 29f25da commit e08a045
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 72 deletions.
24 changes: 15 additions & 9 deletions agent_api_rest/src/holder/holder/offers/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,28 @@ pub(crate) async fn accept(State(state): State<HolderState>, Path(offer_id): Pat
// Requests and Responses.
// Furthermore, the to be implemented Application Layer should be kept very thin as well. See: https://github.com/impierce/ssi-agent/issues/114

let command = OfferCommand::AcceptCredentialOffer {
offer_id: offer_id.clone(),
};
// Accept the Credential Offer if it exists
match query_handler(&offer_id, &state.query.offer).await {
Ok(Some(OfferView { .. })) => {
let command = OfferCommand::AcceptCredentialOffer {
offer_id: offer_id.clone(),
};

// Add the Credential Offer to the state.
if command_handler(&offer_id, &state.command.offer, command).await.is_err() {
// TODO: add better Error responses. This needs to be done properly in all endpoints once
// https://github.com/impierce/openid4vc/issues/78 is fixed.
return StatusCode::INTERNAL_SERVER_ERROR.into_response();
if command_handler(&offer_id, &state.command.offer, command).await.is_err() {
// TODO: add better Error responses. This needs to be done properly in all endpoints once
// https://github.com/impierce/openid4vc/issues/78 is fixed.
return StatusCode::INTERNAL_SERVER_ERROR.into_response();
}
}
Ok(None) => return StatusCode::NOT_FOUND.into_response(),
_ => return StatusCode::INTERNAL_SERVER_ERROR.into_response(),
}

let command = OfferCommand::SendCredentialRequest {
offer_id: offer_id.clone(),
};

// Add the Credential Offer to the state.
// Send the Credential Request
if command_handler(&offer_id, &state.command.offer, command).await.is_err() {
// TODO: add better Error responses. This needs to be done properly in all endpoints once
// https://github.com/impierce/openid4vc/issues/78 is fixed.
Expand Down
7 changes: 3 additions & 4 deletions agent_api_rest/src/holder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
pub mod holder;
pub mod openid4vci;

use agent_holder::state::HolderState;
use axum::routing::get;
use axum::{routing::post, Router};

use crate::holder::holder::{
credentials::credentials,
offers::{accept::accept, reject::reject, *},
};
use crate::API_VERSION;
use agent_holder::state::HolderState;
use axum::routing::get;
use axum::{routing::post, Router};

pub fn router(holder_state: HolderState) -> Router {
Router::new()
Expand Down
3 changes: 1 addition & 2 deletions agent_api_rest/src/issuance/credential_issuer/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ mod tests {
};
use agent_event_publisher_http::EventPublisherHttp;
use agent_issuance::{offer::event::OfferEvent, startup_commands::startup_commands, state::initialize};
use agent_secret_manager::service::Service;
use agent_shared::config::{set_config, Events};
use agent_store::{in_memory, EventPublisher};
use axum::{
Expand Down Expand Up @@ -276,8 +277,6 @@ mod tests {
#[case] is_self_signed: bool,
#[case] delay: u64,
) {
use agent_secret_manager::service::Service;

let (external_server, issuance_event_publishers) = if with_external_server {
let external_server = MockServer::start().await;

Expand Down
5 changes: 3 additions & 2 deletions agent_holder/src/credential/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

This aggregate is defined by:

- credential data
- a format (such as: _Open Badge 3.0_)
- credential_id
- offer_id
- credential
6 changes: 0 additions & 6 deletions agent_holder/src/credential/entity.rs

This file was deleted.

20 changes: 1 addition & 19 deletions agent_holder/src/credential/error.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum CredentialError {
#[error("Credential must be an object")]
InvalidCredentialError,

#[error("This Credential format it not supported")]
UnsupportedCredentialFormat,

#[error("The `credentialSubject` parameter is missing")]
MissingCredentialSubjectError,

#[error("The supplied `credentialSubject` is invalid: {0}")]
InvalidCredentialSubjectError(String),

#[error("The verifiable credential is invalid: {0}")]
InvalidVerifiableCredentialError(String),

#[error("Could not find any data to be signed")]
MissingCredentialDataError,
}
pub enum CredentialError {}
1 change: 0 additions & 1 deletion agent_holder/src/credential/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod aggregate;
pub mod command;
pub mod entity;
pub mod error;
pub mod event;
pub mod queries;
11 changes: 5 additions & 6 deletions agent_holder/src/offer/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Offer

This aggregate holds everything related to an offer of a credential to a subject:
This aggregate holds everything related to a credential offer:

- credential_ids
- form_url_encoded_credential_offer
- pre_authorized_code
- credential_offer
- status
- credential_configurations
- token_response
- access_token
- credential_response
- credentials
25 changes: 21 additions & 4 deletions agent_holder/src/offer/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl Aggregate for Offer {

async fn handle(&self, command: Self::Command, services: &Self::Services) -> Result<Vec<Self::Event>, Self::Error> {
use OfferCommand::*;
use OfferError::*;
use OfferEvent::*;

info!("Handling command: {:?}", command);
Expand Down Expand Up @@ -92,6 +93,11 @@ impl Aggregate for Offer {
}])
}
AcceptCredentialOffer { offer_id } => {
// TODO: should we 'do nothing' or log a `warn!` message instead of returning an error?
if self.status != Status::Pending {
return Err(CredentialOfferStatusNotPendingError);
}

let wallet = &services.wallet;

let credential_issuer_url = self.credential_offer.as_ref().unwrap().credential_issuer.clone();
Expand Down Expand Up @@ -135,6 +141,10 @@ impl Aggregate for Offer {
])
}
SendCredentialRequest { offer_id } => {
if self.status != Status::Accepted {
return Err(CredentialOfferStatusNotAcceptedError);
}

let wallet = &services.wallet;

let credential_issuer_url = self.credential_offer.as_ref().unwrap().credential_issuer.clone();
Expand Down Expand Up @@ -193,10 +203,17 @@ impl Aggregate for Offer {
credentials,
}])
}
RejectCredentialOffer { offer_id } => Ok(vec![CredentialOfferRejected {
offer_id,
status: Status::Rejected,
}]),
RejectCredentialOffer { offer_id } => {
// TODO: should we 'do nothing' or log a `warn!` message instead of returning an error?
if self.status != Status::Pending {
return Err(CredentialOfferStatusNotPendingError);
}

Ok(vec![CredentialOfferRejected {
offer_id,
status: Status::Rejected,
}])
}
}
}

Expand Down
12 changes: 4 additions & 8 deletions agent_holder/src/offer/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ use thiserror::Error;

#[derive(Error, Debug)]
pub enum OfferError {
#[error("Credential is missing")]
MissingCredentialError,
#[error("Missing `Proof` in Credential Request")]
MissingProofError,
#[error("Invalid `Proof` in Credential Request")]
InvalidProofError(String),
#[error("Missing `iss` claim in `Proof`")]
MissingProofIssuerError,
#[error("The Credential Offer has already been accepted and cannot be rejected anymore")]
CredentialOfferStatusNotPendingError,
#[error("The Credential Offer has not been accepted yet")]
CredentialOfferStatusNotAcceptedError,
}
6 changes: 2 additions & 4 deletions agent_holder/src/offer/event.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::collections::HashMap;

use super::aggregate::Status;
use cqrs_es::DomainEvent;
use oid4vci::{
credential_issuer::credential_configurations_supported::CredentialConfigurationsSupportedObject,
credential_offer::CredentialOfferParameters, token_response::TokenResponse,
};
use serde::{Deserialize, Serialize};

use super::aggregate::Status;
use std::collections::HashMap;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum OfferEvent {
Expand Down
12 changes: 5 additions & 7 deletions agent_issuance/src/credential/aggregate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use super::entity::Data;
use crate::credential::command::CredentialCommand;
use crate::credential::error::CredentialError::{self};
use crate::credential::event::CredentialEvent;
use crate::services::IssuanceServices;
use agent_shared::config::{config, get_preferred_did_method, get_preferred_signing_algorithm};
use async_trait::async_trait;
use cqrs_es::Aggregate;
Expand All @@ -23,13 +28,6 @@ use types_ob_v3::prelude::{
ProfileBuilder,
};

use crate::credential::command::CredentialCommand;
use crate::credential::error::CredentialError::{self};
use crate::credential::event::CredentialEvent;
use crate::services::IssuanceServices;

use super::entity::Data;

#[derive(Debug, Clone, Serialize, Deserialize, Default, Derivative)]
#[derivative(PartialEq)]
pub struct Credential {
Expand Down
1 change: 1 addition & 0 deletions agent_secret_manager/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

/// Conventience trait for Services like `IssuanceServices`, `HolderServices`, and `VerifierServices`.
pub trait Service {
fn new(subject: Arc<dyn oid4vc_core::Subject>) -> Self;

Expand Down

0 comments on commit e08a045

Please sign in to comment.