Skip to content

Commit

Permalink
wip: fixture test with json
Browse files Browse the repository at this point in the history
  • Loading branch information
heeckhau committed Oct 24, 2024
1 parent ef43cba commit 1a385f2
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 28 deletions.
1 change: 1 addition & 0 deletions crates/examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tlsn-formats = { workspace = true }
tlsn-tls-core = { workspace = true }
tls-server-fixture = { workspace = true }
tlsn-server-fixture = { workspace = true }
spansy = { workspace = true }

bincode = { workspace = true }
chrono = { workspace = true }
Expand Down
85 changes: 82 additions & 3 deletions crates/examples/attestation/present.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
// attestation and the corresponding connection secrets. See the `prove.rs`
// example to learn how to acquire an attestation from a Notary.

use tlsn_core::{attestation::Attestation, presentation::Presentation, CryptoProvider, Secrets};
use hyper::header;
use spansy::Spanned;
use tlsn_core::{
attestation::Attestation,
presentation::Presentation,
transcript::{self, Direction},
CryptoProvider, Secrets,
};
use tlsn_formats::http::HttpTranscript;
use utils::range::{Difference, RangeSet, ToRangeSet};

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Read attestation from disk.
Expand All @@ -26,16 +34,87 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
builder.reveal_sent(&request.request.target)?;
// Reveal all headers except the value of the User-Agent header.
for header in &request.headers {
if !header.name.as_str().eq_ignore_ascii_case("User-Agent") {
if !(header
.name
.as_str()
.eq_ignore_ascii_case(header::USER_AGENT.as_str())
|| header
.name
.as_str()
.eq_ignore_ascii_case(header::AUTHORIZATION.as_str()))
{
builder.reveal_sent(header)?;
} else {
builder.reveal_sent(&header.without_value())?;
}
}
// Reveal the entire response.
builder.reveal_recv(&transcript.responses[0])?;
// builder.reveal_recv(&transcript.responses[0])?;

dbg!("foo");

let x = &transcript.responses[0].body.as_ref().unwrap().content;
// dbg!(x);
builder.reveal_recv(&transcript.responses[0].without_data())?;

match x {
tlsn_formats::http::BodyContent::Json(json) => {
let id = json.get("id").unwrap();
let lhs = json.span().to_range_set();
let rhs = id.span().to_range_set();
let diff = lhs.difference(&rhs);

let test = json.get("information.name").unwrap();

let test = json
.span()
.to_range_set()
.difference(&json.get("id").unwrap().span().to_range_set());

let transcript_range_set = transcript.responses[0].span().to_range_set();
let json_range_set = json.span().to_range_set();
println!("transcript: {:?}", &transcript_range_set);
println!("Json: {:?}", &json_range_set);

//works
// let xxx = RangeSet::new(&vec![542..562]);
// builder.reveal_recv(&xxx);

//does not work
let xxx = RangeSet::new(&vec![542..561]);
builder.reveal_recv(&xxx)?;

// if true {
// // works
// builder.reveal_recv(&transcript_range_set)?;
// // builder.reveal_recv(json.get("id").unwrap())?;
// } else {
// // does not work
// builder.reveal_recv(&json_range_set)?;
// }
}
tlsn_formats::http::BodyContent::Unknown(span) => {
// dbg!(&span);
let information = span.data();
let information = String::from_utf8_lossy(information);
let parsed = serde_json::from_str::<serde_json::Value>(&information)?;

let json = spansy::json::parse_str(&information)?;
let id = json.get("id").unwrap();
println!("Reveal: {id:?}");
let offset = span.indices().min().unwrap();
let mut s = id.span().indices();
// s.offset(offset);
builder.reveal_recv(s)?;
// println!("Reveal: {:?}", &transcript.responses[0]);
// builder.reveal_recv(&transcript.responses[0])?;
}
_ => {}
}

println!("test");
let transcript_proof = builder.build()?;
println!("transcript_proof: {transcript_proof:?}");

// Use default crypto provider to build the presentation.
let provider = CryptoProvider::default();
Expand Down
28 changes: 17 additions & 11 deletions crates/examples/attestation/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ use notary_client::{Accepted, NotarizationRequest, NotaryClient};
use tls_server_fixture::SERVER_DOMAIN;
use tlsn_common::config::ProtocolConfig;
use tlsn_core::{request::RequestConfig, transcript::TranscriptCommitConfig};
use tlsn_examples::run_notary;
use tlsn_formats::{
http::{DefaultHttpCommitter, HttpCommit, HttpTranscript},
json::{DefaultJsonCommitter, JsonCommit},
};
use tlsn_formats::http::{DefaultHttpCommitter, HttpCommit, HttpTranscript};
use tlsn_prover::{Prover, ProverConfig};
use tlsn_server_fixture::DEFAULT_FIXTURE_PORT;
use tracing::debug;
Expand Down Expand Up @@ -116,7 +112,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Build a simple HTTP request with common headers
let request = Request::builder()
// .uri("/protected")
.uri("/formats/html")
.uri("/protected")
.header("Host", SERVER_DOMAIN)
.header("Accept", "*/*")
// Using "identity" instructs the Server not to use compression for its HTTP response.
Expand All @@ -138,9 +134,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

// Pretty printing :)
let payload = response.into_body().collect().await.unwrap().to_bytes();
// let parsed =
// serde_json::from_str::<serde_json::Value>(&String::from_utf8_lossy(&payload))?;
let parsed = String::from_utf8_lossy(&payload);
let parsed = serde_json::from_str::<serde_json::Value>(&String::from_utf8_lossy(&payload))?;
debug!("{}", serde_json::to_string_pretty(&parsed).unwrap());

// The prover task should be done now, so we can await it.
Expand All @@ -151,12 +145,24 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

// Parse the HTTP transcript.
let transcript = HttpTranscript::parse(prover.transcript())?;
// dbg!(&transcript);

let x: &spansy::http::BodyContent = &transcript.responses[0].body.as_ref().unwrap().content;

match x {
tlsn_formats::http::BodyContent::Json(json) => {
dbg!(&json);
}
tlsn_formats::http::BodyContent::Unknown(span) => {
dbg!("unknown");
dbg!(&span.indices());
}
_ => {}
}

// Commit to the transcript.
let mut builder = TranscriptCommitConfig::builder(prover.transcript());

//FIXME: JSON?
// DefaultJsonCommitter::default().commit_value(&mut builder, value, direction);
DefaultHttpCommitter::default().commit_transcript(&mut builder, &transcript)?;

prover.transcript_commit(builder.build()?);
Expand Down
4 changes: 4 additions & 0 deletions crates/examples/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

rm -rf example.presentation.tlsn
RUST_LOG=debug cargo run --example attestation_present && RUST_LOG=debug cargo run --example attestation_verify
3 changes: 2 additions & 1 deletion crates/server-fixture/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ hyper-util = { workspace = true, features = ["full"] }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
tokio-util = { workspace = true, features = ["compat", "io"] }
tower-service = { version = "0.3" }

serde_json = { workspace = true }
tlsn-server-fixture-certs = { workspace = true }
spansy = { workspace = true }

[[bin]]
name = "tlsn-server-fixture"
Expand Down
9 changes: 7 additions & 2 deletions crates/server-fixture/server/src/data/1kb.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"state": "XY",
"postalCode": "12345"
},
"favoriteColors": ["blue", "red", "green", "yellow"],
"favoriteColors": [
"blue",
"red",
"green",
"yellow"
],
"description": "John is a software engineer. He enjoys hiking, playing video games, and reading books. His favorite book is 'Moby Dick'.",
"education": {
"degree": "Bachelor's in Computer Science",
Expand Down Expand Up @@ -44,4 +49,4 @@
"lastUpdatedAt": "2023-01-12T16:42:10Z",
"version": 1.2
}
}
}
11 changes: 9 additions & 2 deletions crates/server-fixture/server/src/data/4kb.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
"postalCode": "45678"
},
"bio": "John is a passionate software engineer with over a decade of experience. Throughout his career, he's contributed to many open-source projects, co-authored several tech publications, and spoken at international conferences. In his free time, he loves hiking in national parks, experimenting with new recipes, and mentoring young tech enthusiasts.",
"favoriteColors": ["blue", "red", "green", "yellow", "purple", "orange"],
"favoriteColors": [
"blue",
"red",
"green",
"yellow",
"purple",
"orange"
],
"education": {
"highSchool": {
"name": "Anytown High",
Expand Down Expand Up @@ -85,4 +92,4 @@
"editor": "AdminUser123"
}
}
}
}
2 changes: 1 addition & 1 deletion crates/server-fixture/server/src/data/8kb.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@
]
}
}
}
}
24 changes: 16 additions & 8 deletions crates/server-fixture/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::{

use axum::{
extract::{Query, State},
response::{Html, Json},
response::Html,
routing::get,
Router,
Json, Router,
};
use futures::{channel::oneshot, AsyncRead, AsyncWrite};
use futures_rustls::{
Expand All @@ -22,6 +22,7 @@ use hyper::{
};
use hyper_util::rt::TokioIo;

use serde_json::Value;
use tokio_util::compat::FuturesAsyncReadCompatExt;
use tower_service::Service;

Expand Down Expand Up @@ -100,10 +101,17 @@ async fn bytes(
Ok(Bytes::from(vec![0x42u8; size]))
}

fn get_value(filecontent: &str) -> Result<Value, StatusCode> {
serde_json::from_str(filecontent).map_err(|e| {
eprintln!("Failed to parse JSON data: {}", e);
StatusCode::INTERNAL_SERVER_ERROR
})
}

async fn json(
State(state): State<Arc<Mutex<AppState>>>,
Query(params): Query<HashMap<String, String>>,
) -> Result<Json<&'static str>, StatusCode> {
) -> Result<Json<Value>, StatusCode> {
let size = params
.get("size")
.and_then(|size| size.parse::<usize>().ok())
Expand All @@ -114,9 +122,9 @@ async fn json(
}

match size {
1 => Ok(Json(include_str!("data/1kb.json"))),
4 => Ok(Json(include_str!("data/4kb.json"))),
8 => Ok(Json(include_str!("data/8kb.json"))),
1 => Ok(Json(get_value(include_str!("data/1kb.json"))?)),
4 => Ok(Json(get_value(include_str!("data/4kb.json"))?)),
8 => Ok(Json(get_value(include_str!("data/8kb.json"))?)),
_ => Err(StatusCode::NOT_FOUND),
}
}
Expand Down Expand Up @@ -164,6 +172,6 @@ where
}
}

async fn protected_route(_: AuthenticatedUser) -> Result<Json<&'static str>, StatusCode> {
Ok(Json(include_str!("data/protected_data.json")))
async fn protected_route(_: AuthenticatedUser) -> Result<Json<Value>, StatusCode> {
Ok(Json(get_value(include_str!("data/protected_data.json"))?))
}

0 comments on commit 1a385f2

Please sign in to comment.