From 028156c0b17ba1be71b3c10f29ca7bd1e8c2e782 Mon Sep 17 00:00:00 2001 From: nazeh Date: Mon, 10 Feb 2025 18:49:40 +0300 Subject: [PATCH] fix(http-relay): add cors --- Cargo.lock | 1 + .../authz/3rd-party-app/package-lock.json | 1 + .../3rd-party-app/src/pubky-auth-widget.js | 36 ++++++++++++------- http-relay/Cargo.toml | 1 + http-relay/src/lib.rs | 3 ++ pubky/src/native/api/auth.rs | 5 ++- 6 files changed, 33 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e6fa991..6d9f675 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1294,6 +1294,7 @@ dependencies = [ "axum-server", "futures-util", "tokio", + "tower-http", "tracing", "url", ] diff --git a/examples/authz/3rd-party-app/package-lock.json b/examples/authz/3rd-party-app/package-lock.json index b78c83d..c72e50d 100644 --- a/examples/authz/3rd-party-app/package-lock.json +++ b/examples/authz/3rd-party-app/package-lock.json @@ -17,6 +17,7 @@ } }, "../../../pubky/pkg": { + "name": "@synonymdev/pubky", "version": "0.4.0-rc.4", "license": "MIT", "dependencies": { diff --git a/examples/authz/3rd-party-app/src/pubky-auth-widget.js b/examples/authz/3rd-party-app/src/pubky-auth-widget.js index 8263d27..a7c6230 100644 --- a/examples/authz/3rd-party-app/src/pubky-auth-widget.js +++ b/examples/authz/3rd-party-app/src/pubky-auth-widget.js @@ -52,6 +52,8 @@ export class PubkyAuthWidget extends LitElement { throw new Error("window.pubky is unavailable, make sure to import `@synonymdev/pubky` before this web component.") } + window.pubky.setLogLevel("debug"); + super() this.testnet = false; @@ -92,6 +94,11 @@ export class PubkyAuthWidget extends LitElement { _generateURL() { + if (!this.open) { + return; + } + + let authRequest= this.pubkyClient.authRequest( this.testnet ? TESTNET_HTTP_RELAY : (this.relay || DEFAULT_HTTP_RELAY), this.caps @@ -103,38 +110,41 @@ export class PubkyAuthWidget extends LitElement { console.error(e) }) - this.authUrl = authRequest.url() + this.authRequest = authRequest; this._updateQr(); } _updateQr() { if (this.canvas) { - this._setQr(this.canvas); + QRCode.toCanvas(this.canvas, this.authRequest.url(), { + margin: 2, + scale: 8, + + color: { + light: '#fff', + dark: '#000', + }, + }); } } _setQr(canvas) { this.canvas = canvas - QRCode.toCanvas(canvas, this.authUrl, { - margin: 2, - scale: 8, - - color: { - light: '#fff', - dark: '#000', - }, - }); } _switchOpen() { this.open = !this.open + if (!this.authRequest) { + this._generateURL() + } + setTimeout(() => { this.pubky = null }, 80) } async _copyToClipboard() { try { - await navigator.clipboard.writeText(this.authUrl); + await navigator.clipboard.writeText(this.authRequest.url()); this.showCopied = true; setTimeout(() => { this.showCopied = false }, 1000) } catch (error) { @@ -190,7 +200,7 @@ export class PubkyAuthWidget extends LitElement { ` diff --git a/http-relay/Cargo.toml b/http-relay/Cargo.toml index 2bca62c..7bc0b85 100644 --- a/http-relay/Cargo.toml +++ b/http-relay/Cargo.toml @@ -11,3 +11,4 @@ futures-util = "0.3.31" tokio = { version = "1.42.0", features = ["full"] } tracing = "0.1.41" url = "2.5.4" +tower-http = { version = "0.6.2", features = ["cors", "trace"] } diff --git a/http-relay/src/lib.rs b/http-relay/src/lib.rs index ef5e0d2..367418e 100644 --- a/http-relay/src/lib.rs +++ b/http-relay/src/lib.rs @@ -17,6 +17,7 @@ use axum_server::Handle; use tokio::sync::{oneshot, Mutex}; use futures_util::TryFutureExt; +use tower_http::{cors::CorsLayer, trace::TraceLayer}; use url::Url; // Shared state to store GET requests and their notifications @@ -69,6 +70,8 @@ impl HttpRelay { let app = Router::new() .route("/link/:id", get(link::get).post(link::post)) + .layer(CorsLayer::very_permissive()) + .layer(TraceLayer::new_for_http()) .with_state(shared_state); let http_handle = Handle::new(); diff --git a/pubky/src/native/api/auth.rs b/pubky/src/native/api/auth.rs index 375570c..dbc355f 100644 --- a/pubky/src/native/api/auth.rs +++ b/pubky/src/native/api/auth.rs @@ -271,9 +271,10 @@ impl Client { } } +#[derive(Debug, Clone)] pub struct AuthRequest { url: Url, - rx: flume::Receiver>, + pub(crate) rx: flume::Receiver>, } impl AuthRequest { @@ -283,6 +284,8 @@ impl AuthRequest { } // TODO: Return better errors + + /// Returns the result of an Auth request. pub async fn response(&self) -> Result { self.rx .recv_async()