Skip to content

Commit

Permalink
Usability improvements (#12)
Browse files Browse the repository at this point in the history
A couple of usability improvements made when integrating into a real web application:

* Derive serde traits on verifier::session::Status.
* Move verifier::session::Status to a separate frontend crate, to resolve frontend/wasm-target compilation issues with other dependencies in the main crate.
  • Loading branch information
cobward authored Aug 16, 2024
1 parent a3a9209 commit 2842f2b
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 50 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ did-web = "0.2.2"
http = "1.1.0"
jsonpath_lib = "0.3.0"
jsonschema = "0.18.0"
oid4vp-frontend = { version = "0.1.0", path = "oid4vp-frontend" }
p256 = { version = "0.13.2", features = ["jwk"], optional = true }
regex = "1.10.6"
reqwest = { version = "0.12.5", features = ["rustls-tls"], optional = true }
Expand Down
96 changes: 96 additions & 0 deletions oid4vp-frontend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions oid4vp-frontend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "oid4vp-frontend"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = "1.0"
serde_json = "1.0"
50 changes: 50 additions & 0 deletions oid4vp-frontend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! OID4VP library data structures that are needed on the frontend, without all of the other
//! dependencies that can cause compilation issues with web targets.
use serde::{Deserialize, Serialize};
use serde_json::Value as Json;

/// Status of an OID4VP session.
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
pub enum Status {
/// Wallet has been sent the request by reference, waiting for the wallet to request the request.
SentRequestByReference,
/// Wallet has received the request, waiting on the wallet to process the request.
SentRequest,
/// Verifier has received the response and is now processing it.
ReceivedResponse,
/// Verifier has finished processing the response.
Complete(Outcome),
}

/// Outcome of an OID4VP session.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Outcome {
/// An error occurred during response processing.
Error { cause: String },
/// The authorization response did not pass verification.
Failure { reason: String },
/// The authorization response is verified.
Success { info: Json },
}

impl PartialEq for Outcome {
fn eq(&self, other: &Self) -> bool {
core::mem::discriminant(self) == core::mem::discriminant(other)
}
}

impl Outcome {
fn ordering(&self) -> u8 {
match self {
Outcome::Error { .. } => 0,
Outcome::Failure { .. } => 1,
Outcome::Success { .. } => 2,
}
}
}

impl PartialOrd for Outcome {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.ordering().partial_cmp(&other.ordering())
}
}
48 changes: 2 additions & 46 deletions src/verifier/session.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{collections::BTreeMap, fmt::Debug, sync::Arc};

use anyhow::{bail, Error, Ok, Result};
use anyhow::{bail, Ok, Result};
use async_trait::async_trait;
use serde_json::Value as Json;
pub use oid4vp_frontend::*;
use tokio::sync::Mutex;
use uuid::Uuid;

Expand All @@ -20,28 +20,6 @@ pub struct Session {
pub presentation_definition: PresentationDefinition,
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Status {
/// Wallet has been sent the request by reference, waiting for the wallet to request the request.
SentRequestByReference,
/// Wallet has received the request, waiting on the wallet to process the request.
SentRequest,
/// Verifier has received the response and is now processing it.
ReceivedResponse,
/// Verifier has finished processing the response.
Complete(Outcome),
}

#[derive(Debug, Clone)]
pub enum Outcome {
/// An error occurred during response processing.
Error { cause: Arc<Error> },
/// The authorization response did not pass verification.
Failure { reason: String },
/// The authorization response is verified.
Success { info: Json },
}

/// Storage interface for session information.
#[async_trait]
pub trait SessionStore: Debug {
Expand Down Expand Up @@ -100,25 +78,3 @@ impl SessionStore for MemoryStore {
bail!("session not found")
}
}

impl PartialEq for Outcome {
fn eq(&self, other: &Self) -> bool {
core::mem::discriminant(self) == core::mem::discriminant(other)
}
}

impl Outcome {
fn ordering(&self) -> u8 {
match self {
Outcome::Error { .. } => 0,
Outcome::Failure { .. } => 1,
Outcome::Success { .. } => 2,
}
}
}

impl PartialOrd for Outcome {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.ordering().partial_cmp(&other.ordering())
}
}
5 changes: 1 addition & 4 deletions tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,5 @@ async fn w3c_vc_did_client_direct_post() {
assert_eq!(None, redirect);

let status = verifier.poll_status(id).await.unwrap();
match status {
Status::Complete(Outcome::Success { .. }) => (),
_ => panic!("unexpected status: {status:?}"),
}
assert!(matches!(status, Status::Complete(Outcome::Success { .. })))
}

0 comments on commit 2842f2b

Please sign in to comment.