Updates open_invite to return request and state

This commit is contained in:
onyinyang 2023-01-25 15:02:23 -05:00
parent e117dc5540
commit dd4e80bfe8
No known key found for this signature in database
GPG Key ID: 156A6435430C2036
3 changed files with 93 additions and 18 deletions

View File

@ -10,9 +10,10 @@ license = "MIT"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies] [dependencies]
lox = { git = "https://git-crysp.uwaterloo.ca/iang/lox.git" } lox = { git = "https://gitlab.torproject.org/onyinyang/lox.git" }
wasm-bindgen = "0.2" wasm-bindgen = "0.2"
serde_json = "1.0.87" serde_json = "1.0.87"
serde-wasm-bindgen = "0.4.5"
console_error_panic_hook = "0.1.7" console_error_panic_hook = "0.1.7"

View File

@ -1,9 +1,12 @@
import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js"; import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js";
init().then(() => { init().then(() => {
set_panic_hook(); set_panic_hook();
request_open_invite().then((token) => { requested = request_open_invite().then((token) => {
open_invite(token); open_invite(token);
}); });
// lox_credential = request_new_lox_credential(request_cred[0]).then((lox_cred) => {
// handle_new_lox_credential(request_cred[1], response, pubkey);
// })
}); });
function request_open_invite() { function request_open_invite() {
@ -21,7 +24,7 @@ function request_open_invite() {
function loxServerRequest(data) { function loxServerRequest(data) {
return new Promise((fulfill, reject) => { return new Promise((fulfill, reject) => {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest(data);
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if (xhr.DONE !== xhr.readyState) { if (xhr.DONE !== xhr.readyState) {
return; return;
@ -37,12 +40,28 @@ function loxServerRequest(data) {
return; return;
}; };
try { try {
if (data != ""){
xhr.open('GET', "http://localhost:8001"); xhr.open('GET', "http://localhost:8001");
}
else {
xhr.open("Post", "http://localhost:8001")
xhr.setRequestHeader("Content-Type", "application/json");
var data = JSON.stringify({})
}
} catch (err) { } catch (err) {
console.log("Error connecting to lox bridge db"); console.log("Error connecting to lox bridge db");
reject(); reject();
return; return;
} }
xhr.send(); xhr.send(data);
}); });
} }
// The correct key should be matched against a public commit to the key to
// verify that the key issuer is in fact the correct Bridge Authority
function loxKeyRequest(key_type) {
return new Promise((fulfull, reject) => {
})
}

View File

@ -1,12 +1,15 @@
use wasm_bindgen::prelude::*; use lox::bridge_table::BridgeLine;
use lox::OPENINV_LENGTH; use lox::cred;
use lox::proto::open_invite; use lox::proto::open_invite;
use lox::{IssuerPubKey, OPENINV_LENGTH};
use serde_json; use serde_json;
use serde_wasm_bindgen;
use std::array::TryFromSliceError; use std::array::TryFromSliceError;
use std::panic; use std::panic;
use wasm_bindgen::prelude::*;
#[wasm_bindgen] #[wasm_bindgen]
extern { extern "C" {
#[wasm_bindgen(js_namespace = console)] #[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str); pub fn log(s: &str);
} }
@ -17,20 +20,72 @@ pub fn set_panic_hook() {
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn open_invite(invite: &[u8]) { pub fn open_invite(invite: &[u8]) -> Result<String, JsValue> {
unsafe {
log(&format!("Using invite: {:?}", invite)); log(&format!("Using invite: {:?}", invite));
match validate(invite) {
Ok(token) => {
let (request, _state) = open_invite::request(&token);
let serialized_request = serde_json::to_string(&request).unwrap();
log(&format!("Formatted open invite request: {}", serialized_request));
},
Err(e) => {
log(&format!("{:?}", e));
},
} }
let token = match validate(invite) {
Ok(token) => token,
Err(e) => return Err(JsValue::from(e.to_string())),
};
let (request, state) = open_invite::request(&token);
let serialized_request = serde_json::to_string(&request).unwrap();
let serialized_state = serde_json::to_string(&state).unwrap();
unsafe {
log(&format!(
"Formatted open invite request: {}",
serialized_request
));
}
let open_lox_result = concat_string(serialized_request, serialized_state);
Ok(open_lox_result)
} }
fn concat_string(request: String, state: String) -> String {
let mut new_string: String = "Request:".to_owned();
new_string.push_str(&request);
new_string.push_str(", State:");
new_string.push_str(&state);
return new_string;
}
/*
#[wasm_bindgen]
pub fn handle_new_lox_credential(state: String, response: String) -> Result<String, JsValue> {
unsafe {
log(&format!("Using server response: {:?}", response));
}
let deserialized_state = serde_json::from_str(&state).unwrap();
let deserialized_response = serde_json::from_str(&response).unwrap();
let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, lox_pub) {
Ok(lox_cred) => lox_cred,
Err(e) => return Err(JsValue::from(e.to_string())),
};
let serialized_credential = serde_json::to_string(&lox_cred.0).unwrap();
let serialized_bridgeline= serde_json::to_string(&lox_cred.1).unwrap();
unsafe {
log(&format!("Got new Lox Credential: {}", serialized_credential));
log(&format!("Got new bridgeline: {}", serialized_bridgeline));
}
Ok((serialized_credential, serialized_bridgeline))
} */
/* Somehow get pubkeys and return to function
#[wasm_bindgen]
pub async fn get_pubkey(key_type: String) -> Result<JsValue, JsValue> {
let mut url = "http://localhost:8001/".to_owned() + &key_type;
let res = reqwest::Client::new()
.get(url)
.send()
.await?;
let text = res.text().await?;
let pub_key: IssuerPubKey = serde_json::from_str(&text).unwrap();
Ok(pub_key)
} */
// This should also check the pubkey
fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> {
invite.try_into() invite.try_into()
} }