Skip to content

Commit

Permalink
Add root_event_time to http config and verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreenbury committed Oct 2, 2023
1 parent 8f049c5 commit 211ba8f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 29 deletions.
4 changes: 4 additions & 0 deletions trustchain-http/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
str::FromStr,
};
use toml;
use trustchain_core::verifier::Timestamp;
use trustchain_core::TRUSTCHAIN_CONFIG;

const DEFAULT_HOST: &str = "127.0.0.1";
Expand All @@ -26,6 +27,8 @@ pub struct HTTPConfig {
pub https: bool,
/// Path containing certificate and key necessary for https
pub https_path: Option<String>,
/// Root event time for verifier.
pub root_event_time: Option<Timestamp>,
}

impl std::fmt::Display for HTTPConfig {
Expand All @@ -43,6 +46,7 @@ impl Default for HTTPConfig {
issuer_did: None,
https: false,
https_path: None,
root_event_time: None,
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions trustchain-http/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub enum TrustchainHTTPError {
RequestDoesNotExist,
#[error("Could not deserialize data: {0}")]
FailedToDeserialize(serde_json::Error),
#[error("Root event time not configured for verification.")]
RootEventTimeNotSet,
}

impl From<ResolverError> for TrustchainHTTPError {
Expand Down Expand Up @@ -118,6 +120,9 @@ impl IntoResponse for TrustchainHTTPError {
err @ TrustchainHTTPError::FailedToDeserialize(_) => {
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string())
}
err @ TrustchainHTTPError::RootEventTimeNotSet => {
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string())
}
};
let body = Json(json!({ "error": err_message }));
(status, body).into_response()
Expand Down
3 changes: 1 addition & 2 deletions trustchain-http/src/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ use async_trait::async_trait;
use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::{Html, IntoResponse};
use axum::{Form, Json};
use axum::Json;
use chrono::Utc;
use log::info;
use serde::{Deserialize, Serialize};
use ssi::did_resolve::DIDResolver;
use ssi::one_or_many::OneOrMany;
use ssi::vc::Credential;
use ssi::vc::VCDateTime;
use std::collections::HashMap;
use std::sync::Arc;
use trustchain_core::issuer::Issuer;
use trustchain_core::resolver::Resolver;
Expand Down
2 changes: 1 addition & 1 deletion trustchain-http/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::config::http_config;
use crate::middleware::validate_did;
use crate::{config::HTTPConfig, issuer, resolver, state::AppState, static_handlers, verifier};
use axum::routing::{post, IntoMakeService};
use axum::routing::IntoMakeService;
use axum::{middleware, routing::get, Router};
use axum_server::tls_rustls::RustlsConfig;
use hyper::server::conn::AddrIncoming;
Expand Down
62 changes: 36 additions & 26 deletions trustchain-http/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl TrustchainVerifierHTTP for TrustchainVerifierHTTPHandler {}
#[serde(rename_all = "camelCase")]
pub struct PostVerifier {
pub presentation_or_credential: PresentationOrCredential,
// TODO: remove field as obsolete
pub root_event_time: Timestamp,
}

Expand Down Expand Up @@ -106,7 +107,10 @@ impl TrustchainVerifierHTTPHandler {
PresentationOrCredential::Presentation(ref presentation) => {
TrustchainVerifierHTTPHandler::verify_presentation(
presentation,
verification_info.root_event_time,
app_state
.config
.root_event_time
.ok_or(TrustchainHTTPError::RootEventTimeNotSet)?,
&app_state.verifier,
)
.await
Expand All @@ -122,7 +126,10 @@ impl TrustchainVerifierHTTPHandler {
PresentationOrCredential::Credential(ref credential) => {
TrustchainVerifierHTTPHandler::verify_credential(
credential,
verification_info.root_event_time,
app_state
.config
.root_event_time
.ok_or(TrustchainHTTPError::RootEventTimeNotSet)?,
&app_state.verifier,
)
.await
Expand Down Expand Up @@ -179,6 +186,7 @@ mod tests {
/// Lazy static reference to core configuration loaded from `trustchain_config.toml`.
pub static ref TEST_HTTP_CONFIG: HTTPConfig = HTTPConfig {
issuer_did: Some("did:ion:test:EiAtHHKFJWAk5AsM3tgCut3OiBY4ekHTf66AAjoysXL65Q".to_string()),
root_event_time: Some(1666265405),
..Default::default()
};
}
Expand Down Expand Up @@ -351,18 +359,19 @@ mod tests {
assert_eq!(response.status(), StatusCode::OK);
assert_eq!("Credential received and verified!", response.text().await);

// Test post of credential to verifier with bad root event time
let app = TrustchainRouter::from(state.clone()).into_router();
let uid = "b9519df2-35c1-11ee-8314-7f66e4585b4f";
let uri = format!("/vc/verifier/{uid}");
let client = TestClient::new(app);
let mut post_verifier: PostVerifier =
serde_json::from_str(TEST_POST_VERIFIER_CREDENTIAL).unwrap();
post_verifier.root_event_time = 1666265406;
let response = client.post(&uri).json(&post_verifier).send().await;
assert_eq!(response.status(), StatusCode::OK);
// TODO: consider refining error returned
assert_eq!(response.text().await, r#"{"error":"Trustchain Verifier error: A commitment error during verification: Failed content verification. Expected data 1666265406 not found in candidate: 1666265405."}"#.to_string());
// TODO: remove as root event time now determined by verifier
// // Test post of credential to verifier with bad root event time
// let app = TrustchainRouter::from(state.clone()).into_router();
// let uid = "b9519df2-35c1-11ee-8314-7f66e4585b4f";
// let uri = format!("/vc/verifier/{uid}");
// let client = TestClient::new(app);
// let mut post_verifier: PostVerifier =
// serde_json::from_str(TEST_POST_VERIFIER_CREDENTIAL).unwrap();
// post_verifier.root_event_time = 1666265406;
// let response = client.post(&uri).json(&post_verifier).send().await;
// assert_eq!(response.status(), StatusCode::OK);
// // TODO: consider refining error returned
// assert_eq!(response.text().await, r#"{"error":"Trustchain Verifier error: A commitment error during verification: Failed content verification. Expected data 1666265406 not found in candidate: 1666265405."}"#.to_string());
}

#[tokio::test]
Expand All @@ -384,17 +393,18 @@ mod tests {
assert_eq!(response.status(), StatusCode::OK);
assert_eq!("Presentation received and verified!", response.text().await);

// Test post of presentation to verifier with bad root event time
let app = TrustchainRouter::from(state.clone()).into_router();
let uid = "b9519df2-35c1-11ee-8314-7f66e4585b4f";
let uri = format!("/vc/verifier/{uid}");
let client = TestClient::new(app);
let mut post_verifier: PostVerifier =
serde_json::from_str(TEST_POST_VERIFIER_PRESENTATION).unwrap();
post_verifier.root_event_time = 1666265406;
let response = client.post(&uri).json(&post_verifier).send().await;
assert_eq!(response.status(), StatusCode::OK);
// TODO: consider refining error returned
assert_eq!(response.text().await, r#"{"error":"Trustchain presentation error: A wrapped Credential error: A wrapped Verifier error: A commitment error during verification: Failed content verification. Expected data 1666265406 not found in candidate: 1666265405."}"#.to_string());
// TODO: remove as root event time now determined by verifier
// // Test post of presentation to verifier with bad root event time
// let app = TrustchainRouter::from(state.clone()).into_router();
// let uid = "b9519df2-35c1-11ee-8314-7f66e4585b4f";
// let uri = format!("/vc/verifier/{uid}");
// let client = TestClient::new(app);
// let mut post_verifier: PostVerifier =
// serde_json::from_str(TEST_POST_VERIFIER_PRESENTATION).unwrap();
// post_verifier.root_event_time = 1666265406;
// let response = client.post(&uri).json(&post_verifier).send().await;
// assert_eq!(response.status(), StatusCode::OK);
// // TODO: consider refining error returned
// assert_eq!(response.text().await, r#"{"error":"Trustchain presentation error: A wrapped Credential error: A wrapped Verifier error: A commitment error during verification: Failed content verification. Expected data 1666265406 not found in candidate: 1666265405."}"#.to_string());
}
}

0 comments on commit 211ba8f

Please sign in to comment.