LifeRPG_v2.0/modern/backend/totp.py
TLimoges33 7fe4ae5365
🧙‍♂️ Transform LifeRPG into The Wizard's Grimoire - Production-Ready Application
 Major Features Added:
- Complete magical theming and rebranding from LifeRPG to The Wizard's Grimoire
- Production-grade React frontend with Tailwind CSS v4 and magical aesthetics
- Comprehensive analytics dashboard with Recharts integration (ScryingPortal)
- Push notifications system with PWA service worker support
- Drag & drop functionality using @dnd-kit for habit reordering
- Social features with friends system and leaderboards
- Performance optimization tools and monitoring
- Mobile app enhancement with PWA installation support

🏗️ Technical Infrastructure:
- Advanced service worker with offline support and background sync
- Zustand state management for scalable application state
- Production-ready UI component system with enhanced Button, Card, Input
- Progressive Web App (PWA) with manifest and app installation
- FastAPI backend with comprehensive API endpoints
- Docker containerization and CI/CD pipeline setup

📱 Progressive Web App Features:
- Offline functionality with intelligent caching
- Push notification support for habit reminders
- App installation on mobile and desktop platforms
- Background sync for offline data management
- Performance monitoring and optimization tools

🎨 User Experience:
- Magical wizard/grimoire theming throughout application
- Responsive design optimized for all device sizes
- Drag & drop habit management with smooth animations
- Interactive analytics with multiple chart types
- Social connectivity with friends and competitive features
- Comprehensive notification and performance settings

🔧 Developer Experience:
- Modern development stack with Vite and React
- Comprehensive testing setup and CI/CD pipelines
- Code quality tools with pre-commit hooks
- Docker development environment
- Detailed documentation and implementation guides

This represents a complete transformation from prototype to production-ready application with enterprise-grade features and magical user experience.
2025-08-30 17:32:42 +00:00

46 lines
1.1 KiB
Python

import os
import base64
import secrets
from typing import List, Tuple
import pyotp
from passlib.hash import bcrypt
ISSUER = os.getenv('TOTP_ISSUER', 'LifeRPG')
def generate_totp_secret() -> str:
# 32 bytes -> base32
return pyotp.random_base32()
def provisioning_uri(secret: str, email: str) -> str:
return pyotp.totp.TOTP(secret).provisioning_uri(name=email, issuer_name=ISSUER)
def verify_totp(secret: str, code: str) -> bool:
try:
totp = pyotp.TOTP(secret)
return bool(totp.verify(code, valid_window=1))
except Exception:
return False
def generate_recovery_codes(count: int = 10) -> List[str]:
return [secrets.token_urlsafe(10) for _ in range(count)]
def hash_recovery_codes(codes: List[str]) -> List[str]:
return [bcrypt.hash(c) for c in codes]
def verify_and_consume_recovery_code(stored_hashes: List[str], code: str) -> Tuple[bool, List[str]]:
remaining = []
used = False
for h in stored_hashes:
if not used and bcrypt.verify(code, h):
used = True
continue
remaining.append(h)
return used, remaining