Skip to main content

x-lock bot protection middleware for Python — invisible alternative to reCAPTCHA

Project description

xlock-py

Flow-aware automation protection for Python. Protect auth, checkout, and API routes while keeping flow structure in code.

Supports FastAPI, Django, and Flask.

Install

# Core (Django — no extra deps)
pip install xlock-py

# FastAPI / Starlette
pip install xlock-py[fastapi]

# Flask
pip install xlock-py[flask]

# Everything
pip install xlock-py[all]

Quick Start

FastAPI / Starlette

from fastapi import FastAPI
from xlock import XLockMiddleware

app = FastAPI()
app.add_middleware(
    XLockMiddleware,
    site_key="sk_...",
    protected_paths=["/api/auth", "/api/checkout"],
    flows=[
        {
            "slug": "login",
            "match": [{"method": "POST", "pathPattern": "/api/auth/login"}],
        },
        {
            "slug": "checkout",
            "match": [{"method": "POST", "pathPattern": "/api/checkout"}],
        },
    ],
)

Django

# settings.py
MIDDLEWARE = [
    "xlock.XLockDjangoMiddleware",
    # ... other middleware
]

XLOCK_SITE_KEY = "sk_..."
XLOCK_PROTECTED_PATHS = ["/api/auth/", "/api/checkout/"]
XLOCK_FLOWS = [
    {
        "slug": "login",
        "match": [{"method": "POST", "pathPattern": "/api/auth/login"}],
    }
]

Flask

from flask import Flask
from xlock import XLockFlask

app = Flask(__name__)
xlock = XLockFlask(
    app,
    site_key="sk_...",
    protected_paths=["/api/auth"],
    flows=[
        {
            "slug": "login",
            "match": [{"method": "POST", "pathPattern": "/api/auth/login"}],
        }
    ],
)

Direct Verification

from xlock import verify

result = verify(
    token="v3.abc123...",
    site_key="sk_...",
    method="POST",
    path="/api/login",
    domain="app.example.com",
    flows=[
        {
            "slug": "login",
            "match": [{"method": "POST", "pathPattern": "/api/login"}],
        }
    ],
)
if result.blocked:
    print(f"Blocked: {result.reason}")
else:
    print(result.action, result.resolved_flow)

Result Contract

VerifyResult includes:

  • action: allow, log, pow, challenge, deny, delay
  • reason
  • score
  • signals
  • flow_hint
  • resolved_flow
  • delay_ms
  • challenge_mode
  • policy_id

Configuration

Option Env Var Default Description
site_key XLOCK_SITE_KEY Your x-lock site key
api_url XLOCK_API_URL https://api.x-lock.dev Runtime API endpoint
fail_open XLOCK_FAIL_OPEN True Allow requests on API errors
protected_paths XLOCK_PROTECTED_PATHS [] Path prefixes to protect
flows / XLOCK_FLOWS [] SDK-canonical flow declarations used for local flowHint resolution

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xlock_py-0.1.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xlock_py-0.1.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file xlock_py-0.1.1.tar.gz.

File metadata

  • Download URL: xlock_py-0.1.1.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for xlock_py-0.1.1.tar.gz
Algorithm Hash digest
SHA256 380a4c47015dfae3d3c82d34f8273312fefd117e66761ae16469221427003c20
MD5 033ca54a29c0242609d3163c4524538f
BLAKE2b-256 fca405e56e23e3e3b927b004776b573faf24d998206798854269d86f9ec60f2c

See more details on using hashes here.

File details

Details for the file xlock_py-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: xlock_py-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for xlock_py-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7d944ee809d2a6d45a842f47f2c220e5f6ee7ba9830bf427463977e507bd375c
MD5 faec1858d4aff338823e9380ac1e6a49
BLAKE2b-256 f2b09d4c043a982e9cddfaabf0e442ddd443403f972ffb72252f4979b8b1a0a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page