web-auth-bridge
A Python library for authenticating against HTTP APIs whose login flows require a real browser — SSO, SAML, Cloudflare, or stateful web portals — and then reusing the resulting session for fast programmatic access.
Overview
web-auth-bridge separates authentication from execution:
- A Playwright-driven browser performs the login once.
- The resulting cookies and tokens are extracted into a typed
AuthResultand cached on disk. - Subsequent calls use a plain
httpxclient with those cookies, or, when a site requires a browser beyond login, a pool of pre-authenticated headless contexts.
The library is intended for CLIs, agents, and integrations that need to script against sites with complex or interactive authentication.
Installation
pip install web-auth-bridge
playwright install chromium
For sites that reject Python's default TLS fingerprint (for example, Garmin's OAuth2 token exchange):
pip install "web-auth-bridge[tls-impersonation]"
Quick start
import asyncio
from web_auth_bridge import WebAuthBridge, AuthResult, Credentials
class ExampleCallback:
async def authenticate(self, page, credentials):
await page.goto("https://example.com/login")
await page.fill('input[name="email"]', credentials.username)
await page.fill('input[name="password"]', credentials.password)
async with page.expect_navigation():
await page.click('button[type="submit"]')
return AuthResult()
async def is_authenticated(self, result):
return not result.is_expired
async def main():
bridge = WebAuthBridge(
app_name="example",
auth_callback=ExampleCallback(),
credentials=Credentials(username="me@example.com", password="..."),
)
await bridge.ensure_authenticated()
async with bridge.http_client() as client:
resp = await client.get("https://example.com/api/me")
print(resp.json())
asyncio.run(main())
On the first run the browser opens to log in. On subsequent runs the cached session is reused until it expires.
When to use it
- Sites guarded by Cloudflare or other WAFs that challenge non-browser clients.
- SAML or OAuth portals without a public token endpoint.
- Internal applications requiring MFA or interactive SSO.
- Stateful web portals (for example, ASP.NET) with no HTTP API surface.
- CLI tools for personal or private APIs where reimplementing the auth flow would cost more than the tool itself.
If a site already offers a clean API token, use httpx directly.
Alternatives
A managed browser is not always the right tool. Lighter-weight approaches exist and, when they work, cost a fraction of the memory and startup time:
cloudscraper,curl_cffi, andtls-clientimpersonate real browser TLS and HTTP/2 fingerprints to defeat fingerprint-based WAFs without running a browser.hrequestsandbotasauruswrap similar techniques with higher-level scraping APIs.- Hand-crafted HTTP flows against documented OAuth, OIDC, or token endpoints remain the best option whenever a site exposes them.
web-auth-bridge exists for the cases those approaches do not cover:
interactive SSO, MFA, SAML, stateful web portals, JavaScript-rendered
challenges, and WAF rulesets that evolve faster than any fingerprint library
can track. The tradeoff is resource cost for resilience — running an actual
browser is heavier than a spoofed request, but it behaves like a browser
because it is one, and it continues to work when the underlying site
changes.
Modes
| Mode | Credentials source | Browser visible | Typical use case |
|---|---|---|---|
| Headless + stored creds | .env / config file |
No | Fully automated CLIs and agents |
| Headed + stored creds | .env / config file |
Yes | Debugging; sites requiring a visible UI |
| Headed + manual entry | User types into page | Yes | Secrets that must not be stored on disk |
| Headless + no creds | Cached session | No | Every run after the first, while cached |
Parallel headless browsers
For sites without a usable API, the bridge can produce a pool of pre-authenticated browser contexts:
await bridge.ensure_authenticated()
async with bridge.context_pool(size=4) as pool:
pages = [await ctx.new_page() for ctx in pool]
...
Documentation
- Architecture — components, flows, and diagrams
- Design notes — decisions, alternatives, and constraints
- Examples — minimal snippets for common flows
- Testing — unit, integration, and live-site tests
- Garmin example — end-to-end Cloudflare-guarded SSO plus OAuth2 Bearer token flow
Development
git clone https://github.com/mikejhill/web-auth-bridge
cd web-auth-bridge
uv sync --all-extras
uv run playwright install chromium
uv run poe test
uv run poe cov
uv run poe lint
Contributions are welcome. Please use Conventional Commits; the release workflow derives versions from commit messages.
License
MIT
Metadata
Release files for web-auth-bridge 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| web_auth_bridge-0.1.1.tar.gz | 25.7 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| web_auth_bridge-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.7 MB
Release files / web_auth_bridge-0.1.1.tar.gz
| Download URL | web_auth_bridge-0.1.1.tar.gz |
|---|---|
| Size | 25.7 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bc6be36f25ffeb858cd0ca50bf0b4f32701779c9c94db92b097e32811885f9ee
|
|
BLAKE2b-256 checksum How to use checksums |
04b3036f0fc4a8212c63ad02d348caaf7c0c2bdd4d5b13f87abf08c1ac2b2748
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 17, 2026.
Transparency logRelease files / web_auth_bridge-0.1.1-py3-none-any.whl
| Download URL | web_auth_bridge-0.1.1-py3-none-any.whl |
|---|---|
| Size | 26.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
de6f9cdfb0ab64c3f88dbb17cced913840ba4d5c90e5690ca2887426028ce02f
|
|
BLAKE2b-256 checksum How to use checksums |
8e1767f36fdadbeed30b02b0707058bc9c46cdfbce91087a1f5da0eecaea63ca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 17, 2026.
Transparency log