day-part-auth 0.1.2

Sign-in for Day apps: OAuth 2.0 and OpenID Connect through the system's auth browser, Sign in with Apple, Google, and passkeys
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
// Copyright © The Daybrite Project
// SPDX-License-Identifier: MPL-2.0

//! The web auth session: the system's own sign-in browser, which returns the URL the provider
//! redirected to. Layer one of the crate; everything above it takes the callback URL from here.
//!
//! | Platform | Browser | Callback |
//! |---|---|---|
//! | iOS, macOS | `ASWebAuthenticationSession` (Swift arm) | returned by the session itself |
//! | Android | a Custom Tab (Java arm) | the app's custom scheme, through the deep-link rail |
//! | Web | a popup of the same origin (JavaScript arm) | the popup posts its URL to the opener |
//! | Windows, Linux | the default browser | a loopback listener, or the deep-link rail |
//! | HarmonyOS | the default browser | the deep-link rail |
//!
//! Embedded web views are not offered on any platform: providers refuse them (RFC 8252 §8.12),
//! and the person has no address bar to check who is asking.
//!
//! # Callbacks on the deep-link rail
//!
//! A custom-scheme redirect arrives like any other URL the OS hands the app: through
//! `day::on_open_url`. The session registers a handler in a scope of its own for the one URL it
//! expects and declines everything else, so the app's handler keeps seeing the rest; the
//! registration goes when the session ends. Registering needs the UI thread, which is where a
//! sign-in starts (a button's action).
//!
//! # The loopback listener
//!
//! [`Loopback`] binds `127.0.0.1` (port 0 lets the OS pick, RFC 8252 §7.3), answers the one
//! request the browser makes with a short "you can return to the app" page, and hands the
//! request URL back. It accepts one connection, on a thread of its own, within the timeout.

use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
#[cfg(any(
    target_os = "android",
    target_os = "windows",
    target_os = "linux",
    target_env = "ohos"
))]
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use crate::AuthError;

/// How the browser session is shown.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SessionOptions {
    /// Ask for a browsing session that shares no cookies with the person's browser, so the
    /// provider does not know them from an earlier sign-in. Honored by Apple
    /// (`prefersEphemeralWebBrowserSession`); the other platforms cannot and ignore it.
    pub ephemeral: bool,
    /// How long a loopback listener waits for the browser's request before giving up with
    /// [`AuthError::Timeout`]. The interactive sessions have no timeout of their own: the
    /// person closes them.
    pub timeout: Duration,
    /// The page the loopback listener answers the browser with. HTML; the default says the
    /// sign-in is done and the window can be closed.
    pub completion_page: Option<String>,
}

impl Default for SessionOptions {
    fn default() -> Self {
        SessionOptions {
            ephemeral: false,
            timeout: Duration::from_secs(300),
            completion_page: None,
        }
    }
}

/// What this platform's session can do.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SessionCapabilities {
    /// Whether a session can be opened at all.
    pub support: day_bridge::Support,
    /// Whether an `https://` callback is received without leaving the browser (Apple from
    /// iOS 17.4 and macOS 14.4 with an associated domain; the web).
    pub https_callback: bool,
    /// Whether [`SessionOptions::ephemeral`] is honored.
    pub ephemeral: bool,
    /// Whether a custom-scheme callback is received (every platform with a deep-link rail).
    pub scheme_callback: bool,
    /// Whether a loopback listener can serve as the callback (the desktops).
    pub loopback: bool,
}

/// What this platform's session can do.
pub fn capabilities() -> SessionCapabilities {
    let apple = cfg!(any(target_os = "ios", target_os = "macos"));
    let desktop = cfg!(any(
        target_os = "windows",
        all(target_os = "linux", not(target_env = "ohos"))
    ));
    let web = cfg!(target_arch = "wasm32");
    let support = if cfg!(any(
        target_os = "ios",
        target_os = "macos",
        target_os = "android",
        target_os = "windows",
        target_os = "linux",
        target_env = "ohos",
        target_arch = "wasm32"
    )) {
        day_bridge::Support::Native
    } else {
        day_bridge::Support::Unsupported
    };
    SessionCapabilities {
        support,
        https_callback: apple || web,
        ephemeral: apple,
        scheme_callback: !web,
        loopback: desktop || apple,
    }
}

/// Open `url` in the system's sign-in browser and resolve with the callback URL once the
/// provider returns to the exact callback endpoint, preserving its registered query fields. The person
/// dismissing the browser resolves with [`AuthError::Cancelled`].
///
/// Call it on the UI thread: the custom-scheme platforms register for the callback on the
/// deep-link rail, which lives there. The future is `Send`; awaited under `day::task` the
/// continuation runs on the UI thread again.
///
/// # Errors
///
/// The future resolves to [`AuthError::Cancelled`] when the person dismisses the browser,
/// [`AuthError::InvalidRequest`] when `url` or the callback is unusable here, and the
/// platform's error otherwise.
pub fn open(
    url: &str,
    callback_prefix: &str,
    options: &SessionOptions,
) -> impl std::future::Future<Output = Result<String, AuthError>> + Send {
    let (tx, rx) = day_async::oneshot();
    start(
        url,
        callback_prefix,
        options,
        Box::new(move |answer| tx.send(answer)),
    );
    let expected = callback_prefix.to_string();
    async move {
        let received = rx.await.unwrap_or(Err(AuthError::Cancelled))?;
        if !crate::urls::callback_matches(&expected, &received) {
            return Err(AuthError::InvalidRequest(
                "authentication callback endpoint mismatch".into(),
            ));
        }
        Ok(received)
    }
}

/// The page's own URL without its fragment, on the web: the redirect the popup flow returns to,
/// and what [`crate::Redirect::Auto`] resolves to there. `None` everywhere else.
pub fn page_url() -> Option<String> {
    if cfg!(target_arch = "wasm32") {
        crate::native::page_url()
            .ok()
            .filter(|u| u.starts_with("https://") || u.starts_with("http://"))
    } else {
        None
    }
}

/// Dismiss the session on screen, if any. Its caller resolves with [`AuthError::Cancelled`].
pub fn cancel() {
    imp::cancel();
}

type OnDone = Box<dyn FnOnce(Result<String, AuthError>) + Send>;

fn start(url: &str, callback_prefix: &str, options: &SessionOptions, on_done: OnDone) {
    if crate::urls::parse(callback_prefix).is_err() {
        on_done(Err(AuthError::InvalidRequest(
            "the callback must be an absolute URL".into(),
        )));
        return;
    }
    if !crate::provider::is_trusted_url(url) {
        on_done(Err(AuthError::InvalidRequest(
            "the sign-in URL must be https (or loopback for a local provider)".into(),
        )));
        return;
    }
    imp::start(url, callback_prefix, options, on_done);
}

/// Wrap a one-shot completion so it can be called from two places and fire once.
#[cfg(any(
    target_os = "android",
    target_os = "windows",
    target_os = "linux",
    target_env = "ohos"
))]
fn once(cb: OnDone) -> Arc<dyn Fn(Result<String, AuthError>) + Send + Sync> {
    let cell = Mutex::new(Some(cb));
    Arc::new(move |answer| {
        let taken = match cell.lock() {
            Ok(mut g) => g.take(),
            Err(poisoned) => poisoned.into_inner().take(),
        };
        if let Some(cb) = taken {
            cb(answer);
        }
    })
}

// ---------------------------------------------------------------------------
// The deep-link rail: a custom-scheme callback through `day::on_open_url`
// ---------------------------------------------------------------------------

// The scope of the rail watch in flight on this (the UI) thread. One at a time: a new session
// replaces the last, and a delivered callback removes its own.
#[cfg(any(
    target_os = "android",
    target_os = "windows",
    target_os = "linux",
    target_env = "ohos"
))]
thread_local! {
    static WATCH: std::cell::RefCell<Option<day_reactive::Scope>> = const { std::cell::RefCell::new(None) };
}

/// Watch the deep-link rail for one URL starting with `prefix`, in a scope of its own, from the
/// UI thread. The handler delivers the URL and removes itself; a session that ends another way
/// leaves the watch until the next session replaces it, which is harmless, since the completion
/// it would deliver to fires once.
#[cfg(any(
    target_os = "android",
    target_os = "windows",
    target_os = "linux",
    target_env = "ohos"
))]
fn watch_rail(prefix: String, deliver: Arc<dyn Fn(Result<String, AuthError>) + Send + Sync>) {
    unwatch_rail();
    let scope = day_reactive::Scope::child();
    scope.enter(|| {
        day_core::on_open_url(move |url| {
            if crate::urls::callback_matches(&prefix, url) {
                deliver(Ok(url.to_string()));
                unwatch_rail();
                true
            } else {
                false
            }
        });
    });
    WATCH.with(|w| *w.borrow_mut() = Some(scope));
}

/// Dispose the rail watch in flight, if any.
#[cfg(any(
    target_os = "android",
    target_os = "windows",
    target_os = "linux",
    target_env = "ohos"
))]
fn unwatch_rail() {
    let taken = WATCH.with(|w| w.borrow_mut().take());
    if let Some(scope) = taken {
        scope.dispose();
    }
}

// ---------------------------------------------------------------------------
// The loopback listener (desktops, and any platform with a browser to open)
// ---------------------------------------------------------------------------

/// A listener on `127.0.0.1` for the provider's redirect.
pub struct Loopback {
    listener: TcpListener,
    redirect_uri: String,
}

impl Loopback {
    /// Bind `127.0.0.1:port` (0 for any free port) and build the redirect URI with the port
    /// the OS gave and `path` (`/callback` by default).
    ///
    /// # Errors
    ///
    /// Returns [`AuthError::Platform`] when the port cannot be bound.
    pub fn bind(port: u16, path: &str) -> Result<Loopback, AuthError> {
        let listener = TcpListener::bind(("127.0.0.1", port))
            .map_err(|e| AuthError::Platform(format!("loopback listener: {e}")))?;
        let bound = listener
            .local_addr()
            .map_err(|e| AuthError::Platform(format!("loopback listener: {e}")))?;
        let path = if path.is_empty() { "/callback" } else { path };
        let path = if path.starts_with('/') {
            path.to_string()
        } else {
            format!("/{path}")
        };
        Ok(Loopback {
            listener,
            redirect_uri: format!("http://127.0.0.1:{}{}", bound.port(), path),
        })
    }

    /// The redirect URI to send the provider: `http://127.0.0.1:<port><path>`.
    pub fn redirect_uri(&self) -> &str {
        &self.redirect_uri
    }

    /// Wait on a thread of its own for the browser's request and resolve with its full URL.
    /// Any request to another path is answered 404 and ignored.
    ///
    /// # Errors
    ///
    /// The future resolves to [`AuthError::Timeout`] when no callback arrives within the
    /// session's limit, and [`AuthError::Platform`] when the listener fails.
    pub fn wait(
        self,
        options: &SessionOptions,
    ) -> impl std::future::Future<Output = Result<String, AuthError>> + Send {
        let (tx, rx) = day_async::oneshot();
        let timeout = options.timeout;
        let page = options
            .completion_page
            .clone()
            .unwrap_or_else(|| DEFAULT_COMPLETION_PAGE.to_string());
        let spawned = std::thread::Builder::new()
            .name("day-auth-loopback".into())
            .spawn(move || tx.send(self.serve(timeout, &page)));
        let started = spawned.is_ok();
        async move {
            if !started {
                return Err(AuthError::Platform(
                    "could not start the loopback thread".into(),
                ));
            }
            rx.await.unwrap_or(Err(AuthError::Cancelled))
        }
    }

    fn serve(self, timeout: Duration, page: &str) -> Result<String, AuthError> {
        let deadline = Instant::now() + timeout;
        self.listener
            .set_nonblocking(true)
            .map_err(|e| AuthError::Platform(format!("loopback listener: {e}")))?;
        let path = self
            .redirect_uri
            .splitn(4, '/')
            .nth(3)
            .map_or_else(|| "/".to_string(), |p| format!("/{p}"));
        loop {
            match self.listener.accept() {
                Ok((stream, _)) => {
                    let _ = stream.set_nonblocking(false);
                    let _ = stream.set_read_timeout(Some(Duration::from_secs(10)));
                    if let Some(url) = self.answer(stream, &path, page) {
                        return Ok(url);
                    }
                }
                Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                    if Instant::now() >= deadline {
                        return Err(AuthError::Timeout);
                    }
                    std::thread::sleep(Duration::from_millis(50));
                }
                Err(e) => return Err(AuthError::Platform(format!("loopback listener: {e}"))),
            }
        }
    }

    /// Read one request line, answer it, and return the full URL when it is the callback.
    fn answer(&self, mut stream: TcpStream, path: &str, page: &str) -> Option<String> {
        let mut buf = Vec::with_capacity(4096);
        let mut chunk = [0u8; 1024];
        loop {
            match stream.read(&mut chunk) {
                Ok(0) | Err(_) => break,
                Ok(n) => {
                    buf.extend_from_slice(&chunk[..n]);
                    if buf.windows(4).any(|w| w == b"\r\n\r\n") || buf.len() > 64 * 1024 {
                        break;
                    }
                }
            }
        }
        let head = String::from_utf8_lossy(&buf);
        let request_line = head.lines().next().unwrap_or("");
        let mut parts = request_line.split(' ');
        let (Some(method), Some(target)) = (parts.next(), parts.next()) else {
            let _ = respond(&mut stream, 400, "text/plain", "bad request");
            return None;
        };
        let target_path = target.split('?').next().unwrap_or("");
        if method != "GET" || target_path != path {
            let _ = respond(&mut stream, 404, "text/plain", "not found");
            return None;
        }
        let _ = respond(&mut stream, 200, "text/html; charset=utf-8", page);
        let base = self.redirect_uri.trim_end_matches(path);
        Some(format!("{base}{target}"))
    }
}

fn respond(
    stream: &mut TcpStream,
    status: u16,
    content_type: &str,
    body: &str,
) -> std::io::Result<()> {
    let reason = match status {
        200 => "OK",
        400 => "Bad Request",
        404 => "Not Found",
        _ => "",
    };
    write!(
        stream,
        "HTTP/1.1 {status} {reason}\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\nCache-Control: no-store\r\n\r\n",
        body.len()
    )?;
    stream.write_all(body.as_bytes())?;
    stream.flush()
}

/// The page the browser shows once the provider has redirected back.
const DEFAULT_COMPLETION_PAGE: &str = "<!doctype html><html><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"><title>Signed in</title><style>body{font-family:system-ui,sans-serif;display:flex;align-items:center;justify-content:center;height:100vh;margin:0;background:#f4f5f7;color:#1f2933}main{text-align:center;padding:2rem}h1{font-size:1.4rem;margin:0 0 .5rem}p{margin:0;color:#52606d}</style></head><body><main><h1>Signed in</h1><p>You can close this window and return to the app.</p></main></body></html>";

// ---------------------------------------------------------------------------
// Per-platform sessions
// ---------------------------------------------------------------------------

/// Apple: `ASWebAuthenticationSession`, through the Swift arm. It returns the callback URL
/// itself, custom scheme or (iOS 17.4, macOS 14.4) https.
#[cfg(any(target_os = "ios", target_os = "macos"))]
mod imp {
    use super::*;
    use crate::native::{close_session, open_session_native_async};

    pub fn start(url: &str, callback_prefix: &str, options: &SessionOptions, on_done: OnDone) {
        let scheme = callback_prefix.split(':').next().unwrap_or("").to_string();
        let _ = open_session_native_async(
            url,
            &scheme,
            callback_prefix,
            options.ephemeral,
            move |answer| on_done(super::read_answer(answer)),
        );
    }

    pub fn cancel() {
        let _ = close_session();
    }
}

/// Android: a Custom Tab through the Java arm; the callback arrives on the deep-link rail. The
/// arm reports when the app's activity resumes, which is the tab closing: a callback that has
/// not arrived shortly after that is a dismissal.
#[cfg(target_os = "android")]
mod imp {
    use super::*;
    use crate::native::{close_session, open_session_native_async};

    pub fn start(url: &str, callback_prefix: &str, options: &SessionOptions, on_done: OnDone) {
        let deliver = once(on_done);
        watch_rail(callback_prefix.to_string(), deliver.clone());
        let scheme = callback_prefix.split(':').next().unwrap_or("").to_string();
        let resumed = deliver.clone();
        let _ = open_session_native_async(
            url,
            &scheme,
            callback_prefix,
            options.ephemeral,
            move |answer| match answer {
                // The tab closed. The deep link, when there is one, is delivered on the UI thread
                // as the activity resumes; give it a moment before calling this a dismissal.
                Ok(_) => {
                    let resumed = resumed.clone();
                    let _ = std::thread::Builder::new()
                        .name("day-auth-resume".into())
                        .spawn(move || {
                            std::thread::sleep(Duration::from_millis(700));
                            resumed(Err(AuthError::Cancelled));
                        });
                }
                Err(e) => resumed(Err(AuthError::from(e))),
            },
        );
    }

    pub fn cancel() {
        let _ = close_session();
    }
}

/// The web: a popup of the same origin through the JavaScript arm, which posts the callback
/// URL to its opener and closes.
#[cfg(target_arch = "wasm32")]
mod imp {
    use super::*;
    use crate::native::{close_session, open_session_native_async};

    pub fn start(url: &str, callback_prefix: &str, options: &SessionOptions, on_done: OnDone) {
        let _ =
            open_session_native_async(url, "", callback_prefix, options.ephemeral, move |answer| {
                on_done(super::read_answer(answer));
            });
    }

    pub fn cancel() {
        let _ = close_session();
    }
}

/// Windows, Linux and HarmonyOS: the default browser, and the callback on the deep-link rail
/// (desktop single-instance forwarding or the HarmonyOS want). A loopback redirect does not come
/// through here: [`Loopback`] serves it.
#[cfg(not(any(
    target_os = "ios",
    target_os = "macos",
    target_os = "android",
    target_arch = "wasm32"
)))]
mod imp {
    use super::*;

    type Deliver = Arc<dyn Fn(Result<String, AuthError>) + Send + Sync>;
    static ACTIVE: Mutex<Option<Deliver>> = Mutex::new(None);

    pub fn start(url: &str, callback_prefix: &str, _options: &SessionOptions, on_done: OnDone) {
        let deliver = once(on_done);
        watch_rail(callback_prefix.to_string(), deliver.clone());
        if let Ok(mut slot) = ACTIVE.lock() {
            *slot = Some(deliver);
        }
        day_core::open_url(url);
    }

    pub fn cancel() {
        let taken = ACTIVE.lock().ok().and_then(|mut s| s.take());
        if let Some(deliver) = taken {
            deliver(Err(AuthError::Cancelled));
        }
    }
}

/// The arm's completion: a URL, `cancelled`, or `error:<message>`.
#[cfg(any(target_os = "ios", target_os = "macos", target_arch = "wasm32"))]
fn read_answer(answer: Result<String, day_bridge::Error>) -> Result<String, AuthError> {
    match answer {
        Ok(text) if text == "cancelled" => Err(AuthError::Cancelled),
        Ok(text) if text == "unsupported" => Err(AuthError::Unsupported),
        Ok(text) if text.starts_with("error:") => Err(AuthError::Platform(text[6..].to_string())),
        Ok(url) if url.contains(':') => Ok(url),
        Ok(other) => Err(AuthError::Platform(format!(
            "unexpected session answer {other:?}"
        ))),
        Err(e) => Err(AuthError::from(e)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn capabilities_answer_for_this_host() {
        let caps = capabilities();
        if cfg!(any(
            target_os = "macos",
            target_os = "linux",
            target_os = "windows"
        )) {
            assert_eq!(caps.support, day_bridge::Support::Native);
            assert!(caps.loopback);
        }
    }

    #[test]
    fn loopback_binds_builds_the_redirect_and_returns_the_request_url() {
        let loopback = Loopback::bind(0, "/callback").expect("bind");
        let redirect = loopback.redirect_uri().to_string();
        assert!(redirect.starts_with("http://127.0.0.1:"));
        assert!(redirect.ends_with("/callback"));
        let options = SessionOptions {
            timeout: Duration::from_secs(5),
            ..SessionOptions::default()
        };
        let (tx, rx) = std::sync::mpsc::channel();
        let server = std::thread::spawn(move || {
            let answer = loopback.serve(options.timeout, "done");
            let _ = tx.send(answer);
        });
        // A request to another path is ignored; the callback is answered and returned.
        let port: u16 = redirect
            .trim_start_matches("http://127.0.0.1:")
            .split('/')
            .next()
            .and_then(|p| p.parse().ok())
            .expect("port");
        let mut other = TcpStream::connect(("127.0.0.1", port)).expect("connect");
        other
            .write_all(b"GET /favicon.ico HTTP/1.1\r\nHost: x\r\n\r\n")
            .expect("write");
        let mut reply = String::new();
        let _ = other.read_to_string(&mut reply);
        assert!(reply.starts_with("HTTP/1.1 404"));
        let mut browser = TcpStream::connect(("127.0.0.1", port)).expect("connect");
        browser
            .write_all(b"GET /callback?code=abc&state=xyz HTTP/1.1\r\nHost: x\r\n\r\n")
            .expect("write");
        let mut reply = String::new();
        let _ = browser.read_to_string(&mut reply);
        assert!(reply.starts_with("HTTP/1.1 200"));
        assert!(reply.ends_with("done"));
        let answer = rx.recv().expect("answer");
        assert_eq!(answer, Ok(format!("{redirect}?code=abc&state=xyz")));
        server.join().expect("join");
    }

    #[test]
    fn loopback_times_out_without_a_request() {
        let loopback = Loopback::bind(0, "cb").expect("bind");
        assert!(loopback.redirect_uri().ends_with("/cb"));
        assert_eq!(
            loopback.serve(Duration::from_millis(120), "done"),
            Err(AuthError::Timeout)
        );
    }

    #[test]
    fn open_rejects_unusable_arguments_through_the_callback() {
        let (tx, rx) = std::sync::mpsc::channel();
        start(
            "https://x",
            "",
            &SessionOptions::default(),
            Box::new(move |a| {
                let _ = tx.send(a);
            }),
        );
        std::assert_matches!(rx.recv(), Ok(Err(AuthError::InvalidRequest(_))));
        let (tx, rx) = std::sync::mpsc::channel();
        start(
            "ftp://x",
            "myapp://cb",
            &SessionOptions::default(),
            Box::new(move |a| {
                let _ = tx.send(a);
            }),
        );
        std::assert_matches!(rx.recv(), Ok(Err(AuthError::InvalidRequest(_))));
    }
}