LockWatch
---------

LockWatch is a drop-in API security middleware library for Python, published to PyPI under version
0.1.0. It solves the recurring problem of bolting together rate limiting, JWT management, anomaly
alerting, and audit logging across multiple FastAPI and Flask projects, collapsing what typically
requires four separate packages and several hundred lines of Redis plumbing into a single
`app.add_middleware()` call.

The motivation is practical: every production API at some point needs the same set of primitives,
rate limiting to prevent abuse, short-lived tokens that rotate safely, alerting when an IP starts
hammering a login endpoint, and a queryable record of who did what. Assembling these from scratch
or from ad-hoc PyPI packages is tedious and produces inconsistent security posture across projects.
LockWatch was built to make the correct default easy.

The primary outcome is a published library that any FastAPI or Flask application can adopt with a
two-line change, gaining all four security primitives with configurable fail-open or fail-closed
behavior on infrastructure failures.

Stack: Python 3.11, Redis (async sorted sets), python-jose RS256 JWTs, SQLAlchemy 2 async with
asyncpg, httpx for webhook dispatch, Alembic for schema migrations, hatchling for packaging.

What makes LockWatch technically interesting is that the rate limiter uses a true sliding-window
algorithm via a Redis sorted set pipeline (ZADD, ZREMRANGEBYSCORE, ZCARD, ZRANGE with WITHSCORES
to retrieve the oldest timestamp and compute when the window resets, EXPIRE) rather than the
fixed-window approach common in simpler libraries, eliminating the thundering-herd problem at
window boundaries with O(log N) overhead per request.

All four primitives (rate limiting, JWT rotation, anomaly detection, and audit logging) are
available on FastAPI via ASGI middleware; Flask support currently covers rate limiting only, with
the remaining components under development.
