Skip to content

Commit

Permalink
Move session status structs to frontend crate.
Browse files Browse the repository at this point in the history
This resolves issues with compilation of the dependencies of the main
crate for frontend targets.
  • Loading branch information
cobward committed Aug 15, 2024
1 parent 6932636 commit d9b14ec
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 46 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async-trait = "0.1.73"
base64 = "0.21.4"
did-web = "0.2.2"
http = "1.1.0"
oid4vp-frontend = { path = "oid4vp-frontend" }
p256 = { version = "0.13.2", features = ["jwk"], optional = true }
reqwest = { version = "0.12.5", features = ["rustls-tls"], optional = true }
serde = "1.0.188"
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())
}
}
47 changes: 1 addition & 46 deletions src/verifier/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::{collections::BTreeMap, fmt::Debug, sync::Arc};

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

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

#[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),
}

#[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 },
}

/// Storage interface for session information.
#[async_trait]
pub trait SessionStore: Debug {
Expand Down Expand Up @@ -101,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())
}
}

0 comments on commit d9b14ec

Please sign in to comment.