Skip to content

Commit 7e64612

Browse files
wpfleger96Duncan
andauthored
feat(audio-client): accept a client request in connect (#8497)
## Summary Related: [block/buzz-app#1424](block/buzz-app#1424) (native federated sessions; attaches the assertion on Huddle audio once this lands) `buzz_audio_client::connection::connect` took the audio URL as `&str`, so a host had no way to add headers to the WebSocket upgrade. buzz-app needs to send its NIP-FI user assertion (`Nostr-Federated-Identity: Bearer <jwt>`) on the Huddle audio socket. The relay checks NIP-FI when the audio connection upgrades (`audio/handler.rs` → `check_nip_fi_at_upgrade`). This PR widens the parameter to `impl IntoClientRequest + Unpin`, the same bound `connect_async_tls_with_config` already takes. Nothing else changes: TLS, timeouts and the NIP-42 handshake work as before. Existing `&str`/`&String` callers, including `desktop/src-tauri/src/huddle/relay_api.rs`, compile unchanged. --------- Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: Duncan <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz>
1 parent 93114b4 commit 7e64612

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

‎crates/buzz-audio-client/src/connection.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde_json::{json, Value};
55
use std::{collections::BTreeSet, future::Future, time::Duration};
66
use tokio_tungstenite::{
77
connect_async_tls_with_config,
8-
tungstenite::{protocol::WebSocketConfig, Message},
8+
tungstenite::{client::IntoClientRequest, protocol::WebSocketConfig, Message},
99
Connector, MaybeTlsStream, WebSocketStream,
1010
};
1111

@@ -99,11 +99,12 @@ async fn control(socket: &mut AudioSocket) -> Result<Value, String> {
9999
/// Connect and authenticate using a host-provided asynchronous signer.
100100
///
101101
/// The host validates the destination before calling, and constructs the NIP-42
102-
/// event (including its exact relay tag) from the challenge. No private key is
102+
/// event (including its exact relay tag) from the challenge. `request` is the
103+
/// audio URL, or a request carrying host-supplied upgrade headers. No private key is
103104
/// accepted. Dropping this future cancels connection/admission; the entire
104105
/// operation is bounded to 15 seconds, including signing.
105106
pub async fn connect<F, Fut>(
106-
audio_url: &str,
107+
request: impl IntoClientRequest + Unpin,
107108
parent: Option<&str>,
108109
connector: Option<Connector>,
109110
sign: F,
@@ -117,7 +118,7 @@ where
117118
.max_message_size(Some(65_536))
118119
.max_frame_size(Some(65_536));
119120
let (mut socket, _) =
120-
connect_async_tls_with_config(audio_url, Some(config), false, connector)
121+
connect_async_tls_with_config(request, Some(config), false, connector)
121122
.await
122123
.map_err(|e| format!("audio WS connect failed: {e}"))?;
123124
let challenge = tokio::time::timeout(Duration::from_secs(5), control(&mut socket))

‎crates/buzz-audio-client/tests/connection.rs‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ use futures_util::{SinkExt, StreamExt};
33
use serde_json::{json, Value};
44
use std::time::Duration;
55
use tokio::{net::TcpListener, sync::oneshot};
6-
use tokio_tungstenite::{accept_async, tungstenite::Message};
6+
use tokio_tungstenite::{
7+
accept_async, accept_hdr_async,
8+
tungstenite::{
9+
client::IntoClientRequest,
10+
handshake::server::{Request, Response},
11+
Message,
12+
},
13+
};
714

815
#[tokio::test]
916
async fn authenticates_with_host_signer_and_preserves_v2_media() {
@@ -131,3 +138,52 @@ fn rejects_invalid_rosters_without_truncation() {
131138
)
132139
.is_err());
133140
}
141+
142+
#[tokio::test]
143+
// tungstenite fixes the accept callback's `Result<Response, ErrorResponse>` signature.
144+
#[allow(clippy::result_large_err)]
145+
async fn request_headers_reach_the_upgrade() {
146+
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
147+
let url = format!("ws://{}/huddle/room/audio", listener.local_addr().unwrap());
148+
let server = tokio::spawn(async move {
149+
let (stream, _) = listener.accept().await.unwrap();
150+
let (seen, header) = oneshot::channel();
151+
let mut socket = accept_hdr_async(stream, |request: &Request, response: Response| {
152+
let _ = seen.send(request.headers().get("authorization").cloned());
153+
Ok(response)
154+
})
155+
.await
156+
.unwrap();
157+
socket
158+
.send(Message::Text(
159+
json!({"type":"challenge","challenge":"c"})
160+
.to_string()
161+
.into(),
162+
))
163+
.await
164+
.unwrap();
165+
socket.next().await.unwrap().unwrap();
166+
socket
167+
.send(Message::Text(
168+
json!({"type":"joined","peer_index":0,"peers":[]})
169+
.to_string()
170+
.into(),
171+
))
172+
.await
173+
.unwrap();
174+
header.await.unwrap()
175+
});
176+
let mut request = url.into_client_request().unwrap();
177+
request
178+
.headers_mut()
179+
.insert("authorization", "Bearer upgrade-token".parse().unwrap());
180+
connect(request, None, None, |challenge| async move {
181+
Ok(json!({ "signed": challenge }))
182+
})
183+
.await
184+
.unwrap();
185+
assert_eq!(
186+
server.await.unwrap().unwrap().to_str().unwrap(),
187+
"Bearer upgrade-token"
188+
);
189+
}

0 commit comments

Comments
 (0)