Full-stack mobile app for pole dance championship management. Backend: FastAPI + SQLAlchemy 2 (async) + SQLite (dev) / PostgreSQL (prod) - JWT auth with refresh token rotation - Championship CRUD with Instagram Graph API sync (APScheduler) - Registration flow with status management - Participant list publish with Expo push notifications - Alembic migrations, pytest test suite Mobile: React Native + Expo (TypeScript) - Auth gate: pending approval screen for new members - Championships list & detail screens - Registration form with status tracking - React Query + Zustand + React Navigation v6 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
import hashlib
|
|
import uuid
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
import jwt
|
|
from passlib.context import CryptContext
|
|
|
|
from app.config import settings
|
|
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
|
|
|
def hash_password(password: str) -> str:
|
|
return pwd_context.hash(password)
|
|
|
|
|
|
def verify_password(plain: str, hashed: str) -> bool:
|
|
return pwd_context.verify(plain, hashed)
|
|
|
|
|
|
def create_access_token(user_id: str, role: str, status: str) -> str:
|
|
expire = datetime.now(timezone.utc) + timedelta(
|
|
minutes=settings.access_token_expire_minutes
|
|
)
|
|
payload = {
|
|
"sub": user_id,
|
|
"role": role,
|
|
"status": status,
|
|
"exp": expire,
|
|
"type": "access",
|
|
}
|
|
return jwt.encode(payload, settings.secret_key, algorithm=settings.algorithm)
|
|
|
|
|
|
def create_refresh_token() -> tuple[str, str, datetime]:
|
|
"""Returns (raw_token, hashed_token, expires_at)."""
|
|
raw = str(uuid.uuid4())
|
|
hashed = hashlib.sha256(raw.encode()).hexdigest()
|
|
expires_at = datetime.now(timezone.utc) + timedelta(
|
|
days=settings.refresh_token_expire_days
|
|
)
|
|
return raw, hashed, expires_at
|
|
|
|
|
|
def decode_access_token(token: str) -> dict:
|
|
"""Raises jwt.InvalidTokenError on failure."""
|
|
payload = jwt.decode(token, settings.secret_key, algorithms=[settings.algorithm])
|
|
if payload.get("type") != "access":
|
|
raise jwt.InvalidTokenError("Not an access token")
|
|
return payload
|
|
|
|
|
|
def hash_token(raw: str) -> str:
|
|
return hashlib.sha256(raw.encode()).hexdigest()
|