File size: 10,918 Bytes
1b4b6d2 fa156f3 1b4b6d2 7516417 1b4b6d2 2685ca0 1b4b6d2 2685ca0 1b4b6d2 361e46e 1b4b6d2 361e46e 1b4b6d2 2685ca0 361e46e 1b4b6d2 51a8001 1b4b6d2 fa156f3 9dfa4be 1b4b6d2 2685ca0 b8df65e 1b4b6d2 369b6c9 1b4b6d2 369b6c9 1b4b6d2 369b6c9 fa156f3 361e46e 369b6c9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | from fastapi import APIRouter, HTTPException, Depends, Header, Request
from pydantic import BaseModel, EmailStr
import hmac
import hashlib
import time
import os
from typing import Optional
from utils.db import safe_query, safe_execute
from utils.security import password_hash, password_ok, login_rate_limited, touch_login
router = APIRouter()
# Session-signing secret. MUST come from env in production — no hardcoded fallback
# (a known constant would let anyone forge valid session tokens).
SECRET_KEY = os.environ.get("JWT_SECRET")
if not SECRET_KEY:
# Ephemeral random secret (all sessions invalidate on restart).
import secrets as _secrets
SECRET_KEY = _secrets.token_hex(32)
class UserRegister(BaseModel):
email: EmailStr
password: str
name: Optional[str] = None
class UserLogin(BaseModel):
email: EmailStr
password: str
class ResetPasswordReq(BaseModel):
email: EmailStr
class UpdatePasswordReq(BaseModel):
token: str
password: str
def generate_session_token(user_id: int, role: str, token_version: int = 0) -> str:
expiry = int(time.time()) + 8 * 3600 # 8 hours session
msg = f"{user_id}:{role}:{token_version}:{expiry}"
sig = hmac.new(SECRET_KEY.encode(), msg.encode(), hashlib.sha256).hexdigest()
return f"{msg}:{sig}"
def verify_session_token(token: str) -> dict:
try:
parts = token.split(":")
if len(parts) != 5:
raise HTTPException(status_code=401, detail="Invalid session token format")
user_id, role, token_version, expiry, sig = parts
msg = f"{user_id}:{role}:{token_version}:{expiry}"
expected_sig = hmac.new(SECRET_KEY.encode(), msg.encode(), hashlib.sha256).hexdigest()
if not hmac.compare_digest(sig, expected_sig):
raise HTTPException(status_code=401, detail="Session signature mismatch")
if int(expiry) < time.time():
raise HTTPException(status_code=401, detail="Session expired")
# Check token_version against DB to allow instant revocation.
# IMPORTANT: role is read from the DB, NOT trusted from the token — so a
# demotion/suspension takes effect immediately instead of persisting until
# the token expires.
df = safe_query("SELECT email, name, role, COALESCE(token_version, 0) as tv FROM qto_users WHERE id=%s", (user_id,))
if df.empty:
raise HTTPException(status_code=401, detail="User not found")
db_version = int(df.iloc[0]["tv"])
if int(token_version) != db_version:
raise HTTPException(status_code=401, detail="Session revoked — please log in again")
db_role = df.iloc[0]["role"]
if db_role == "blocked":
raise HTTPException(status_code=403, detail="Your account has been suspended.")
email = df.iloc[0]["email"]
raw_name = df.iloc[0]["name"]
name = str(raw_name) if raw_name is not None and str(raw_name).lower() != "nan" else None
return {"id": int(user_id), "role": db_role, "email": email, "name": name}
except HTTPException:
raise
except Exception:
raise HTTPException(status_code=401, detail="Invalid token")
def get_current_user(request: Request, authorization: Optional[str] = Header(None)) -> dict:
if not authorization:
authorization = request.query_params.get("Authorization")
if not authorization or not authorization.startswith("Bearer "):
raise HTTPException(status_code=401, detail="Missing or invalid Authorization header")
token = authorization.split(" ")[1]
return verify_session_token(token)
def get_optional_current_user(request: Request, authorization: Optional[str] = Header(None)) -> Optional[dict]:
if not authorization:
authorization = request.query_params.get("Authorization")
if not authorization or not authorization.startswith("Bearer "):
return None
try:
token = authorization.split(" ")[1]
return verify_session_token(token)
except Exception:
return None
@router.post("/register")
async def register(req: UserRegister):
email = req.email.strip().lower()
if len(req.password) < 6:
raise HTTPException(status_code=400, detail="Password must be at least 6 characters.")
# Check if user already exists
df = safe_query("SELECT id FROM qto_users WHERE email=%s", (email,))
if not df.empty:
raise HTTPException(status_code=400, detail="User with this email already exists.")
# Check if account was deleted in the last 90 days
import pandas as pd
from datetime import datetime, timedelta
df_del = safe_query("SELECT deleted_at FROM qto_deleted_accounts WHERE email=%s ORDER BY id DESC LIMIT 1", (email,))
if not df_del.empty:
deleted_at = df_del.iloc[0]["deleted_at"]
if pd.notna(deleted_at):
try:
dt = pd.to_datetime(deleted_at).tz_localize(None)
now = pd.Timestamp.utcnow().tz_localize(None)
if (now - dt).days < 90:
raise HTTPException(status_code=400, detail="This email is restricted from registering for 90 days after account deletion.")
except HTTPException:
raise
except Exception as e:
pass
hashed = password_hash(req.password)
success, err = safe_execute(
"INSERT INTO qto_users (email, password_hash, role, extra_projects_allowance, name) VALUES (%s, %s, %s, %s, %s)",
(email, hashed, "user", 0, req.name)
)
if not success:
raise HTTPException(status_code=500, detail=f"Database insertion failed: {err}")
return {"message": "User registered successfully."}
@router.post("/login")
async def login(req: UserLogin):
email = req.email.strip().lower()
if login_rate_limited(email):
raise HTTPException(status_code=429, detail="Too many failed login attempts. Please try again in 15 minutes.")
df = safe_query("SELECT id, email, password_hash, role FROM qto_users WHERE email=%s", (email,))
if df.empty:
# Log failed attempt
safe_execute(
"INSERT INTO qto_usage_logs (event_type, metadata) VALUES (%s, %s)",
("login_failed", f'{{"email": "{email}"}}')
)
raise HTTPException(status_code=401, detail="Invalid email or password.")
user = df.iloc[0]
if not password_ok(req.password, user["password_hash"]):
safe_execute(
"INSERT INTO qto_usage_logs (event_type, metadata) VALUES (%s, %s)",
("login_failed", f'{{"email": "{email}"}}')
)
raise HTTPException(status_code=401, detail="Invalid email or password.")
if user["role"] == "blocked":
raise HTTPException(status_code=403, detail="Your account has been suspended.")
touch_login(user["id"])
# Read token_version for revocation support
tv_df = safe_query("SELECT COALESCE(token_version, 0) as tv FROM qto_users WHERE id=%s", (user["id"],))
tv = int(tv_df.iloc[0]["tv"]) if not tv_df.empty else 0
token = generate_session_token(user["id"], user["role"], tv)
return {
"token": token,
"user": {
"id": int(user["id"]),
"email": user["email"],
"role": user["role"]
}
}
@router.get("/profile")
async def profile(current_user: dict = Depends(get_current_user)):
import pandas as pd
df = safe_query("SELECT id, email, role, created_at, name FROM qto_users WHERE id=%s", (current_user["id"],))
if df.empty:
raise HTTPException(status_code=404, detail="User not found")
user = df.iloc[0]
# Get plan information
from utils.plans import get_plan_for_user
plan = get_plan_for_user(user["id"], user["role"])
return {
"id": int(user["id"]),
"name": user["name"] if "name" in user and pd.notna(user["name"]) else None,
"email": user["email"],
"role": user["role"],
"created_at": user["created_at"],
"plan": {
"tier": plan.tier,
"name": plan.name,
"project_limit": plan.projects,
"ai_call_limit": plan.ai_calls,
"export_limit": plan.exports
}
}
@router.post("/forgot-password")
async def forgot_password(req: ResetPasswordReq):
from utils.security import issue_reset_token
token = issue_reset_token(req.email)
if token:
import logging
logging.getLogger("qto").info("Password reset token for %s: %s", req.email, token)
# Send actual email
from utils.mailer import send_reset_email
send_reset_email(req.email, token)
# Always return the same generic message — do not reveal whether the email exists
# (prevents account enumeration).
return {"message": "If an account exists for this email, a reset token has been sent."}
@router.post("/reset-password")
async def reset_password_route(req: UpdatePasswordReq):
from utils.security import reset_password
if len(req.password) < 6:
raise HTTPException(status_code=400, detail="Password must be at least 6 characters.")
if reset_password(req.token, req.password):
return {"message": "Password updated successfully."}
raise HTTPException(status_code=400, detail="Invalid or expired reset token.")
class UserProfileUpdate(BaseModel):
name: str
@router.put("/profile")
async def update_profile(req: UserProfileUpdate, current_user: dict = Depends(get_current_user)):
success, err = safe_execute("UPDATE qto_users SET name=%s WHERE id=%s", (req.name, current_user["id"]))
if not success:
raise HTTPException(status_code=500, detail=f"Database update failed: {err}")
return {"message": "Profile updated successfully."}
@router.delete("/account")
async def delete_account(current_user: dict = Depends(get_current_user)):
user_id = current_user["id"]
email = current_user["email"]
# Stop all future billing BEFORE removing the account, otherwise the user
# keeps getting charged by Dodo after deleting their account.
try:
from utils.payments import cancel_user_subscriptions
cancel_user_subscriptions(user_id)
except Exception:
import logging
logging.getLogger("qto").exception("Failed to cancel subscriptions on account delete for %s", user_id)
# Record the deletion and remove the user atomically (all-or-nothing).
try:
from utils.db import transaction
with transaction() as cur:
cur.execute("INSERT INTO qto_deleted_accounts (email) VALUES (%s)", (email,))
cur.execute("DELETE FROM qto_users WHERE id=%s", (user_id,))
except Exception as err:
raise HTTPException(status_code=500, detail=f"Database deletion failed: {err}")
return {"message": "Account deleted successfully."}
|