feat(pubky): reconnect to timeout Auth request until the receiver is dropped

This commit is contained in:
nazeh
2025-02-09 12:59:14 +03:00
parent 893300d6b2
commit 5fbbdb577b
3 changed files with 37 additions and 14 deletions

View File

@@ -83,7 +83,7 @@ async fn main() -> Result<()> {
println!("Sending AuthToken to the 3rd party app...");
client.send_auth_token(&keypair, url).await?;
client.send_auth_token(&keypair, &url).await?;
Ok(())
}

View File

@@ -24,6 +24,8 @@ cookie = "0.18.1"
tracing = "0.1.41"
cookie_store = { version = "0.21.1", default-features = false }
anyhow = "1.0.94"
flume = { version = "0.11.1", default-features = false, features = ["async"] }
futures-util = "0.3.31"
# Native dependencies
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
@@ -46,16 +48,15 @@ web-sys = "0.3.76"
anyhow = "1.0.94"
axum = "0.7.9"
axum-server = "0.7.1"
futures-util = "0.3.31"
http-relay = { path = "../http-relay" }
pubky-homeserver = { path = "../pubky-homeserver" }
tokio = "1.42.0"
[build-dependencies]
cfg_aliases = "0.2.1"
[package.metadata.docs.rs]
all-features = true
[package.metadata.wasm-pack.profile.release]
wasm-opt = ['-g', '-O']
# [lints.clippy]
unwrap_used = "deny"

View File

@@ -1,7 +1,6 @@
use pkarr::Keypair;
use pubky_common::session::Session;
use reqwest::IntoUrl;
use tokio::sync::oneshot;
use url::Url;
use pkarr::PublicKey;
@@ -21,9 +20,9 @@ impl Client {
self.inner_signup(keypair, homeserver).await
}
/// Check the current sesison for a given Pubky in its homeserver.
/// Check the current sessison for a given Pubky in its homeserver.
///
/// Returns [Session] or `None` (if recieved `404 NOT_FOUND`),
/// Returns [Session] or `None` (if received `404 NOT_FOUND`),
/// or [reqwest::Error] if the response has any other `>=400` status code.
pub async fn session(&self, pubky: &PublicKey) -> Result<Option<Session>> {
self.inner_session(pubky).await
@@ -46,20 +45,24 @@ impl Client {
&self,
relay: T,
capabilities: &Capabilities,
) -> Result<(Url, tokio::sync::oneshot::Receiver<Result<PublicKey>>)> {
) -> Result<AuthRequest> {
// TODO: use `async_compat` to remove the dependency on Tokio runtime.
let mut relay: Url = relay.into_url()?;
let (pubkyauth_url, client_secret) = self.create_auth_request(&mut relay, capabilities)?;
let (url, client_secret) = self.create_auth_request(&mut relay, capabilities)?;
let (tx, rx) = oneshot::channel::<Result<PublicKey>>();
let (tx, rx) = flume::bounded(1);
let this = self.clone();
tokio::spawn(async move {
tx.send(this.subscribe_to_auth_response(relay, &client_secret).await)
let result = this
.subscribe_to_auth_response(relay, &client_secret, tx.clone())
.await;
tx.send(result)
});
Ok((pubkyauth_url, rx))
Ok(AuthRequest { url, rx })
}
/// Sign an [pubky_common::auth::AuthToken], encrypt it and send it to the
@@ -67,8 +70,27 @@ impl Client {
pub async fn send_auth_token<T: IntoUrl>(
&self,
keypair: &Keypair,
pubkyauth_url: T,
pubkyauth_url: &T,
) -> Result<()> {
self.inner_send_auth_token(keypair, pubkyauth_url).await
}
}
pub struct AuthRequest {
url: Url,
rx: flume::Receiver<Result<PublicKey>>,
}
impl AuthRequest {
/// Returns the Pubky Auth URL.
pub fn url(&self) -> &Url {
&self.url
}
pub async fn response(&self) -> Result<PublicKey> {
self.rx
.recv_async()
.await
.expect("sender dropped unexpectedly")
}
}