Production-grade authentication library: Rust crypto + Python workflows for FastAPI
Project description
Irene Auth - Production-Grade Authentication Library
Irene Auth is a production-grade authentication library combining Rust-powered cryptography with Python workflows for FastAPI applications.
๐ฏ All 8 Core Features Implemented
| # | Feature | Status | Description |
|---|---|---|---|
| 1 | User Model | โ | SQLAlchemy models with relationships |
| 2 | Password Hashing | โ | Argon2id (Rust, memory-hard, GPU-resistant) |
| 3 | Signup + Login | โ | Complete authentication flows |
| 4 | JWT Access Tokens | โ | HS256 + RS256 support (Rust) |
| 5 | Refresh Tokens | โ | Storage, validation, revocation |
| 6 | RBAC | โ | Role-based access control |
| 7 | Password Reset | โ | Secure token-based reset flow |
| 8 | Email Verification | โ | Token-based email verification |
๐๏ธ Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ FastAPI Application (Your Code) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Python Auth Library (irene_auth) โ
โ โข Models, workflows, RBAC โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Rust Crypto Module (Secure) โ
โ โข Argon2id, JWT, tokens โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Rust handles ALL cryptography:
- โ Argon2id password hashing
- โ CSPRNG token generation
- โ SHA-256 token hashing
- โ Constant-time comparisons
- โ JWT encoding/decoding
Python handles workflows:
- โ Database operations
- โ Business logic
- โ FastAPI integration
- โ Email sending
๐ Quick Start
Installation
pip install irene-auth
Basic Usage
from fastapi import FastAPI, Depends
from irene_auth import AuthManager, AuthConfig, User
app = FastAPI()
# Configure authentication
config = AuthConfig(
database_url="sqlite:///./auth.db",
secret_key="your-secret-key-min-32-characters-long"
)
auth = AuthManager(config)
# Signup endpoint
@app.post("/signup")
async def signup(email: str, password: str):
user, verification_token = auth.signup(email, password)
# TODO: Send verification email with token
return {"message": "Check your email for verification"}
# Login endpoint
@app.post("/login")
async def login(email: str, password: str):
tokens = auth.login(email, password)
return tokens # {access_token, refresh_token, token_type}
# Protected endpoint (requires authentication)
@app.get("/profile")
async def get_profile(user: User = Depends(auth.get_current_user)):
return {
"email": user.email,
"is_verified": user.is_verified,
"roles": [role.name for role in user.roles]
}
# Admin-only endpoint (requires role)
@app.get("/admin")
async def admin_panel(user: User = Depends(auth.require_role("admin"))):
return {"message": "Welcome, admin!"}
๐ Complete Example
See examples/fastapi_app.py for a complete working example with all 8 features.
๐ Security Features
Password Security (Feature 2)
- Argon2id hashing (memory-hard, 64 MiB, 3 iterations)
- GPU/ASIC resistant
- Automatic salt generation
JWT Tokens (Feature 4)
- HS256 (HMAC-SHA256) symmetric signing
- RS256 (RSA-SHA256) asymmetric signing
- Automatic expiration validation
- Custom claims support
Token Security (Features 5, 7, 8)
- CSPRNG token generation (OS-provided)
- SHA-256 hashing for database storage
- Constant-time comparison (prevents timing attacks)
- Time-limited tokens with expiration
Database Security
- Never stores plaintext passwords
- Never stores plaintext tokens
- Constant-time token verification
- SQL injection protection (SQLAlchemy ORM)
๐ฏ Feature Details
1. User Model
from irene_auth.models import User, Role, Base
# SQLAlchemy models with relationships
user = User(email="user@example.com", password_hash="...")
user.roles # List of roles
user.refresh_tokens # List of active tokens
2 & 3. Password Hashing + Signup/Login
# Signup (Feature 3)
user, token = auth.signup("user@example.com", "SecurePass123!")
# Login (Feature 3)
tokens = auth.login("user@example.com", "SecurePass123!")
# Returns: {access_token, refresh_token, token_type}
4. JWT Access Tokens
# Automatic with login, or manual:
access_token = auth.jwt_manager.create_access_token(user)
# Verify and decode
claims = auth.jwt_manager.decode_access_token(access_token)
5. Refresh Tokens
# Create refresh token
refresh_token = auth.refresh_manager.create_refresh_token(user, db)
# Use to get new access token
new_access_token = auth.refresh_access_token(refresh_token, db)
# Revoke token
auth.refresh_manager.revoke_token(refresh_token, db)
6. Role-Based Access Control
# Create roles
auth.rbac_manager.create_role("admin", "Administrator", db)
auth.rbac_manager.create_role("moderator", "Moderator", db)
# Assign to user
auth.rbac_manager.assign_role(user, "admin", db)
# Check permissions
if user.has_role("admin"):
print("User is admin")
# Protect endpoints
@app.get("/admin")
async def admin_only(user: User = Depends(auth.require_role("admin"))):
return {"message": "Admin access"}
@app.get("/moderate")
async def moderate(user: User = Depends(auth.require_any_role(["admin", "moderator"]))):
return {"message": "Can moderate"}
7. Password Reset
# User requests reset
token = auth.reset_manager.request_password_reset("user@example.com", db)
# TODO: Send email with token
# User submits new password with token
success = auth.reset_manager.reset_password(token, "NewPassword123!", db)
8. Email Verification
# On signup, token is generated
user, verification_token = auth.signup("user@example.com", "pass")
# TODO: Send verification email
# User clicks link with token
success = auth.verification_manager.verify_email(verification_token, db)
# Resend verification
new_token = auth.verification_manager.resend_verification("user@example.com", db)
โ๏ธ Configuration
from irene_auth import AuthConfig
config = AuthConfig(
database_url="postgresql://user:pass@localhost/db",
secret_key="your-secret-key-at-least-32-characters",
algorithm="HS256", # or "RS256"
access_token_expire_minutes=15,
refresh_token_expire_days=30,
verification_token_expire_hours=24,
reset_token_expire_hours=1,
# For RS256:
private_key_path="/path/to/private.pem",
public_key_path="/path/to/public.pem"
)
๐ Security Best Practices
DO โ
- Use strong secret keys (32+ characters, random)
- Use HTTPS in production
- Validate email addresses before signup
- Send verification emails to confirm ownership
- Implement rate limiting on auth endpoints
- Log authentication events for auditing
- Rotate refresh tokens on use (optional)
- Revoke tokens on logout
- Use short-lived access tokens (15-30 minutes)
- Store refresh tokens securely on client
DON'T โ
- DON'T store passwords in plaintext
- DON'T store tokens in plaintext (database)
- DON'T send tokens in URLs (use headers/body)
- DON'T ignore verification (email enumeration risk)
- DON'T use weak secret keys
- DON'T skip HTTPS in production
- DON'T expose detailed error messages
- DON'T allow unlimited login attempts
๐ API Reference
Core Functions (from Rust)
import irene_auth
# Password operations
hash = irene_auth.hash_password("password")
is_valid = irene_auth.verify_password("password", hash)
# Token operations
token = irene_auth.generate_token(32)
token_hash = irene_auth.hash_token(token)
is_valid = irene_auth.verify_token_hash(token, token_hash)
# JWT operations
jwt = irene_auth.jwt_encode({"sub": "123"}, b"secret", 3600, "HS256")
claims = irene_auth.jwt_decode(jwt, b"secret", ["HS256"])
Python Classes
- AuthManager: Main authentication coordinator
- JWTManager: JWT token creation/validation
- RefreshTokenManager: Refresh token lifecycle
- PasswordResetManager: Password reset flow
- EmailVerificationManager: Email verification flow
- RBACManager: Role and permission management
๐งช Testing
# Install dev dependencies
pip install irene-auth[dev]
# Run tests
pytest
# Run with coverage
pytest --cov=irene_auth
๐ข Publishing to PyPI
# Build wheel
maturin build --release
# Publish
maturin publish
๐ License
MIT License - see LICENSE file
๐ Acknowledgments
Built with:
- Rust - Systems programming language
- PyO3 - Rust-Python bindings
- FastAPI - Modern Python web framework
- SQLAlchemy - Python SQL toolkit
- Argon2 - Password hashing
- jsonwebtoken - JWT implementation
๐ค Contributing
Contributions welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests
- Submit a pull request
Security issues: Report privately to security@example.com
๐ง Support
- Documentation: See this README and examples/
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Production-ready authentication for FastAPI | Rust-powered security | All 8 core features ๐
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file irene_auth-0.1.0.tar.gz.
File metadata
- Download URL: irene_auth-0.1.0.tar.gz
- Upload date:
- Size: 52.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8931f68c7d7254fe5d0b5c92cb99a148f6eed54cba152e01d22a3b09f6673307
|
|
| MD5 |
5e00703cdd6a535f2884f94cb7d42eba
|
|
| BLAKE2b-256 |
499ba95c0c0f14a2da29f93d1c6c03489f0e497da7dd9551801635bf2b6de273
|
File details
Details for the file irene_auth-0.1.0-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: irene_auth-0.1.0-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 613.2 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5da92bb849e027d82fe109bb53d09c62e70e8e8024664ffce6473b12e37cdace
|
|
| MD5 |
0d74a5ef829f194d80864e799b2c4bee
|
|
| BLAKE2b-256 |
eefc5b9f7f1e41b32d1e706b8ac4d9d9dbc00c51f18becd93271794896096a15
|