Skip to content

Commit

Permalink
Use new enum PresentationVerificationStatus instead of RevocationStatus
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Mar 14, 2023
1 parent 27fdb0d commit e849297
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 30 deletions.
5 changes: 2 additions & 3 deletions aries_vcx/src/handlers/proof_presentation/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use crate::core::profile::profile::Profile;
use crate::errors::error::prelude::*;
use crate::handlers::connection::mediated_connection::MediatedConnection;
use crate::protocols::proof_presentation::verifier::messages::VerifierMessages;
use crate::protocols::proof_presentation::verifier::state_machine::{
PresentationVerificationStatus, VerifierSM, VerifierState,
};
use crate::protocols::proof_presentation::verifier::state_machine::{VerifierSM, VerifierState};
use crate::protocols::proof_presentation::verifier::verification_status::PresentationVerificationStatus;
use crate::protocols::SendClosure;
use messages::a2a::A2AMessage;
use messages::protocols::proof_presentation::presentation_proposal::PresentationProposal;
Expand Down
1 change: 1 addition & 0 deletions aries_vcx/src/protocols/proof_presentation/verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::protocols::proof_presentation::verifier::messages::VerifierMessages;
pub mod messages;
pub mod state_machine;
pub mod states;
pub mod verification_status;

pub fn verify_thread_id(thread_id: &str, message: &VerifierMessages) -> VcxResult<()> {
if !settings::indy_mocks_enabled() && !message.thread_id_matches(thread_id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::protocols::proof_presentation::verifier::states::initial::InitialVeri
use crate::protocols::proof_presentation::verifier::states::presentation_proposal_received::PresentationProposalReceivedState;
use crate::protocols::proof_presentation::verifier::states::presentation_request_sent::PresentationRequestSentState;
use crate::protocols::proof_presentation::verifier::states::presentation_request_set::PresentationRequestSetState;
use crate::protocols::proof_presentation::verifier::verification_status::PresentationVerificationStatus;
use crate::protocols::proof_presentation::verifier::verify_thread_id;
use crate::protocols::SendClosure;
use messages::a2a::{A2AMessage, MessageId};
Expand All @@ -21,6 +22,7 @@ use messages::protocols::proof_presentation::presentation_ack::PresentationAck;
use messages::protocols::proof_presentation::presentation_proposal::PresentationProposal;
use messages::protocols::proof_presentation::presentation_request::PresentationRequest;
use messages::status::Status;
use strum_macros::{AsRefStr, EnumString};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct VerifierSM {
Expand Down Expand Up @@ -66,13 +68,6 @@ impl Default for VerifierFullState {
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum PresentationVerificationStatus {
Valid,
Invalid,
Unavailable,
}

fn build_verification_ack(thread_id: &str) -> PresentationAck {
PresentationAck::create().set_thread_id(thread_id).set_out_time()
}
Expand Down Expand Up @@ -422,11 +417,8 @@ impl VerifierSM {

pub fn get_presentation_verification_status(&self) -> PresentationVerificationStatus {
match self.state {
VerifierFullState::Finished(ref state) => match &state.revocation_status {
None => PresentationVerificationStatus::Unavailable,
Some(status) => status.clone(),
},
_ => PresentationVerificationStatus::Unavailable,
VerifierFullState::Finished(ref state) => state.revocation_status.clone(),
_ => PresentationVerificationStatus::Unavailable(),
}
}

Expand Down Expand Up @@ -479,6 +471,8 @@ impl VerifierSM {
#[cfg(test)]
#[cfg(feature = "general_test")]
pub mod unit_tests {
use std::str::FromStr;

use crate::common::proofs::proof_request::test_utils::_presentation_request_data;
use crate::common::test_utils::mock_profile;
use crate::test::source_id;
Expand Down Expand Up @@ -775,7 +769,7 @@ pub mod unit_tests {

assert_match!(VerifierState::Failed, verifier_sm.get_state());
assert_match!(
PresentationVerificationStatus::Unavailable,
PresentationVerificationStatus::Unavailable(),
verifier_sm.get_presentation_verification_status()
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
use crate::protocols::proof_presentation::verifier::state_machine::PresentationVerificationStatus;
use serde::{Deserialize, Deserializer};

use messages::concepts::problem_report::ProblemReport;
use messages::protocols::proof_presentation::presentation::Presentation;
use messages::protocols::proof_presentation::presentation_request::PresentationRequest;
use messages::status::Status;

use crate::protocols::proof_presentation::verifier::verification_status::PresentationVerificationStatus;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FinishedState {
pub presentation_request: Option<PresentationRequest>,
pub presentation: Option<Presentation>,
pub status: Status,
pub revocation_status: Option<PresentationVerificationStatus>,
#[serde(
default = "PresentationVerificationStatus::Unavailable",
deserialize_with = "null_to_unavailable"
)]
pub revocation_status: PresentationVerificationStatus,
}

// For backwards compatibility, if "revocation_status": null, we deserialize as Unavailable
fn null_to_unavailable<'de, D>(deserializer: D) -> Result<PresentationVerificationStatus, D::Error>
where
D: Deserializer<'de>,
{
let opt = Option::deserialize(deserializer)?;
Ok(opt.unwrap_or_else(PresentationVerificationStatus::Unavailable))
}

impl FinishedState {
Expand All @@ -19,7 +35,87 @@ impl FinishedState {
presentation_request: None,
presentation: None,
status: Status::Declined(problem_report),
revocation_status: None,
revocation_status: PresentationVerificationStatus::Unavailable(),
}
}
}

#[cfg(test)]
#[cfg(feature = "general_test")]
pub mod unit_tests {
use std::str::FromStr;

use messages::protocols::proof_presentation::presentation::test_utils::{_presentation, _presentation_1};
use messages::protocols::proof_presentation::presentation_proposal::test_utils::_presentation_proposal;
use messages::protocols::proof_presentation::presentation_request::test_utils::_presentation_request;
use messages::protocols::proof_presentation::test_utils::{_ack, _problem_report};

use crate::common::proofs::proof_request::test_utils::_presentation_request_data;
use crate::common::test_utils::mock_profile;
use crate::test::source_id;
use crate::utils::devsetup::{SetupEmpty, SetupMocks};

use super::*;

#[test]
fn test_verifier_state_finished_ser() {
let state = FinishedState {
presentation_request: None,
presentation: None,
status: Status::Success,
revocation_status: PresentationVerificationStatus::Valid,
};
let serialized = serde_json::to_string(&state).unwrap();
let expected =
r#"{"presentation_request":null,"presentation":null,"status":"Success","revocation_status":"Valid"}"#;
assert_eq!(serialized, expected);
}

#[test]
fn test_verifier_state_finished_deser() {
{
let serialized =
r#"{"presentation":null,"presentation_request":null,"status":"Success","revocation_status":"Invalid"}"#;
let deserialized: FinishedState = serde_json::from_str(serialized).unwrap();
assert_eq!(deserialized.revocation_status, PresentationVerificationStatus::Invalid)
}
{
let serialized =
r#"{"presentation":null,"presentation_request":null,"status":"Success","revocation_status":"Valid"}"#;
let deserialized: FinishedState = serde_json::from_str(serialized).unwrap();
assert_eq!(deserialized.revocation_status, PresentationVerificationStatus::Valid)
}
}

#[test]
fn test_verifier_state_finished_deser_legacy() {
{
let serialized = r#"{"presentation":null,"presentation_request":null,"status":"Success"}"#;
let deserialized: FinishedState = serde_json::from_str(serialized).unwrap();
assert_eq!(
deserialized.revocation_status,
PresentationVerificationStatus::Unavailable()
)
}
{
let serialized =
r#"{"presentation":null,"presentation_request":null,"status":"Success","revocation_status":null}"#;
let deserialized: FinishedState = serde_json::from_str(serialized).unwrap();
assert_eq!(
deserialized.revocation_status,
PresentationVerificationStatus::Unavailable()
)
}
{
let serialized =
r#"{"presentation":null,"presentation_request":null,"status":"Success","revocation_status":"Revoked"}"#;
let deserialized: FinishedState = serde_json::from_str(serialized).unwrap();
assert_eq!(deserialized.revocation_status, PresentationVerificationStatus::Invalid)
}
{
let serialized = r#"{"presentation":null,"presentation_request":null,"status":"Success","revocation_status":"NonRevoked"}"#;
let deserialized: FinishedState = serde_json::from_str(serialized).unwrap();
assert_eq!(deserialized.revocation_status, PresentationVerificationStatus::Valid)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::common::proofs::verifier::verifier::validate_indy_proof;
use crate::core::profile::profile::Profile;
use crate::errors::error::{AriesVcxError, AriesVcxErrorKind, VcxResult};
use crate::global::settings;
use crate::protocols::proof_presentation::verifier::state_machine::PresentationVerificationStatus;
use crate::protocols::proof_presentation::verifier::states::finished::FinishedState;
use crate::protocols::proof_presentation::verifier::verification_status::PresentationVerificationStatus;
use messages::concepts::problem_report::ProblemReport;
use messages::protocols::proof_presentation::presentation::Presentation;
use messages::protocols::proof_presentation::presentation_request::PresentationRequest;
Expand Down Expand Up @@ -59,7 +59,7 @@ impl
)> for FinishedState
{
fn from(
(state, presentation, was_revoked): (
(state, presentation, verification_status): (
PresentationRequestSentState,
Presentation,
PresentationVerificationStatus,
Expand All @@ -70,7 +70,7 @@ impl
presentation_request: Some(state.presentation_request),
presentation: Some(presentation),
status: Status::Success,
revocation_status: Some(was_revoked),
revocation_status: verification_status,
}
}
}
Expand All @@ -82,7 +82,7 @@ impl From<(PresentationRequestSentState, ProblemReport)> for FinishedState {
presentation_request: Some(state.presentation_request),
presentation: None,
status: Status::Failed(problem_report),
revocation_status: None,
revocation_status: PresentationVerificationStatus::Unavailable(),
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use serde::{Deserialize, Deserializer};

#[derive(Serialize, Debug, Clone, PartialEq, Eq)]
pub enum PresentationVerificationStatus {
Valid,
Invalid,
Unavailable(),
}

impl<'de> Deserialize<'de> for PresentationVerificationStatus {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
match s.as_str() {
"Valid" | "NonRevoked" => Ok(PresentationVerificationStatus::Valid),
"Invalid" | "Revoked" => Ok(PresentationVerificationStatus::Invalid),
"Unavailable" => Ok(PresentationVerificationStatus::Unavailable()),
_ => Err(serde::de::Error::custom(format!("unexpected value for Color: {}", s))),
}
}
}

#[cfg(test)]
#[cfg(feature = "general_test")]
pub mod unit_tests {
use std::str::FromStr;

use crate::common::proofs::proof_request::test_utils::_presentation_request_data;
use crate::common::test_utils::mock_profile;
use crate::test::source_id;
use crate::utils::devsetup::{SetupEmpty, SetupMocks};
use messages::protocols::proof_presentation::presentation::test_utils::{_presentation, _presentation_1};
use messages::protocols::proof_presentation::presentation_proposal::test_utils::_presentation_proposal;
use messages::protocols::proof_presentation::presentation_request::test_utils::_presentation_request;
use messages::protocols::proof_presentation::test_utils::{_ack, _problem_report};

use super::*;

#[test]
fn test_presentation_status_ser_deser() {
assert_eq!(
PresentationVerificationStatus::Valid,
serde_json::from_str("\"Valid\"").unwrap()
);
assert_eq!(
PresentationVerificationStatus::Valid,
serde_json::from_str("\"NonRevoked\"").unwrap()
);
assert_eq!(
PresentationVerificationStatus::Invalid,
serde_json::from_str("\"Invalid\"").unwrap()
);
assert_eq!(
PresentationVerificationStatus::Invalid,
serde_json::from_str("\"Revoked\"").unwrap()
);
assert_eq!(
PresentationVerificationStatus::Unavailable(),
serde_json::from_str("\"Unavailable\"").unwrap()
);
}
}
5 changes: 2 additions & 3 deletions aries_vcx/tests/utils/devsetup_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ pub mod test_utils {
use aries_vcx::protocols::mediated_connection::invitee::state_machine::InviteeState;
use aries_vcx::protocols::mediated_connection::inviter::state_machine::InviterState;
use aries_vcx::protocols::proof_presentation::prover::state_machine::ProverState;
use aries_vcx::protocols::proof_presentation::verifier::state_machine::{
PresentationVerificationStatus, VerifierState,
};
use aries_vcx::protocols::proof_presentation::verifier::state_machine::VerifierState;
use aries_vcx::protocols::proof_presentation::verifier::verification_status::PresentationVerificationStatus;
use aries_vcx::utils::devsetup::*;
use aries_vcx::utils::provision::provision_cloud_agent;
use vdrtools::{PoolHandle, WalletHandle};
Expand Down
5 changes: 2 additions & 3 deletions aries_vcx/tests/utils/scenarios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ pub mod test_utils {
use aries_vcx::protocols::mediated_connection::invitee::state_machine::InviteeState;
use aries_vcx::protocols::mediated_connection::inviter::state_machine::InviterState;
use aries_vcx::protocols::proof_presentation::prover::state_machine::ProverState;
use aries_vcx::protocols::proof_presentation::verifier::state_machine::{
PresentationVerificationStatus, VerifierState,
};
use aries_vcx::protocols::proof_presentation::verifier::state_machine::VerifierState;
use aries_vcx::protocols::proof_presentation::verifier::verification_status::PresentationVerificationStatus;
use aries_vcx::utils::constants::{DEFAULT_PROOF_NAME, TAILS_DIR, TEST_TAILS_URL};
use aries_vcx::utils::filters::{filter_credential_offers_by_comment, filter_proof_requests_by_name};
use aries_vcx::utils::get_temp_dir_path;
Expand Down
2 changes: 1 addition & 1 deletion libvcx_core/src/api_vcx/api_handle/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl From<PresentationVerificationStatus> for VcxPresentationVerificationStatus
match verification_status {
PresentationVerificationStatus::Valid => VcxPresentationVerificationStatus::Valid,
PresentationVerificationStatus::Invalid => VcxPresentationVerificationStatus::Invalid,
PresentationVerificationStatus::Unavailable => VcxPresentationVerificationStatus::Unavailable
PresentationVerificationStatus::Unavailable() => VcxPresentationVerificationStatus::Unavailable
}
}
}
Expand Down

0 comments on commit e849297

Please sign in to comment.