loginflow 0.1.1

Browser-driven login discovery, form drive, MFA, and session capture into authjar
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
//! Loginflow drives a target from URL + credentials to a captured session in authjar.
//!
//! v0.1 substrate: HTML form discovery, browser fill/submit via runtime-headless,
//! TOTP MFA, HTTP fast-path compatible with scald's `LoginFlow`, and canary verification.
//!
//! ## Safe Defaults
//! - **Input size**: HTTP bodies are bounded to 1MB by default.
//! - **Recursion depth**: DOM tree recursion is bounded to 128 levels.
//! - **Outbound network**: Loopback and private IP space are allowed for dev targets, but can be restricted via stealth profiles.
//! - **Process spawning**: Browser launches use runtime-headless's canonical stealth launch defaults and remain overrideable.
//! - **Filesystem writes**: Output is written strictly to the configured authjar store; no other filesystem access occurs.

#![forbid(unsafe_code)]
#![cfg_attr(
    not(test),
    deny(
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::todo,
        clippy::unimplemented,
        clippy::panic
    )
)]
#![allow(
    clippy::module_name_repetitions,
    clippy::must_use_candidate,
    clippy::missing_errors_doc
)]

pub mod capture;
pub mod discover;
pub mod drive;
pub mod error;
pub mod http_fastpath;
mod http_identity;
pub mod mfa;
pub mod verify;

pub use capture::{capture_from_auth_session, CapturedSession, ScaldAuth};
pub use discover::{
    discover_best_login_form, discover_best_oauth_entry, discover_login_forms_in_html,
    discover_oauth_entry_points, DiscoveredForm, DiscoveredOAuth, FormMethod,
};
#[cfg(feature = "browser")]
pub use drive::webauthn::{respond_to_webauthn_challenge, VirtualAuthenticatorConfig};
pub use drive::{
    follow_oauth_redirect_chain, oauth_redirect_client, CaptchaChallenge, CaptchaError,
    CaptchaKind, CaptchaSolution, CaptchaSolver, OAuthChainResult, RedirectHop, StubCaptchaSolver,
};
#[cfg(all(feature = "browser", feature = "captchaforge"))]
pub use drive::{solve_via_captchaforge, CaptchaForgeSolver};
pub use error::{
    BuildError, CaptureError, DiscoverError, DriveError, HttpLoginError, LoginFlowError, MfaError,
    VerifyError,
};
pub use http_fastpath::{eligible_for_http_fastpath, perform_http_login, ScaldLoginFlow};
pub use mfa::{totp_code_at, MfaPrompt, MfaResponse, MfaSource, TotpMfaSource, TotpSecret};
pub use verify::{verify_canary, CanaryConfig};

#[cfg(feature = "stealth")]
pub use guise::ProfileBundle;

use authjar::SessionStore;
use http_identity::HttpIdentity;
use secrecy::SecretString;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use url::Url;

#[cfg(feature = "browser")]
use runtime_headless::{BrowserLaunchOptions, BrowserRuntime, Page};

/// Credentials for a single login attempt.
#[derive(Clone)]
pub struct Credentials {
    /// Username or email.
    pub username: String,
    /// Password (never logged).
    pub password: SecretString,
    /// Optional MFA provider (TOTP, etc.).
    pub mfa_source: Option<Arc<dyn MfaSource>>,
}

/// Configured login driver (opaque builder output).
pub struct LoginFlow {
    insecure: bool,
    session_name: String,
    prefer_http: bool,
    force_browser: bool,
    http_timeout: Duration,
    canary: Option<CanaryConfig>,
    auth_store: Option<Arc<Mutex<SessionStore>>>,
    #[allow(dead_code)]
    captcha_solver: Option<Arc<dyn CaptchaSolver>>,
    #[cfg(feature = "stealth")]
    stealth_profile: Option<ProfileBundle>,
    #[cfg(feature = "browser")]
    browser: Option<Arc<BrowserRuntime>>,
    #[cfg(feature = "browser")]
    launch_options: BrowserLaunchOptions,
}

/// Builder for [`LoginFlow`].
pub struct LoginFlowBuilder {
    insecure: bool,
    session_name: String,
    prefer_http: bool,
    force_browser: bool,
    http_timeout: Duration,
    canary: Option<CanaryConfig>,
    auth_store: Option<Arc<Mutex<SessionStore>>>,
    captcha_solver: Option<Arc<dyn CaptchaSolver>>,
    #[cfg(feature = "stealth")]
    stealth_profile: Option<ProfileBundle>,
    #[cfg(feature = "browser")]
    browser: Option<Arc<BrowserRuntime>>,
    #[cfg(feature = "browser")]
    launch_options: BrowserLaunchOptions,
}

impl LoginFlow {
    /// Start a builder with default options.
    #[must_use]
    pub fn builder() -> LoginFlowBuilder {
        LoginFlowBuilder::default()
    }

    fn http_identity(&self) -> HttpIdentity {
        #[cfg(feature = "stealth")]
        {
            self.stealth_profile
                .map(HttpIdentity::Stealth)
                .unwrap_or_default()
        }
        #[cfg(not(feature = "stealth"))]
        {
            HttpIdentity::None
        }
    }

    /// Discover a login form from the current page HTML (browser feature).
    ///
    /// # Errors
    ///
    /// Returns [`DiscoverError`] when HTML cannot be fetched or parsed.
    #[cfg(feature = "browser")]
    pub async fn discover_form(
        &self,
        page: &Page,
    ) -> Result<Option<DiscoveredForm>, DiscoverError> {
        let html =
            runtime_headless::evaluate_script_value(page, "document.documentElement.outerHTML")
                .await
                .map_err(|e| DiscoverError::Parse(e.to_string()))?;
        let page_url = page
            .url()
            .await
            .map_err(|e| DiscoverError::Parse(e.to_string()))?
            .ok_or(DiscoverError::Parse("page has no url".into()))?;
        let url = Url::parse(&page_url).map_err(|e| DiscoverError::Parse(e.to_string()))?;
        discover_best_login_form(&html, &url)
    }

    /// Drive a previously discovered form in an open page.
    ///
    /// # Errors
    ///
    /// Returns [`DriveError`] when fill/submit fails.
    #[cfg(feature = "browser")]
    pub async fn drive_form(
        &self,
        page: &Page,
        form: &DiscoveredForm,
        credentials: &Credentials,
    ) -> Result<(), DriveError> {
        let page_url = page
            .url()
            .await
            .map_err(|e| DriveError::Headless(e.to_string()))?
            .unwrap_or_default();
        drive::drive_html_form(
            page,
            &page_url,
            form,
            &credentials.username,
            &credentials.password,
            credentials.mfa_source.as_ref(),
        )
        .await
    }

    /// End-to-end login: discover → HTTP or browser → capture → optional canary.
    ///
    /// # Errors
    ///
    /// Returns [`LoginFlowError`] on discovery, drive, capture, or verification failure.
    pub async fn login(
        &self,
        target_url: &Url,
        credentials: &Credentials,
    ) -> Result<CapturedSession, LoginFlowError> {
        if target_url.host_str().is_none() {
            return Err(LoginFlowError::MissingHost);
        }
        let html = self.fetch_page_html(target_url).await?;
        let form = discover_best_login_form(&html, target_url)?.ok_or_else(|| {
            LoginFlowError::NoLoginForm {
                url: target_url.to_string(),
            }
        })?;

        if form.has_totp_field && credentials.mfa_source.is_none() {
            return Err(LoginFlowError::MfaRequired);
        }

        let use_http = eligible_for_http_fastpath(&form, self.force_browser)
            && (self.prefer_http || !self.force_browser);

        let session_name = self.session_name.clone();
        let captured = if use_http {
            let mfa_code = if form.has_totp_field {
                let source = credentials
                    .mfa_source
                    .as_ref()
                    .ok_or(LoginFlowError::MfaRequired)?;
                let resp = source.fetch(&MfaPrompt::Totp { field_name: None }).await?;
                Some(resp.code)
            } else {
                None
            };
            let flow = ScaldLoginFlow::from_discovered(
                &form,
                &credentials.username,
                &credentials.password,
                mfa_code.as_deref(),
            );
            let mut captured = http_fastpath::perform_http_login_with_identity(
                &flow,
                &[],
                target_url,
                self.http_timeout,
                self.http_identity(),
                self.insecure,
            )
            .await
            .map_err(LoginFlowError::Http)?;
            captured.auth_session.name = session_name;
            captured
        } else {
            #[cfg(feature = "browser")]
            {
                self.login_via_browser(target_url, &form, credentials)
                    .await?
            }
            #[cfg(not(feature = "browser"))]
            {
                return Err(LoginFlowError::Build(BuildError::BrowserUnavailable));
            }
        };

        if let Some(ref canary) = self.canary {
            verify::verify_canary_with_identity(
                &captured.scald_auth,
                canary,
                self.http_identity(),
                self.insecure,
            )
            .await
            .map_err(LoginFlowError::Verify)?;
        }

        if let Some(store) = &self.auth_store {
            captured.persist_shared(store);
        }

        Ok(captured)
    }

    #[cfg(feature = "browser")]
    async fn login_via_browser(
        &self,
        target_url: &Url,
        form: &DiscoveredForm,
        credentials: &Credentials,
    ) -> Result<CapturedSession, LoginFlowError> {
        let runtime = match &self.browser {
            Some(rt) => Arc::clone(rt),
            None => Arc::new(
                BrowserRuntime::launch(&self.launch_options)
                    .await
                    .map_err(|e| LoginFlowError::Drive(DriveError::Headless(e.to_string())))?,
            ),
        };

        #[cfg(feature = "stealth")]
        let page = if let Some(bundle) = self.stealth_profile {
            guise::cdp::open_profiled_page(runtime.browser(), target_url.as_str(), bundle.browser)
                .await
                .map_err(|e| LoginFlowError::Drive(DriveError::Headless(e.to_string())))?
        } else {
            runtime
                .browser()
                .new_page(target_url.as_str())
                .await
                .map_err(|e| LoginFlowError::Drive(DriveError::Headless(e.to_string())))?
        };

        #[cfg(not(feature = "stealth"))]
        let page = runtime
            .browser()
            .new_page(target_url.as_str())
            .await
            .map_err(|e| LoginFlowError::Drive(DriveError::Headless(e.to_string())))?;

        self.drive_form(&page, form, credentials)
            .await
            .map_err(LoginFlowError::Drive)?;

        let document_cookie = runtime_headless::evaluate_script_value(&page, "document.cookie")
            .await
            .map_err(|e| LoginFlowError::Drive(DriveError::Headless(e.to_string())))?;

        let domain = target_url.host_str().ok_or(LoginFlowError::MissingHost)?;
        capture::capture_from_document_cookies(&self.session_name, domain, &document_cookie)
            .map_err(LoginFlowError::Capture)
    }
}

impl LoginFlowBuilder {
    /// Session name used in authjar (default `"loginflow"`).
    #[must_use]
    pub fn session_name(mut self, name: impl Into<String>) -> Self {
        self.session_name = name.into();
        self
    }

    /// Prefer HTTP fast-path when the discovered form is simple.
    #[must_use]
    pub fn prefer_http(mut self, prefer: bool) -> Self {
        self.prefer_http = prefer;
        self
    }

    /// Force browser drive even for simple forms.
    #[must_use]
    pub fn force_browser(mut self, force: bool) -> Self {
        self.force_browser = force;
        self
    }

    /// HTTP login / fetch timeout.
    #[must_use]
    pub fn http_timeout(mut self, timeout: Duration) -> Self {
        self.http_timeout = timeout;
        self
    }

    /// Disable TLS certificate verification for all outbound HTTP in this flow.
    ///
    /// This is off by default; enabling it logs a loud warning when credentials
    /// are submitted. Use only for testing against self-signed endpoints.
    #[must_use]
    pub fn insecure(mut self, insecure: bool) -> Self {
        self.insecure = insecure;
        self
    }

    /// Post-login canary GET configuration.
    #[must_use]
    pub fn with_canary(mut self, canary: CanaryConfig) -> Self {
        self.canary = Some(canary);
        self
    }

    /// Persist captured sessions into authjar (`Arc<Mutex<SessionStore>>` for shared stores).
    #[must_use]
    pub fn with_authjar(mut self, store: Arc<Mutex<SessionStore>>) -> Self {
        self.auth_store = Some(store);
        self
    }

    /// Captcha gate solver (stub or captchaforge-backed).
    #[must_use]
    pub fn with_captcha_solver(mut self, solver: Arc<dyn CaptchaSolver>) -> Self {
        self.captcha_solver = Some(solver);
        self
    }

    /// Browser + TLS fingerprint bundle for HTTP fetch and browser drive.
    #[cfg(feature = "stealth")]
    #[must_use]
    pub fn with_stealth_profile(mut self, profile: ProfileBundle) -> Self {
        self.stealth_profile = Some(profile);
        self
    }

    /// Reuse an existing headless browser runtime.
    #[cfg(feature = "browser")]
    #[must_use]
    pub fn with_browser(mut self, browser: Arc<BrowserRuntime>) -> Self {
        self.browser = Some(browser);
        self
    }

    /// Launch options when no shared browser is supplied.
    #[cfg(feature = "browser")]
    #[must_use]
    pub fn with_launch_options(mut self, options: BrowserLaunchOptions) -> Self {
        self.launch_options = options;
        self
    }

    /// Build the [`LoginFlow`] driver.
    ///
    /// # Errors
    ///
    /// Currently always succeeds; reserved for future validation.
    pub fn build(self) -> Result<LoginFlow, BuildError> {
        Ok(LoginFlow {
            insecure: self.insecure,
            session_name: if self.session_name.is_empty() {
                "loginflow".to_string()
            } else {
                self.session_name
            },
            prefer_http: self.prefer_http,
            force_browser: self.force_browser,
            http_timeout: self.http_timeout,
            canary: self.canary,
            auth_store: self.auth_store,
            captcha_solver: self.captcha_solver,
            #[cfg(feature = "stealth")]
            stealth_profile: self.stealth_profile,
            #[cfg(feature = "browser")]
            browser: self.browser,
            #[cfg(feature = "browser")]
            launch_options: self.launch_options,
        })
    }
}

impl Default for LoginFlowBuilder {
    fn default() -> Self {
        Self {
            insecure: false,
            session_name: "loginflow".to_string(),
            prefer_http: true,
            force_browser: false,
            http_timeout: Duration::from_secs(30),
            canary: None,
            auth_store: None,
            captcha_solver: None,
            #[cfg(feature = "stealth")]
            stealth_profile: None,
            #[cfg(feature = "browser")]
            browser: None,
            #[cfg(feature = "browser")]
            launch_options: BrowserLaunchOptions::default_stealth(),
        }
    }
}

impl LoginFlow {
    async fn fetch_page_html(&self, url: &Url) -> Result<String, LoginFlowError> {
        let client_builder = reqwest::Client::builder()
            .timeout(self.http_timeout)
            .redirect(reqwest::redirect::Policy::limited(5))
            .danger_accept_invalid_certs(self.insecure);

        let client_builder =
            http_identity::apply_to_client_builder(client_builder, self.http_identity())
                .map_err(|e| LoginFlowError::Http(HttpLoginError::Transport(e)))?;

        let client = client_builder
            .build()
            .map_err(|e| LoginFlowError::Http(HttpLoginError::Transport(e.to_string())))?;

        let response = client
            .get(url.clone())
            .send()
            .await
            .map_err(|e| LoginFlowError::Http(HttpLoginError::Transport(e.to_string())))?;

        response
            .text()
            .await
            .map_err(|e| LoginFlowError::Http(HttpLoginError::Transport(e.to_string())))
    }
}

#[cfg(all(test, feature = "browser"))]
mod tests {
    use super::*;

    #[test]
    fn builder_default_launch_options_delegate_runtime_stealth_profile() {
        let flow = LoginFlowBuilder::default()
            .build()
            .expect("default loginflow builder should build");
        let expected = BrowserLaunchOptions::default_stealth();

        assert_eq!(flow.launch_options.window_width, expected.window_width);
        assert_eq!(flow.launch_options.window_height, expected.window_height);
        assert_eq!(flow.launch_options.no_sandbox, expected.no_sandbox);
        assert_eq!(
            flow.launch_options.disable_default_args,
            expected.disable_default_args
        );
        assert_eq!(flow.launch_options.headed, expected.headed);
        assert_eq!(
            flow.launch_options.request_timeout,
            expected.request_timeout
        );
        assert_eq!(
            flow.launch_options.chrome_executable,
            expected.chrome_executable
        );
        assert_eq!(
            flow.launch_options.new_headless_mode,
            expected.new_headless_mode
        );
        assert_eq!(flow.launch_options.extra_args, expected.extra_args);
        assert_eq!(flow.launch_options.user_data_dir, expected.user_data_dir);
    }
}

#[cfg(all(test, feature = "stealth"))]
mod stealth_http_tests {
    use super::*;
    use guise::{ProfileBundle, StealthProfile};
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::net::TcpListener;

    #[tokio::test]
    async fn fetch_page_html_sends_profile_navigation_headers() {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("listener should bind");
        let addr = listener.local_addr().expect("listener should have address");

        let server = tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.expect("server should accept");
            let mut buf = [0_u8; 4096];
            let n = socket.read(&mut buf).await.expect("server should read");
            let request = String::from_utf8_lossy(&buf[..n]).to_string();
            socket
                .write_all(
                    b"HTTP/1.1 200 OK\r\nContent-Length: 26\r\n\r\n<html><form></form></html>",
                )
                .await
                .expect("server should respond");
            request
        });

        let flow = LoginFlow::builder()
            .with_stealth_profile(ProfileBundle::for_browser(StealthProfile::FirefoxWindows))
            .build()
            .expect("flow should build");
        let html = flow
            .fetch_page_html(&Url::parse(&format!("http://{addr}/login")).expect("valid url"))
            .await
            .expect("page fetch should succeed");
        let request = server.await.expect("server task should finish");
        let request_lower = request.to_ascii_lowercase();
        let facts = guise::fingerprint::profile_facts(StealthProfile::FirefoxWindows);

        assert_eq!(html, "<html><form></form></html>");
        assert!(request_lower.contains("user-agent: mozilla/5.0"));
        assert!(
            request_lower.contains(&facts.user_agent.to_ascii_lowercase()),
            "loginflow HTTP stealth UA drifted from shared profile: {request}"
        );
        assert!(
            request_lower.contains(&format!("accept: {}", facts.accept).to_ascii_lowercase()),
            "loginflow HTTP stealth Accept header drifted from shared profile: {request}"
        );
        assert!(
            request_lower.contains(
                &format!("accept-language: {}", facts.accept_language).to_ascii_lowercase()
            ),
            "loginflow HTTP stealth Accept-Language header drifted from shared profile: {request}"
        );
        assert!(
            !request_lower.contains("accept-encoding:"),
            "loginflow reqwest build does not enable transparent decompression, so stealth HTTP defaults must not advertise compression: {request}"
        );
    }
}