Run the ephemeral callback server. Handles two request types: - `OPTIONS /callback` — CORS preflight, returns 204 with CORS headers. - `POST /callback` — JSON body `{"token":"...","code":"..."}`, validates the confirmation code, sends the token through the channel, and returns a JSON success/error response. CORS is restricted to the gateway origin. Requests with a missing or non-matching `Ori
(
listener: TcpListener,
tx: oneshot::Sender<String>,
expected_code: String,
gateway_endpoint: String,
)
| 401 | /// CORS is restricted to the gateway origin. Requests with a missing or |
| 402 | /// non-matching `Origin` header are rejected with 403. |
| 403 | async fn run_callback_server( |
| 404 | listener: TcpListener, |
| 405 | tx: oneshot::Sender<String>, |
| 406 | expected_code: String, |
| 407 | gateway_endpoint: String, |
| 408 | ) { |
| 409 | let state = Arc::new(CallbackServerState { |
| 410 | allowed_origin: extract_origin(&gateway_endpoint), |
| 411 | expected_code, |
| 412 | tx: Mutex::new(Some(tx)), |
| 413 | }); |
| 414 | |
| 415 | loop { |
| 416 | let Ok((stream, _)) = listener.accept().await else { |
| 417 | return; |
| 418 | }; |
| 419 | let state = Arc::clone(&state); |
| 420 | tokio::spawn(async move { |
| 421 | let service = service_fn(move |req| { |
| 422 | let state = Arc::clone(&state); |
| 423 | async move { Ok::<_, Infallible>(handle_callback_request(req, state).await) } |
| 424 | }); |
| 425 | |
| 426 | if let Err(error) = Builder::new(TokioExecutor::new()) |
| 427 | .serve_connection(TokioIo::new(stream), service) |
| 428 | .await |
| 429 | { |
| 430 | debug!(error = %error, "callback server connection failed"); |
| 431 | } |
| 432 | }); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | #[cfg(test)] |
| 437 | mod tests { |
no test coverage detected