Add server side for trust promo requests

This commit is contained in:
onyinyang 2023-02-06 17:41:26 -05:00
parent 8529083285
commit 3a51161587
No known key found for this signature in database
GPG Key ID: 156A6435430C2036
1 changed files with 22 additions and 3 deletions

View File

@ -90,14 +90,16 @@ async fn handle(
.unwrap()), .unwrap()),
_ => match (req.method(), req.uri().path()) { _ => match (req.method(), req.uri().path()) {
(&Method::GET, "/invite") => Ok::<_, Infallible>(generate_invite(context.db)), (&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)), (&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>({ (&Method::POST, "/openreq") => Ok::<_, Infallible>({
let bytes = body::to_bytes(req.into_body()).await.unwrap(); let bytes = body::to_bytes(req.into_body()).await.unwrap();
verify_and_send_open_cred(bytes, context.ba) verify_and_send_open_cred(bytes, context.ba)
}), }),
//(&Method::POST, "/json_api") => api_post_response(req).await, (&Method::POST, "/trustpromo") => Ok::<_, Infallible>({
//(&Method::GET, "/json_api") => api_get_response().await, let bytes = body::to_bytes(req.into_body()).await.unwrap();
verify_and_send_trust_promo(bytes, context.ba)
}),
_ => { _ => {
// Return 404 not found response. // Return 404 not found response.
Ok(Response::builder() Ok(Response::builder()
@ -121,6 +123,13 @@ fn generate_invite(db: Arc<Mutex<lox::BridgeDb>>) -> Response<Body> {
resp resp
} }
fn send_reachability_cred(ba: Arc<Mutex<BridgeAuth>>) -> Response<Body> {
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<Mutex<BridgeAuth>>) -> Response<Body> { fn send_keys(ba: Arc<Mutex<BridgeAuth>>) -> Response<Body> {
let ba_obj = ba.lock().unwrap(); let ba_obj = ba.lock().unwrap();
// vector of public keys (to serialize) // vector of public keys (to serialize)
@ -149,6 +158,16 @@ fn verify_and_send_open_cred(request: Bytes, ba: Arc<Mutex<BridgeAuth>>) -> Resp
resp resp
} }
fn verify_and_send_trust_promo(request: Bytes, ba: Arc<Mutex<BridgeAuth>>) -> Response<Body> {
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() { async fn shutdown_signal() {
tokio::signal::ctrl_c() tokio::signal::ctrl_c()
.await .await