diff --git a/examples/authz/authenticator.rs b/examples/authz/authenticator.rs index 691dd4b..8622547 100644 --- a/examples/authz/authenticator.rs +++ b/examples/authz/authenticator.rs @@ -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(()) } diff --git a/pubky/Cargo.toml b/pubky/Cargo.toml index 9c3a23d..c41e7f4 100644 --- a/pubky/Cargo.toml +++ b/pubky/Cargo.toml @@ -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" diff --git a/pubky/src/native/api/auth.rs b/pubky/src/native/api/auth.rs index 1f21993..3a0bf86 100644 --- a/pubky/src/native/api/auth.rs +++ b/pubky/src/native/api/auth.rs @@ -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> { self.inner_session(pubky).await @@ -46,20 +45,24 @@ impl Client { &self, relay: T, capabilities: &Capabilities, - ) -> Result<(Url, tokio::sync::oneshot::Receiver>)> { + ) -> Result { + // 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::>(); + 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( &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>, +} + +impl AuthRequest { + /// Returns the Pubky Auth URL. + pub fn url(&self) -> &Url { + &self.url + } + + pub async fn response(&self) -> Result { + self.rx + .recv_async() + .await + .expect("sender dropped unexpectedly") + } +}