Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:0xSteph Pentest ai Auth Session WebAuthenticator

From Leeroopedia
Knowledge Sources
Domains Authentication, Web_Security, Session_Management
Last Updated 2026-10-23 00:00 GMT

Overview

Concrete tool for performing login flows and holding authenticated web sessions, provided by the pentest-ai engine.

Description

The WebAuthenticator class performs a login flow against a target web application and returns an AuthSession holding cookies, bearer tokens, and custom headers. It supports three flow types: form_post (standard HTML login form with automatic CSRF token extraction), bearer_static (static API token injection), and bearer (POST credentials and extract token from JSON response). The resulting session is convertible to AuthCredentials so every tool wrapper that respects auth flags picks up authenticated scans for free.

Usage

Import this class when you need to log into a web target and obtain an authenticated session for credentialed scanning. Use form_post for HTML login forms, bearer_static for static API tokens, and bearer for OAuth2-style token endpoints.

Code Reference

Source Location

Signature

@dataclass
class AuthSession:
    cookies: dict[str, str] = field(default_factory=dict)
    headers: dict[str, str] = field(default_factory=dict)
    bearer_token: str = ""
    created_at: float = 0.0
    expires_at: float | None = None
    flow: str = ""
    last_login_url: str = ""
    set_cookie_raw: str = ""

class WebAuthenticator:
    def __init__(
        self,
        *,
        flow: str,
        login_url: str = "",
        username: str = "",
        password: str = "",
        username_field: str = "username",
        password_field: str = "password",
        extra_form_fields: dict[str, str] | None = None,
        success_marker: str = "",
        success_status: int | None = None,
        bearer_token: str = "",
        body_shape: str = "json",
        token_path: str = "token",
        default_ttl_seconds: int = 3600,
        timeout_seconds: float = 15.0,
        capture_hidden_fields: bool = True,
    ) -> None: ...

    async def login(self, client: httpx.AsyncClient | None = None) -> AuthSession: ...

Import

from engine.auth_session import AuthSession, WebAuthenticator, AuthError

I/O Contract

Inputs

Name Type Required Description
flow str Yes Auth flow: "form_post", "bearer_static", or "bearer"
login_url str Yes (form_post, bearer) URL to POST credentials to
username str Yes (form_post, bearer) Username for login
password str Yes (form_post, bearer) Password for login
bearer_token str Yes (bearer_static) Static bearer token
success_marker str No Substring that must appear in response body on success
success_status int No Expected HTTP status code on success (default: 2xx/3xx)
token_path str No Dotted path into JSON response to extract token (bearer flow)
capture_hidden_fields bool No Auto-extract CSRF/hidden form fields (default: True)

Outputs

Name Type Description
AuthSession dataclass Holds cookies, headers, bearer_token, expiry, flow metadata
AuthSession.to_credentials() AuthCredentials Converts to tool-wrapper-compatible auth credentials
AuthSession.is_expired bool Whether the session has expired

Usage Examples

Form Post Login

import asyncio
from engine.auth_session import WebAuthenticator

auth = WebAuthenticator(
    flow="form_post",
    login_url="http://localhost:3000/login",
    username="admin",
    password="secret123",
    success_marker="Welcome",  # optional: must appear in response body
)

session = asyncio.run(auth.login())
print(f"Cookies: {session.cookies}")
print(f"Expired: {session.is_expired}")

Bearer Token Login

import asyncio
from engine.auth_session import WebAuthenticator

auth = WebAuthenticator(
    flow="bearer",
    login_url="http://api.example.com/auth/token",
    username="user",
    password="pass",
    body_shape="json",
    token_path="access_token",  # dotted path into JSON response
)

session = asyncio.run(auth.login())
print(f"Bearer token: {session.bearer_token[:10]}...")

Static Bearer Token

from engine.auth_session import WebAuthenticator

auth = WebAuthenticator(
    flow="bearer_static",
    bearer_token="eyJhbGciOiJIUzI1NiIs...",
)
session = auth.login()  # no network call needed

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment