Skip to content

Commit

Permalink
fix: combine requests
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed Feb 22, 2024
1 parent a08cc2a commit dc2c552
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
8 changes: 5 additions & 3 deletions src/http_server/index_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use {
serde::Deserialize,
};

const TEMPLATE: &str = r#"
const csrfToken = '{token}';
const SCRIPT: &str = r#"
// event subscribed by Verify Enclave
window.addEventListener("message", (event) => {
const attestationId = event.data
Expand Down Expand Up @@ -46,5 +45,8 @@ pub(super) async fn get(query: Query<Params>) -> Result<impl IntoResponse, Statu
return Err(StatusCode::BAD_REQUEST);
}

Ok(Html(TEMPLATE.replacen("{token}", &query.token, 1)))
Ok(Html(format!(
"const csrfToken = '{}';{}",
query.token, SCRIPT
)))
}
20 changes: 7 additions & 13 deletions src/http_server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use {
self::index_js::SCRIPT,

Check failure on line 2 in src/http_server/mod.rs

View workflow job for this annotation

GitHub Actions / CI / Check App / Clippy

constant `SCRIPT` is private
crate::{
ContextualCommand,
Domain,
Expand Down Expand Up @@ -34,13 +35,13 @@ use {
StatusCode,
},
serde::{Deserialize, Serialize},
std::{convert::Infallible, future::Future, iter, net::SocketAddr, sync::Arc},
std::{convert::Infallible, future::Future, iter, net::SocketAddr, str::Bytes, sync::Arc},

Check failure on line 38 in src/http_server/mod.rs

View workflow job for this annotation

GitHub Actions / CI / Check App / Clippy

unused import: `str::Bytes`
tap::{Pipe, Tap},
tower_http::cors::{self, CorsLayer},
tracing::{info, instrument},
wc::{
geoip,
geoip::block::{middleware::GeoBlockLayer, BlockingPolicy as GeoBlockingPolicy},
wc::geoip::{
self,
block::{middleware::GeoBlockLayer, BlockingPolicy as GeoBlockingPolicy},
},
};

Expand Down Expand Up @@ -132,7 +133,7 @@ pub async fn run<S, G>(
.layer(cors_layer)
.route("/health", get(health::get(health_provider)))
.route("/attestation", post(attestation::post))
.route("/index.js", get(index_js::get))
.route("/index.js", get(index_js::get)) // TODO remove in next deploy
.route("/:project_id", get(root))
.layer(metrics_layer)
.with_state(Arc::new(state));
Expand Down Expand Up @@ -167,13 +168,6 @@ pub async fn run<S, G>(
);
}

fn index_html(token: &str) -> String {
format!(
"<!-- index.html --><html><head><script \
src=\"/index.js?token={token}\"></script></head></html>"
)
}

const UNKNOWN_PROJECT_MSG: &str = "Project with the provided ID doesn't exist. Please, ensure \
that the project is registered on cloud.walletconnect.com";

Expand All @@ -194,7 +188,7 @@ where
VerifyStatus::Disabled => String::new().into_response(),
VerifyStatus::Enabled { verified_domains } => {
let token = s.token_manager.generate_csrf_token()?;
let html = index_html(&token);
let html = format!("<script>const csrfToken = '{token}';{SCRIPT}</script>");
let csp = build_content_security_header(verified_domains);
let headers = [
(header::CONTENT_SECURITY_POLICY, csp),
Expand Down

0 comments on commit dc2c552

Please sign in to comment.