A small FastAPI service for username/password registration and JWT access tokens, backed by SQLite.
POST /registercreates a user. Usernames are case-insensitive (stored lowercase, unique). The password is hashed with bcrypt; plaintext is never stored or returned.POST /tokenexchanges username/password (OAuth2 password flow, form-encoded) for a signed HS256 access token that expires afterACCESS_TOKEN_EXPIRE_MINUTES(default 15). Each client IP getsLOGIN_ATTEMPTS_PER_MINUTEattempts (default 10) per sliding 60-second window, then429withRetry-After.GET /users/mereturns the current user for a validAuthorization: Bearer <token>header.
Python 3.11+, FastAPI, PyJWT, bcrypt, SQLite (stdlib sqlite3). Everything lives in main.py (about 200 lines); tests are in tests/.
python3 -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
export JWT_SECRET=$(python -c "import secrets; print(secrets.token_urlsafe(48))")
uvicorn main:app --reloadOpen http://127.0.0.1:8000/docs. The app refuses to start without a JWT_SECRET of at least 32 characters.
docker build -t authify .
docker run --rm -p 8000:8000 -e JWT_SECRET="$JWT_SECRET" -v authify-data:/data authifyThe container runs as a non-root user and keeps the SQLite file in the /data volume.
See .env.example.
| Variable | Default | Purpose |
|---|---|---|
JWT_SECRET |
none (required) | HMAC signing key |
ACCESS_TOKEN_EXPIRE_MINUTES |
15 |
Token lifetime |
AUTHIFY_DB |
authify.db |
SQLite file path |
LOGIN_ATTEMPTS_PER_MINUTE |
10 |
/token attempts per client IP per minute |
curl -X POST localhost:8000/register -H 'Content-Type: application/json' \
-d '{"username": "alice", "password": "correct-horse"}'
# 201 {"username": "alice", "created_at": "..."}
curl -X POST localhost:8000/token -d username=alice -d password=correct-horse
# 200 {"access_token": "<jwt>", "token_type": "bearer", "expires_in": 900}
curl localhost:8000/users/me -H "Authorization: Bearer <jwt>"
# 200 {"username": "alice", "created_at": "..."}Errors: 409 duplicate username (case-insensitive), 429 too many /token attempts, 422 invalid input (username 3-50 chars of A-Z a-z 0-9 _ . -, password 8+ chars and at most 72 bytes), 401 bad credentials or an invalid/expired token. Wrong password and unknown user return the same response, and both run a bcrypt check so timing does not reveal which usernames exist.
pip install -r requirements-dev.txt
ruff check . && pytestThe tests cover hashing, duplicates (including case variants), validation, token expiry claims, the /token rate limit and its sliding window, and rejection of expired, expiry-less, wrongly signed, alg=none and unknown-user tokens. CI runs them on Python 3.11 and 3.12, runs pip-audit on the pinned requirements and builds the Docker image.
- One file, stdlib SQLite. The service is small enough that splitting it into packages or adding an ORM would add indirection without benefit.
- Short-lived access tokens only. Simpler than refresh-token rotation; clients log in again after expiry.
- In-process rate limiter. A sliding window per IP needs no extra infrastructure; the trade-off is that it is per process (see limitations).
- Lowercase usernames at the edge. Normalizing in the request model and on login keeps the
PRIMARY KEYthe single uniqueness check, and concurrent duplicate inserts still end in409viaIntegrityError.
- bcrypt password hashes; passwords over 72 bytes are rejected rather than silently truncated.
- Unknown usernames are checked against a dummy bcrypt hash, so wrong-user and wrong-password responses match in body and timing.
- Tokens require
expandsub; only HS256 is accepted (soalg=nonefails). The key comes fromJWT_SECRET(32+ chars, or the app refuses to start). /tokenis rate limited per client IP.- The Docker image runs as a non-root user; dev dependencies are kept out of
requirements.txt, and all versions are pinned.
- Access tokens only: no refresh tokens, logout or revocation.
- The rate limiter lives in process memory: limits are per instance and reset on restart. Behind a reverse proxy, every request appears to come from the proxy IP unless uvicorn is started with
--proxy-headersand--forwarded-allow-ips. No per-account lockout. - SQLite with one connection per request suits a single instance, not a cluster.
- No email verification, password reset or roles.
MIT, see LICENSE.