-
Notifications
You must be signed in to change notification settings - Fork 588
Expand file tree
/
Copy pathauth.rs
More file actions
869 lines (767 loc) · 28.4 KB
/
auth.rs
File metadata and controls
869 lines (767 loc) · 28.4 KB
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//! Browser-based Cloudflare Access authentication flow.
//!
//! Opens the user's browser to the gateway's `/auth/connect` page, which
//! (after Cloudflare Access login) extracts the `CF_Authorization` cookie
//! and sends it via an XHR POST to a localhost callback server running here.
//! A confirmation code binds the browser session to this CLI session,
//! preventing port-redirection attacks.
use bytes::Bytes;
use http_body_util::{BodyExt, Full};
use hyper::{
Method, Request, Response, StatusCode,
body::Incoming,
header::{
ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN,
ACCESS_CONTROL_MAX_AGE, CONTENT_TYPE, ORIGIN, VARY,
},
service::service_fn,
};
use hyper_util::{
rt::{TokioExecutor, TokioIo},
server::conn::auto::Builder,
};
use miette::{IntoDiagnostic, Result};
use serde::Deserialize;
use std::convert::Infallible;
use std::io::Write;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::net::TcpListener;
use tokio::sync::oneshot;
use tracing::debug;
/// Timeout for the browser auth flow.
const AUTH_TIMEOUT: Duration = Duration::from_secs(120);
/// Length of the confirmation code (alphanumeric characters).
const CODE_LENGTH: usize = 7;
/// Generate a random alphanumeric confirmation code (e.g. "A7X-3KPX").
///
/// Uses a dash separator in the middle for readability.
fn generate_confirmation_code() -> String {
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hasher};
let charset = b"ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; // no 0/O/1/I ambiguity
let mut code = String::with_capacity(CODE_LENGTH + 1); // +1 for dash
// Use two independent RandomState instances as entropy sources. Each
// `RandomState::new()` is seeded from the OS. We combine both hashers'
// output per character to avoid depending on a single seed, and mix in
// the character index plus the previous hash for avalanche diffusion.
let state_a = RandomState::new();
let state_b = RandomState::new();
let mut prev_hash: u64 = 0;
for i in 0..CODE_LENGTH {
if i == 3 {
code.push('-');
}
let mut hasher_a = state_a.build_hasher();
hasher_a.write_usize(i);
hasher_a.write_u64(prev_hash);
let hash_a = hasher_a.finish();
let mut hasher_b = state_b.build_hasher();
hasher_b.write_u64(hash_a);
hasher_b.write_usize(i);
let hash_b = hasher_b.finish();
prev_hash = hash_b;
let idx = (hash_b as usize) % charset.len();
code.push(charset[idx] as char);
}
code
}
/// Run the browser-based CF Access auth flow.
///
/// 1. Generates a one-time confirmation code
/// 2. Starts an ephemeral localhost HTTP server
/// 3. Opens the browser to the gateway's `/auth/connect` page
/// 4. Waits for the XHR POST callback with the CF JWT and matching code
/// 5. Returns the token
pub async fn browser_auth_flow(gateway_endpoint: &str) -> Result<String> {
let listener = TcpListener::bind("127.0.0.1:0").await.into_diagnostic()?;
let local_addr = listener.local_addr().into_diagnostic()?;
let callback_port = local_addr.port();
let code = generate_confirmation_code();
let auth_url = format!(
"{}/auth/connect?callback_port={callback_port}&code={code}",
gateway_endpoint.trim_end_matches('/')
);
// Channel to receive the token from the callback handler.
let (tx, rx) = oneshot::channel::<String>();
// Spawn the callback server.
let server_handle = tokio::spawn(run_callback_server(
listener,
tx,
code.clone(),
gateway_endpoint.to_string(),
));
// Allow suppressing the browser popup via environment variable (useful for
// CI, e2e tests, and headless environments).
let no_browser = std::env::var("OPENSHELL_NO_BROWSER")
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(false);
// Prompt the user before opening the browser.
eprintln!(" Confirmation code: {code}");
eprintln!(" Verify this code matches your browser before clicking Connect.");
eprintln!();
if no_browser {
eprintln!("Browser opening suppressed (OPENSHELL_NO_BROWSER is set).");
eprintln!("Open this URL in your browser:");
eprintln!(" {auth_url}");
eprintln!();
} else {
eprint!("Press Enter to open the browser for authentication...");
std::io::stderr().flush().ok();
let mut _input = String::new();
std::io::stdin().read_line(&mut _input).ok();
if let Err(e) = open_browser(&auth_url) {
debug!(error = %e, "failed to open browser");
eprintln!("Could not open browser automatically.");
eprintln!("Open this URL in your browser:");
eprintln!(" {auth_url}");
eprintln!();
} else {
eprintln!("Browser opened.");
}
}
// Wait for the callback or timeout.
let token = tokio::select! {
result = rx => {
result.map_err(|_| miette::miette!("auth callback channel closed unexpectedly"))?
}
() = tokio::time::sleep(AUTH_TIMEOUT) => {
return Err(miette::miette!(
"authentication timed out after {} seconds.\n\
Try again with: openshell gateway login",
AUTH_TIMEOUT.as_secs()
));
}
};
// Abort the server task (it may still be running if the OS reuses the
// listener after the first accepted connection).
server_handle.abort();
Ok(token)
}
/// Open a URL in the default browser.
fn open_browser(url: &str) -> std::result::Result<(), String> {
#[cfg(target_os = "macos")]
{
std::process::Command::new("open")
.arg(url)
.spawn()
.map_err(|e| format!("failed to run `open`: {e}"))?;
}
#[cfg(target_os = "linux")]
{
std::process::Command::new("xdg-open")
.arg(url)
.spawn()
.map_err(|e| format!("failed to run `xdg-open`: {e}"))?;
}
#[cfg(target_os = "windows")]
{
std::process::Command::new("cmd")
.args(["/C", "start", url])
.spawn()
.map_err(|e| format!("failed to open browser: {e}"))?;
}
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
{
return Err("unsupported platform for browser opening".to_string());
}
Ok(())
}
/// Extract the origin (scheme + host) from a gateway endpoint URL.
///
/// For example, `https://8080-3vdegyusg.brevlab.com/some/path` → `https://8080-3vdegyusg.brevlab.com`.
/// Returns `None` if the URL cannot be parsed.
fn extract_origin(gateway_endpoint: &str) -> Option<String> {
// Split on "://" to get scheme and the rest.
let (scheme, rest) = gateway_endpoint.split_once("://")?;
// The host (with optional port) is everything before the first '/'.
let host = rest.split('/').next().unwrap_or(rest);
Some(format!("{scheme}://{host}"))
}
#[derive(Deserialize)]
struct CallbackPayload {
token: String,
code: String,
}
struct CallbackServerState {
allowed_origin: Option<String>,
expected_code: String,
tx: Mutex<Option<oneshot::Sender<String>>>,
}
impl CallbackServerState {
fn take_sender(&self) -> Option<oneshot::Sender<String>> {
self.tx
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.take()
}
}
type CallbackResponse = Response<Full<Bytes>>;
fn callback_response(
status: StatusCode,
allowed_origin: Option<&str>,
content_type: Option<&str>,
body: impl Into<Bytes>,
) -> CallbackResponse {
let mut response = Response::new(Full::new(body.into()));
*response.status_mut() = status;
let headers = response.headers_mut();
if let Some(origin) = allowed_origin {
headers.insert(
ACCESS_CONTROL_ALLOW_ORIGIN,
hyper::header::HeaderValue::from_str(origin).expect("callback origin is valid"),
);
headers.insert(
ACCESS_CONTROL_ALLOW_METHODS,
hyper::header::HeaderValue::from_static("POST, OPTIONS"),
);
headers.insert(
ACCESS_CONTROL_ALLOW_HEADERS,
hyper::header::HeaderValue::from_static("Content-Type"),
);
headers.insert(
ACCESS_CONTROL_MAX_AGE,
hyper::header::HeaderValue::from_static("60"),
);
headers.insert(VARY, hyper::header::HeaderValue::from_static("Origin"));
}
if let Some(content_type) = content_type {
headers.insert(
CONTENT_TYPE,
hyper::header::HeaderValue::from_str(content_type)
.expect("callback content type is valid"),
);
}
response
}
fn empty_response(status: StatusCode, allowed_origin: Option<&str>) -> CallbackResponse {
callback_response(status, allowed_origin, None, Bytes::new())
}
fn json_response(
status: StatusCode,
allowed_origin: Option<&str>,
body: &'static str,
) -> CallbackResponse {
callback_response(status, allowed_origin, Some("application/json"), body)
}
async fn handle_callback_request(
req: Request<Incoming>,
state: Arc<CallbackServerState>,
) -> CallbackResponse {
let method = req.method().clone();
let path = req.uri().path().to_string();
debug!(method = %method, path = %path, "callback request received");
let allowed_origin = state.allowed_origin.as_deref();
if path != "/callback" {
return empty_response(StatusCode::NOT_FOUND, allowed_origin);
}
if let Some(expected_origin) = allowed_origin {
let request_origin = req
.headers()
.get(ORIGIN)
.and_then(|value| value.to_str().ok());
if request_origin != Some(expected_origin) {
debug!(
request_origin = ?request_origin,
allowed_origin = expected_origin,
"callback origin mismatch"
);
let _ = state.take_sender();
return json_response(
StatusCode::FORBIDDEN,
allowed_origin,
r#"{"ok":false,"error":"origin not allowed"}"#,
);
}
}
if method == Method::OPTIONS {
return empty_response(StatusCode::NO_CONTENT, allowed_origin);
}
if method != Method::POST {
return json_response(
StatusCode::METHOD_NOT_ALLOWED,
allowed_origin,
r#"{"error":"method not allowed"}"#,
);
}
let body = match req.into_body().collect().await {
Ok(body) => body.to_bytes(),
Err(error) => {
debug!(error = %error, "failed to read callback body");
let _ = state.take_sender();
return json_response(
StatusCode::BAD_REQUEST,
allowed_origin,
r#"{"ok":false,"error":"invalid request body"}"#,
);
}
};
let payload: CallbackPayload = match serde_json::from_slice(&body) {
Ok(payload) => payload,
Err(error) => {
debug!(error = %error, "failed to decode callback JSON");
let _ = state.take_sender();
return json_response(
StatusCode::BAD_REQUEST,
allowed_origin,
r#"{"ok":false,"error":"missing token or code"}"#,
);
}
};
if payload.token.is_empty() || payload.code.is_empty() {
let _ = state.take_sender();
return json_response(
StatusCode::BAD_REQUEST,
allowed_origin,
r#"{"ok":false,"error":"missing token or code"}"#,
);
}
if payload.code != state.expected_code {
let _ = state.take_sender();
return json_response(
StatusCode::FORBIDDEN,
allowed_origin,
r#"{"ok":false,"error":"confirmation code mismatch"}"#,
);
}
if let Some(sender) = state.take_sender() {
let _ = sender.send(payload.token);
}
json_response(StatusCode::OK, allowed_origin, r#"{"ok":true}"#)
}
/// 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 `Origin` header are rejected with 403.
async fn run_callback_server(
listener: TcpListener,
tx: oneshot::Sender<String>,
expected_code: String,
gateway_endpoint: String,
) {
let state = Arc::new(CallbackServerState {
allowed_origin: extract_origin(&gateway_endpoint),
expected_code,
tx: Mutex::new(Some(tx)),
});
loop {
let Ok((stream, _)) = listener.accept().await else {
return;
};
let state = Arc::clone(&state);
tokio::spawn(async move {
let service = service_fn(move |req| {
let state = Arc::clone(&state);
async move { Ok::<_, Infallible>(handle_callback_request(req, state).await) }
});
if let Err(error) = Builder::new(TokioExecutor::new())
.serve_connection(TokioIo::new(stream), service)
.await
{
debug!(error = %error, "callback server connection failed");
}
});
}
}
#[cfg(test)]
mod tests {
use super::*;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
// ---------------------------------------------------------------
// Confirmation code generation
// ---------------------------------------------------------------
#[test]
fn confirmation_code_format() {
let code = generate_confirmation_code();
// Format: XXX-XXXX (3 chars, dash, 4 chars) = 8 chars total
assert_eq!(code.len(), 8, "code should be 8 chars: {code}");
assert_eq!(
code.chars().nth(3),
Some('-'),
"code should have dash at position 3: {code}"
);
assert!(
code.chars().all(|c| c.is_ascii_alphanumeric() || c == '-'),
"code should be alphanumeric + dash: {code}"
);
}
#[test]
fn confirmation_codes_are_unique() {
let codes: Vec<_> = (0..10).map(|_| generate_confirmation_code()).collect();
// While theoretically possible to collide, 10 codes from a 32^7 space
// should practically never collide.
let unique: std::collections::HashSet<_> = codes.iter().collect();
assert!(
unique.len() > 1,
"codes should be random, got all identical: {codes:?}"
);
}
// ---------------------------------------------------------------
// Origin extraction
// ---------------------------------------------------------------
#[test]
fn extract_origin_https() {
assert_eq!(
extract_origin("https://gateway.example.com"),
Some("https://gateway.example.com".to_string())
);
}
#[test]
fn extract_origin_with_port() {
assert_eq!(
extract_origin("https://8080-abc.brevlab.com:8080/some/path"),
Some("https://8080-abc.brevlab.com:8080".to_string())
);
}
#[test]
fn extract_origin_strips_path() {
assert_eq!(
extract_origin("https://gateway.example.com/auth/connect?foo=bar"),
Some("https://gateway.example.com".to_string())
);
}
#[test]
fn extract_origin_no_scheme() {
assert_eq!(extract_origin("gateway.example.com"), None);
}
// ---------------------------------------------------------------
// Callback server integration tests
// ---------------------------------------------------------------
const TEST_GATEWAY: &str = "https://gateway.example.com";
#[tokio::test]
async fn callback_server_captures_token_with_valid_code() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = oneshot::channel();
tokio::spawn(run_callback_server(
listener,
tx,
"ABC-1234".to_string(),
TEST_GATEWAY.to_string(),
));
// Simulate a browser XHR POST.
let mut stream = tokio::net::TcpStream::connect(addr).await.unwrap();
let body = r#"{"token":"test-jwt-123","code":"ABC-1234"}"#;
let request = format!(
"POST /callback HTTP/1.1\r\n\
Host: 127.0.0.1\r\n\
Origin: {TEST_GATEWAY}\r\n\
Content-Type: application/json\r\n\
Content-Length: {}\r\n\r\n\
{}",
body.len(),
body,
);
stream.write_all(request.as_bytes()).await.unwrap();
let token = rx.await.unwrap();
assert_eq!(token, "test-jwt-123");
}
#[tokio::test]
async fn callback_server_cors_reflects_gateway_origin() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (tx, _rx) = oneshot::channel();
tokio::spawn(run_callback_server(
listener,
tx,
"ABC-1234".to_string(),
TEST_GATEWAY.to_string(),
));
let mut stream = tokio::net::TcpStream::connect(addr).await.unwrap();
let body = r#"{"token":"jwt","code":"ABC-1234"}"#;
let request = format!(
"POST /callback HTTP/1.1\r\n\
Host: 127.0.0.1\r\n\
Origin: {TEST_GATEWAY}\r\n\
Content-Type: application/json\r\n\
Content-Length: {}\r\n\r\n\
{}",
body.len(),
body,
);
stream.write_all(request.as_bytes()).await.unwrap();
let mut buf = vec![0u8; 4096];
let n = stream.read(&mut buf).await.unwrap();
let response = String::from_utf8_lossy(&buf[..n]);
let expected_header = format!("access-control-allow-origin: {TEST_GATEWAY}");
assert!(
response
.lines()
.any(|line| line.eq_ignore_ascii_case(&expected_header)),
"response should reflect gateway origin:\n{response}"
);
assert!(
!response
.lines()
.any(|line| line.eq_ignore_ascii_case("access-control-allow-origin: *")),
"response should NOT use wildcard origin:\n{response}"
);
}
#[tokio::test]
async fn callback_server_rejects_wrong_origin() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = oneshot::channel();
tokio::spawn(run_callback_server(
listener,
tx,
"ABC-1234".to_string(),
TEST_GATEWAY.to_string(),
));
let mut stream = tokio::net::TcpStream::connect(addr).await.unwrap();
let body = r#"{"token":"jwt","code":"ABC-1234"}"#;
let request = format!(
"POST /callback HTTP/1.1\r\n\
Host: 127.0.0.1\r\n\
Origin: https://evil.example.com\r\n\
Content-Type: application/json\r\n\
Content-Length: {}\r\n\r\n\
{}",
body.len(),
body,
);
stream.write_all(request.as_bytes()).await.unwrap();
let mut buf = vec![0u8; 4096];
let n = stream.read(&mut buf).await.unwrap();
let response = String::from_utf8_lossy(&buf[..n]);
assert!(
response.contains("403 Forbidden"),
"wrong origin should return 403:\n{response}"
);
assert!(
response.contains("origin not allowed"),
"should explain the error:\n{response}"
);
// Token channel should not receive a value.
assert!(
rx.await.is_err(),
"token channel should not receive a value with wrong origin"
);
}
#[tokio::test]
async fn callback_server_rejects_missing_origin() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = oneshot::channel();
tokio::spawn(run_callback_server(
listener,
tx,
"ABC-1234".to_string(),
TEST_GATEWAY.to_string(),
));
// POST without Origin header.
let mut stream = tokio::net::TcpStream::connect(addr).await.unwrap();
let body = r#"{"token":"jwt","code":"ABC-1234"}"#;
let request = format!(
"POST /callback HTTP/1.1\r\n\
Host: 127.0.0.1\r\n\
Content-Type: application/json\r\n\
Content-Length: {}\r\n\r\n\
{}",
body.len(),
body,
);
stream.write_all(request.as_bytes()).await.unwrap();
let mut buf = vec![0u8; 4096];
let n = stream.read(&mut buf).await.unwrap();
let response = String::from_utf8_lossy(&buf[..n]);
assert!(
response.contains("403 Forbidden"),
"missing origin should return 403:\n{response}"
);
assert!(
response.contains("origin not allowed"),
"should explain the error:\n{response}"
);
assert!(
rx.await.is_err(),
"token channel should not receive a value without origin"
);
}
#[tokio::test]
async fn callback_server_rejects_wrong_code() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = oneshot::channel();
tokio::spawn(run_callback_server(
listener,
tx,
"ABC-1234".to_string(),
TEST_GATEWAY.to_string(),
));
let mut stream = tokio::net::TcpStream::connect(addr).await.unwrap();
let body = r#"{"token":"test-jwt","code":"WRONG-CODE"}"#;
let request = format!(
"POST /callback HTTP/1.1\r\n\
Host: 127.0.0.1\r\n\
Origin: {TEST_GATEWAY}\r\n\
Content-Type: application/json\r\n\
Content-Length: {}\r\n\r\n\
{}",
body.len(),
body,
);
stream.write_all(request.as_bytes()).await.unwrap();
// Read the response — should be 403.
let mut buf = vec![0u8; 4096];
let n = stream.read(&mut buf).await.unwrap();
let response = String::from_utf8_lossy(&buf[..n]);
assert!(
response.contains("403 Forbidden"),
"wrong code should return 403:\n{response}"
);
assert!(
response.contains("confirmation code mismatch"),
"should explain the error:\n{response}"
);
// Token channel should not receive a value.
assert!(
rx.await.is_err(),
"token channel should not receive a value with wrong code"
);
}
#[tokio::test]
async fn callback_server_rejects_missing_fields() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = oneshot::channel();
tokio::spawn(run_callback_server(
listener,
tx,
"ABC-1234".to_string(),
TEST_GATEWAY.to_string(),
));
// POST with no body.
let mut stream = tokio::net::TcpStream::connect(addr).await.unwrap();
let request = format!(
"POST /callback HTTP/1.1\r\n\
Host: 127.0.0.1\r\n\
Origin: {TEST_GATEWAY}\r\n\
Content-Length: 0\r\n\r\n"
);
stream.write_all(request.as_bytes()).await.unwrap();
let mut buf = vec![0u8; 4096];
let n = stream.read(&mut buf).await.unwrap();
let response = String::from_utf8_lossy(&buf[..n]);
assert!(
response.contains("400 Bad Request"),
"missing fields should return 400:\n{response}"
);
assert!(
rx.await.is_err(),
"token channel should not receive a value when fields are missing"
);
}
#[tokio::test]
async fn callback_server_handles_cors_preflight() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = oneshot::channel();
tokio::spawn(run_callback_server(
listener,
tx,
"ABC-1234".to_string(),
TEST_GATEWAY.to_string(),
));
// Send OPTIONS preflight with correct origin.
let mut stream = tokio::net::TcpStream::connect(addr).await.unwrap();
let preflight = format!(
"OPTIONS /callback HTTP/1.1\r\n\
Host: 127.0.0.1\r\n\
Origin: {TEST_GATEWAY}\r\n\r\n"
);
stream.write_all(preflight.as_bytes()).await.unwrap();
let mut buf = vec![0u8; 4096];
let n = stream.read(&mut buf).await.unwrap();
let response = String::from_utf8_lossy(&buf[..n]);
let expected_header = format!("access-control-allow-origin: {TEST_GATEWAY}");
assert!(
response.contains("204 No Content"),
"preflight should return 204:\n{response}"
);
assert!(
response
.lines()
.any(|line| line.eq_ignore_ascii_case(&expected_header)),
"preflight should reflect gateway origin:\n{response}"
);
// Now send the actual POST — the server should still be listening.
let mut stream = tokio::net::TcpStream::connect(addr).await.unwrap();
let body = r#"{"token":"jwt-after-preflight","code":"ABC-1234"}"#;
let request = format!(
"POST /callback HTTP/1.1\r\n\
Host: 127.0.0.1\r\n\
Origin: {TEST_GATEWAY}\r\n\
Content-Type: application/json\r\n\
Content-Length: {}\r\n\r\n\
{}",
body.len(),
body,
);
stream.write_all(request.as_bytes()).await.unwrap();
let token = rx.await.unwrap();
assert_eq!(token, "jwt-after-preflight");
}
#[tokio::test]
async fn callback_server_handles_fragmented_post_body() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = oneshot::channel();
tokio::spawn(run_callback_server(
listener,
tx,
"ABC-1234".to_string(),
TEST_GATEWAY.to_string(),
));
let mut stream = tokio::net::TcpStream::connect(addr).await.unwrap();
let body = r#"{"token":"fragmented-jwt","code":"ABC-1234"}"#;
let headers = format!(
"POST /callback HTTP/1.1\r\n\
Host: 127.0.0.1\r\n\
Origin: {TEST_GATEWAY}\r\n\
Content-Type: application/json\r\n\
Content-Length: {}\r\n\r\n",
body.len(),
);
stream.write_all(headers.as_bytes()).await.unwrap();
stream.write_all(&body.as_bytes()[..18]).await.unwrap();
tokio::time::sleep(Duration::from_millis(20)).await;
stream.write_all(&body.as_bytes()[18..]).await.unwrap();
let token = rx.await.unwrap();
assert_eq!(token, "fragmented-jwt");
}
#[tokio::test]
async fn callback_server_rejects_get_method() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (tx, _rx) = oneshot::channel();
tokio::spawn(run_callback_server(
listener,
tx,
"ABC-1234".to_string(),
TEST_GATEWAY.to_string(),
));
let mut stream = tokio::net::TcpStream::connect(addr).await.unwrap();
let request = format!(
"GET /callback?token=jwt&code=ABC-1234 HTTP/1.1\r\n\
Host: 127.0.0.1\r\n\
Origin: {TEST_GATEWAY}\r\n\r\n"
);
stream.write_all(request.as_bytes()).await.unwrap();
let mut buf = vec![0u8; 4096];
let n = stream.read(&mut buf).await.unwrap();
let response = String::from_utf8_lossy(&buf[..n]);
assert!(
response.contains("405 Method Not Allowed"),
"GET should return 405:\n{response}"
);
}
}