fix(http-relay): add cors

This commit is contained in:
nazeh
2025-02-10 18:49:40 +03:00
parent bc1960e4e9
commit 028156c0b1
6 changed files with 33 additions and 14 deletions

1
Cargo.lock generated
View File

@@ -1294,6 +1294,7 @@ dependencies = [
"axum-server",
"futures-util",
"tokio",
"tower-http",
"tracing",
"url",
]

View File

@@ -17,6 +17,7 @@
}
},
"../../../pubky/pkg": {
"name": "@synonymdev/pubky",
"version": "0.4.0-rc.4",
"license": "MIT",
"dependencies": {

View File

@@ -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 {
</div>
<button class="card url" @click=${this._copyToClipboard}>
<div class="copied ${this.showCopied ? "show" : ""}">Copied to Clipboard</div>
<p>${this.authUrl}</p>
<p>${this.authRequest?.url()}</p>
<svg width="14" height="16" viewBox="0 0 14 16" fill="none" xmlns="http://www.w3.org/2000/svg"><rect width="10" height="12" rx="2" fill="white"></rect><rect x="3" y="3" width="10" height="12" rx="2" fill="white" stroke="#3B3B3B"></rect></svg>
</button>
`

View File

@@ -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"] }

View File

@@ -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();

View File

@@ -271,9 +271,10 @@ impl Client {
}
}
#[derive(Debug, Clone)]
pub struct AuthRequest {
url: Url,
rx: flume::Receiver<Result<PublicKey>>,
pub(crate) rx: flume::Receiver<Result<PublicKey>>,
}
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<PublicKey> {
self.rx
.recv_async()