Metadata-Version: 2.4
Name: devguard-python-guard
Version: 1.0.1
Summary: DevGuard zero-dependency anti-theft, domain authorization, and milestone autolock guard for FastAPI, Django, and Flask.
Home-page: https://devguard.uk
Author: DevGuard Security Team
Author-email: DevGuard Security Team <security@devguard.uk>
License: MIT
Project-URL: Homepage, https://devguard.uk
Project-URL: Documentation, https://devguard.uk/docs
Project-URL: Repository, https://github.com/DevGuard-uk/devguard-python-guard
Keywords: devguard,security,anti-theft,middleware,fastapi,django,flask,asgi,wsgi
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Framework :: FastAPI
Classifier: Framework :: Django
Classifier: Framework :: Flask
Classifier: Topic :: Security
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI :: Middleware
Requires-Python: >=3.7
Description-Content-Type: text/markdown
Dynamic: author
Dynamic: home-page
Dynamic: requires-python

# DevGuard Python Guard (`devguard-python-guard`)

Official zero-dependency Python guard for **DevGuard** anti-theft protection, domain allowlisting, invoice milestone autolock enforcement, and SEO-safe HTTP 503 lockouts.

Supports **FastAPI**, **Starlette** (ASGI), **Django**, and **Flask** (WSGI).

---

## Key Features

- **Zero External Dependencies**: Operates exclusively using the Python Standard Library (`urllib`, `json`, `re`, `time`, `threading`).
- **Fail-Safe Open Policy**: Network timeouts, DNS issues, or unreachable APIs will **never** interrupt client application traffic.
- **Domain Authorization**: Enforces authorized hosting domains with support for wildcard subdomains (`*.example.com`).
- **Milestone Autolock**: Automatically queries project status from the DevGuard API with local in-memory TTL caching.
- **SEO-Safe HTTP 503**: Emits HTTP status 503 with `Retry-After: 3600` so search crawlers do not drop site ranking during payment disputes.

---

## Installation

```bash
pip install devguard-python-guard
```

---

## Usage

### 1. FastAPI / Starlette (ASGI)

```python
from fastapi import FastAPI
from devguard_python_guard import DevGuardASGIMiddleware

app = FastAPI()

app.add_middleware(
    DevGuardASGIMiddleware,
    project_id="proj_your_project_id",
    allowed_domains=["example.com", "*.example.com"],
    fail_safe=True,  # Default: True (fail-safe open)
    cache_ttl=300,   # 5-minute status cache
)

@app.get("/")
def home():
    return {"message": "Hello World"}
```

### 2. Django (WSGI)

In `myproject/wsgi.py`:

```python
import os
from django.core.wsgi import get_wsgi_application
from devguard_python_guard import DevGuardWSGIMiddleware

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

application = get_wsgi_application()
application = DevGuardWSGIMiddleware(
    application,
    project_id=os.getenv("DEVGUARD_PROJECT_ID"),
    allowed_domains=["example.com", "*.example.com"],
    fail_safe=True,
)
```

### 3. Flask (WSGI)

```python
from flask import Flask
from devguard_python_guard import DevGuardWSGIMiddleware

app = Flask(__name__)
app.wsgi_app = DevGuardWSGIMiddleware(
    app.wsgi_app,
    project_id="proj_your_project_id",
    allowed_domains=["example.com", "*.example.com"],
    fail_safe=True,
)

@app.route("/")
def index():
    return "Hello Flask"
```

---

## Environment Variables

You can configure DevGuard via environment variables or explicitly in code:

| Variable | Description | Default |
|---|---|---|
| `DEVGUARD_PROJECT_ID` | Project ID from DevGuard dashboard | `""` |
| `DEVGUARD_API_URL` | DevGuard API endpoint | `https://api.devguard.uk` |
| `DEVGUARD_SDK_KEY` | Preferred project SDK key (Portal → Project SDK key) | `None` |
| `DEVGUARD_API_KEY` | Legacy API / Master Secret fallback when SDK key unset | `None` |
| `DEVGUARD_ALLOWED_DOMAINS` | Comma-separated allowed domains | `None` |
| `DEVGUARD_FAIL_SAFE` | `open` (true) or `closed` (false) | `open` |
| `DEVGUARD_CACHE_TTL` | Cache duration in seconds | `300` |
| `DEVGUARD_TRUST_PROXY` | Trust `X-Forwarded-Host` | `false` |
