Skip to main content

myauth

Reusable FastAPI + MongoDB authentication package. Ek baar banao, har project me pip install karke use karo.

v2 — production-hardened: rate limiting, login-lockout, refresh-token rotation + reuse-detection, RBAC, audit logging, logout-all-devices.


Installation

pip install myauth

Quick Start

from fastapi import FastAPI
from myauth import AuthConfig, auth_router, init_auth

app = FastAPI()
app.include_router(auth_router)

config = AuthConfig(
    db_url="mongodb://localhost:27017",
    jwt_secret="a-very-long-random-secret-at-least-32-characters",
    smtp_host="smtp.gmail.com",
    smtp_port=587,
    smtp_user="you@gmail.com",
    smtp_password="your-app-password",
    sender_email="noreply@yourapp.com",
)

@app.on_event("startup")
async def on_startup():
    # 'app' zaroori hai — isi se rate limiting FastAPI ke sath wire hoti hai
    await init_auth(config, app=app)

Endpoints (10)

Method Path Rate Limit (default) Kaam
POST /auth/register 3/min Naya account, verification OTP email pe jata hai
POST /auth/login 5/min Email+password → access + refresh token
POST /auth/refresh 5/min Refresh token rotate hota hai — purana turant invalid
POST /auth/logout 30/min Current session revoke
POST /auth/logout-all 30/min Har device/session ek saath revoke (Bearer token chahiye)
GET /auth/me 30/min Current logged-in user ka data (role samet)
POST /auth/forgot-password 3/min Password reset OTP bhejna
POST /auth/reset-password 5/min OTP verify karke naya password set karna
POST /auth/verify-email 5/min Register OTP se email confirm karna
POST /auth/resend-verification 3/min Naya verification OTP dobara bhejwana

Limit cross hone par 429 Too Many Requests milta hai. Interactive docs: http://localhost:8000/docs


Configuration (AuthConfig)

Core

Field Required Default
db_url
db_name myauth_db
jwt_secret ✅ (32+ chars)
access_token_expiry_minutes 15
refresh_token_expiry_days 7
smtp_host / smtp_user / smtp_password / sender_email

Security (v2)

Field Default Kaam
max_login_attempts 5 Itni galat tries ke baad account lock
lockout_duration_minutes 15 Lock kitni der rahega
password_min_length 10 + upper/lower/digit/special char zaroori
otp_resend_cooldown_seconds 60 OTP spam se bachao
strict_ip_binding False On karne par refresh-token IP change par revoke ho jata hai
rate_limit_login / rate_limit_register / rate_limit_otp_request / rate_limit_otp_verify / rate_limit_general see table above Per-endpoint limits
rate_limit_storage_uri "memory://" ⚠️ Production me "redis://host:6379" set karein — warna multi-server deployment me limit bypass ho sakti hai
require_captcha_on_register False On karke captcha_verify_url + captcha_secret dena zaroori

Secrets: .env me rakhein, os.getenv() se pass karein — package khud .env nahi padhta.


Security Features

  • Passwords/OTPs: bcrypt hashed, kabhi plaintext store nahi
  • JWT: access (role claim samet) + refresh, dono me unique jti — access/refresh ek dusre ki jagah use nahi ho sakte
  • Refresh token rotation: har /refresh par purana token turant invalid; reuse detect hone par (chori hua token dobara use ho) pura session-family revoke — attacker aur legit user dono re-login karenge
  • Login lockout: max_login_attempts ke baad 423 Locked, email-keyed (no enumeration via lockout timing)
  • RBAC: role field + require_role("admin") dependency apne routes pe use karein:
    from myauth import require_role
    @app.get("/admin/users")
    async def list_users(user: dict = Depends(require_role("admin"))):
        ...
    
  • Audit log: login/register/lockout/reuse-detection/logout-all sab audit_logs collection me (90-din TTL)
  • Email enumeration protection: forgot-password/resend-verification hamesha same generic response dete hain
  • Rate limiting: slowapi, per-endpoint config-driven limits (upar table dekhein)

Roadmap / Known Scope

  • Database: Filhal sirf MongoDB support hai (Motor). PostgreSQL support jaan-boojh kar abhi nahi banaya — isay properly karne ke liye Repository Pattern (database-agnostic abstraction layer) chahiye hoga, jo poori codebase restructure kar deta. Jab koi real project Postgres demand karega, tab v2 me isay implement karenge — us waqt requirements clearer honge.

Project Structure

myauth/
├── pyproject.toml
├── src/myauth/
│   ├── __init__.py         # init_auth(config, app), auth_router, require_role export
│   ├── config.py           # AuthConfig + validation
│   ├── db.py                # Motor connection + collections
│   ├── security.py          # hashing, JWT (role + jti claims), token-family
│   ├── otp.py                # OTP generate/verify/cooldown
│   ├── email.py               # SMTP
│   ├── password_policy.py     # complexity rules
│   ├── lockout.py              # brute-force lockout
│   ├── rate_limiting.py         # slowapi wiring
│   ├── captcha.py                # optional captcha hook
│   ├── audit.py                    # security event logging
│   ├── models.py                    # Pydantic schemas
│   ├── dependencies.py               # get_current_user, require_role
│   └── router.py                      # 10 endpoints
└── tests/

Download files

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

Source Distribution

myauth-0.3.0.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

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

myauth-0.3.0-py3-none-any.whl (24.3 kB view details)

Uploaded Python 3

File details

Details for the file myauth-0.3.0.tar.gz.

File metadata

  • Download URL: myauth-0.3.0.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for myauth-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f348f5e929f798fcd02b069a12687e33512f7970e5fcb8816d6d99bf1017a307
MD5 2453ea776ac7b9341b7bce7119e583c2
BLAKE2b-256 87547e2280341a68d86fa4bb7d3b5198f6cce90734f8f1d639b2333dade20ff8

See more details on using hashes here.

File details

Details for the file myauth-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: myauth-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 24.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for myauth-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0997519fbdde5a9d559688e077eb40aba773ceaeee22e09a45616c6cdf8c7a9c
MD5 e4dffe24f9a99777c31447132ca1361b
BLAKE2b-256 bd96f53e0d3abc6855d8cce42591f5158fb2894e92e2858b8e1167ed6630f099

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

0.2.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page