From 3a511615877bf0b59739120f6820c1e29c896bf0 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 6 Feb 2023 17:41:26 -0500 Subject: [PATCH] Add server side for trust promo requests --- crates/lox-distributor/src/main.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/crates/lox-distributor/src/main.rs b/crates/lox-distributor/src/main.rs index 7bba4af..1db0ba5 100644 --- a/crates/lox-distributor/src/main.rs +++ b/crates/lox-distributor/src/main.rs @@ -90,14 +90,16 @@ async fn handle( .unwrap()), _ => match (req.method(), req.uri().path()) { (&Method::GET, "/invite") => Ok::<_, Infallible>(generate_invite(context.db)), + (&Method::GET, "/reachability") => Ok::<_, Infallible>(send_reachability_cred(context.ba)), (&Method::GET, "/pubkeys") => Ok::<_, Infallible>(send_keys(context.ba)), - //TODO: figure out the format of the request and parse it? (&Method::POST, "/openreq") => Ok::<_, Infallible>({ let bytes = body::to_bytes(req.into_body()).await.unwrap(); verify_and_send_open_cred(bytes, context.ba) }), - //(&Method::POST, "/json_api") => api_post_response(req).await, - //(&Method::GET, "/json_api") => api_get_response().await, + (&Method::POST, "/trustpromo") => Ok::<_, Infallible>({ + let bytes = body::to_bytes(req.into_body()).await.unwrap(); + verify_and_send_trust_promo(bytes, context.ba) + }), _ => { // Return 404 not found response. Ok(Response::builder() @@ -121,6 +123,13 @@ fn generate_invite(db: Arc>) -> Response { resp } +fn send_reachability_cred(ba: Arc>) -> Response { + let ba_obj = ba.lock().unwrap(); + let mut resp = Response::new(Body::from(serde_json::to_string(&ba_obj.reachability_pub).unwrap())); + resp.headers_mut().insert("Access-Control-Allow-Origin", HeaderValue::from_static("*")); + resp +} + fn send_keys(ba: Arc>) -> Response { let ba_obj = ba.lock().unwrap(); // vector of public keys (to serialize) @@ -149,6 +158,16 @@ fn verify_and_send_open_cred(request: Bytes, ba: Arc>) -> Resp resp } +fn verify_and_send_trust_promo(request: Bytes, ba: Arc>) -> Response { + let req: proto::trust_promotion::Request = serde_json::from_slice(&request).unwrap(); + let mut ba_obj = ba.lock().unwrap(); + let response = ba_obj.handle_trust_promotion(req).unwrap(); + let trust_promo_resp_str = serde_json::to_string(&response).unwrap(); + let mut resp = Response::new(Body::from(trust_promo_resp_str)); + resp.headers_mut() + .insert("Access-Control-Allow-Origin", HeaderValue::from_static("*")); + resp +} async fn shutdown_signal() { tokio::signal::ctrl_c() .await