diff --git a/client/loginex/src/lib.rs b/client/loginex/src/lib.rs index 09d102f..6c986df 100644 --- a/client/loginex/src/lib.rs +++ b/client/loginex/src/lib.rs @@ -29,6 +29,15 @@ wit_bindgen::generate!({ additional_derives: [PartialEq, serde::Deserialize, serde::Serialize, process_macros::SerdeJsonInto], }); +#[derive(Debug, Serialize, Deserialize)] +enum SignerRequest { + Sign, + Verify { node: String, signature: Vec }, +} +#[derive(Debug, Serialize, Deserialize, SerdeJsonInto)] +struct SignResponse { + signature: Vec, +} #[derive(Debug, Serialize, Deserialize)] enum FrontendRequest { Sign, @@ -37,22 +46,11 @@ enum FrontendRequest { Debug(String), } #[derive(Debug, Serialize, Deserialize)] -enum SignerRequest { - Sign(SignRequest), - Verify { from: Address, data: SignResponse }, -} -#[derive(Debug, Serialize, Deserialize)] -struct SignRequest { +struct LoginMessage { pub site: String, pub time: u64, pub nonce: Option, } -#[derive(Debug, Serialize, Deserialize, SerdeJsonInto)] -struct SignResponse { - pub body: SignRequest, - pub message: Vec, - pub signature: Vec, -} const ICON: &str = include_str!("icon"); #[derive(Debug, Serialize, Deserialize)] @@ -94,6 +92,35 @@ impl VersionedState { } } +fn _signer_test(our: &Address) -> anyhow::Result<()> { + let target = Address::new(our.node(), ("sign", "sign", "sys")); + let login_message = LoginMessage { + site: WEB2_URL.to_string(), + nonce: Some(WEB2_LOGIN_NONCE.to_string()), + time: get_now(), + }; + let blob = serde_json::to_vec(&login_message)?; + // Get the signature from login:sys:sys + let res: SignResponse = Request::to(target.clone()) + .blob_bytes(blob.clone()) + .body(serde_json::to_vec(&SignerRequest::Sign)?) + .send_and_await_response(10)?? + .body() + .try_into()?; + // Send signature to designated endpoint on Web2 app + let body2 = SignerRequest::Verify { + node: our.node().to_string(), + signature: res.signature, + }; + let verify_res = Request::to(target) + .blob_bytes(blob) + .body(serde_json::to_vec(&body2)?) + .send_and_await_response(10)??; + let is_good: bool = serde_json::from_slice(verify_res.body())?; + kiprintln!("verify_res \n{:#?}", is_good); + Ok(()) +} + call_init!(initialize); fn initialize(our: Address) { init_logging(Level::DEBUG, Level::INFO, None, None, None).unwrap(); @@ -115,12 +142,6 @@ fn initialize(our: Address) { http_server .bind_ws_path("/", WsBindingConfig::default()) .unwrap(); - // let http_config = HttpBindingConfig::default().secure_subdomain(true); - - // http_server - // .serve_ui("ui", vec!["/hypr-login"], http_config.clone()) - // .expect("Failed to serve UI"); - // http_server.secure_bind_http_path("/").unwrap(); main_loop(&our, &mut state, &mut http_server); } @@ -207,15 +228,17 @@ fn handle_login_request( let request = serde_json::from_slice::(request_bytes)?; match request { FrontendRequest::Sign => { - let target = Address::new(our.node(), ("login", "login", "sys")); - let lr = SignerRequest::Sign(SignRequest { + let target = Address::new(our.node(), ("sign", "sign", "sys")); + let body = LoginMessage { site: WEB2_URL.to_string(), nonce: Some(WEB2_LOGIN_NONCE.to_string()), time: get_now(), - }); + }; + let body_bytes = serde_json::to_vec(&body)?; // Get the signature from login:sys:sys let res: SignResponse = Request::to(target) - .body(serde_json::to_vec(&lr)?) + .blob_bytes(body_bytes) + .body(serde_json::to_vec(&SignerRequest::Sign)?) .send_and_await_response(10)?? .body() .try_into()?; @@ -255,7 +278,8 @@ fn attempt_login( let mut json_headers = HashMap::new(); json_headers.insert("Content-type".to_string(), "application/json".to_string()); let node = our.node(); - let message = signature_response.message; + let blob = get_blob().ok_or(anyhow::anyhow!("no blob"))?; + let message = blob.bytes(); let signature = signature_response.signature; let json = serde_json::to_vec(&json!({"node":node, "message": message, "signature": signature}))?; diff --git a/client/loginex/src/proxy.rs b/client/loginex/src/proxy.rs index bc51fad..4bc66c1 100644 --- a/client/loginex/src/proxy.rs +++ b/client/loginex/src/proxy.rs @@ -227,6 +227,7 @@ fn mother_script(prefix: &str) -> String { +