A declined card left the account in a state with no way out. `past_due` grants no minutes, so the metering layer resolved the user to the free plan — which meant /api/me reported status=null, the account page swapped "Manage billing" for "Upgrade" (hiding the only button that fixes a card), and the checkout guard then rejected the upgrade with a 409 that every call site swallowed into a generic "Could not start checkout." Silently free, unwarned, unable to pay. Found on 25-jul-2026 in prod: 14 subscriptions in this state, all first charges that failed when the (now retired) 3-day trial ended. The trial is gone, but the trap is not — a renewal failure or an abandoned 3DS payment reproduces it. - metering: split `_subscription_row` (raw) from `_active_subscription` (entitling), and expose both on the balance. Entitlement logic is unchanged. - /api/me: report status/interval from a row needing attention when no subscription entitles, plus `has_billing_account` to route the portal button. - checkout: drop `incomplete` from the blocking states. Stripe collects nothing on it and expires it within 24h, so blocking only stopped the user's retry. Give past_due/unpaid a message that names the problem and the fix. - account page: explain the failed payment inline with a link to the portal instead of showing a bare "past_due" chip, and keep the portal reachable for any Stripe relationship, live or broken. - api client: throw a typed ApiError carrying the server's detail so callers stop replacing real, actionable messages with generic copy. Verified against a real DB: past_due stays blocked but visible and fixable, incomplete can retry, canceled is silent and can subscribe again.
236 lines
9.2 KiB
Python
236 lines
9.2 KiB
Python
"""Authentication for cloud mode: magic link + JWT sessions + current-user loading.
|
|
|
|
- Magic link: POST /api/auth/magic-link -> emailed single-use token (15 min).
|
|
POST /api/auth/magic-link/verify consumes it (POST, not GET, so email scanners
|
|
that prefetch links can't burn them) and returns a 30-day JWT.
|
|
- get_current_user_optional never raises: a missing/expired token degrades to the
|
|
anonymous BYOK flow rather than breaking it.
|
|
- /api/me returns the user's plan + minute balance (no Stripe call needed).
|
|
"""
|
|
import hashlib
|
|
import secrets
|
|
from datetime import datetime, timezone, timedelta
|
|
from typing import Optional
|
|
|
|
import jwt
|
|
from fastapi import APIRouter, Request, HTTPException
|
|
from pydantic import BaseModel, EmailStr
|
|
from sqlalchemy import select, func, and_
|
|
|
|
from .config import settings
|
|
from . import config, database, metering, email_policy
|
|
from .models import User, MagicLinkToken, UploadPostProfile
|
|
|
|
router = APIRouter()
|
|
|
|
JWT_ALGO = "HS256"
|
|
JWT_TTL_DAYS = 30
|
|
MAGIC_TTL_MINUTES = 15
|
|
MAGIC_RATE_LIMIT = 3 # per email
|
|
MAGIC_RATE_WINDOW_MIN = 15
|
|
|
|
|
|
def _now():
|
|
return datetime.now(timezone.utc)
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# JWT + current user
|
|
# --------------------------------------------------------------------------- #
|
|
def issue_jwt(user_id, email) -> str:
|
|
payload = {
|
|
"sub": str(user_id),
|
|
"email": email,
|
|
"exp": _now() + timedelta(days=JWT_TTL_DAYS),
|
|
"iat": _now(),
|
|
}
|
|
return jwt.encode(payload, settings.jwt_secret, algorithm=JWT_ALGO)
|
|
|
|
|
|
def _decode_jwt(token: str) -> Optional[dict]:
|
|
try:
|
|
return jwt.decode(token, settings.jwt_secret, algorithms=[JWT_ALGO])
|
|
except Exception:
|
|
return None
|
|
|
|
|
|
class CurrentUser:
|
|
"""Lightweight per-request user snapshot carried through endpoints.
|
|
|
|
Kept small and eager (no lazy DB access) so sync helpers like
|
|
``managed_keys.has_active_entitlement`` can read it without awaiting.
|
|
"""
|
|
|
|
def __init__(self, id, email, entitled=False, plan=None, upload_post_profile=None):
|
|
self.id = id
|
|
self.email = email
|
|
self.entitled = entitled
|
|
self.plan = plan
|
|
self.upload_post_profile = upload_post_profile
|
|
|
|
|
|
async def _load_current_user(session, user_id) -> Optional["CurrentUser"]:
|
|
user = await session.get(User, user_id)
|
|
if user is None:
|
|
return None
|
|
sub = await metering._active_subscription(session, user_id)
|
|
topups = await metering._topups_fifo(session, user_id)
|
|
topup_remaining = sum(
|
|
(float(t.minutes_total) - float(t.minutes_consumed) for t in topups), 0.0
|
|
)
|
|
profile = await session.get(UploadPostProfile, user_id)
|
|
# Entitled to managed keys with an active plan, any top-up credit, or the
|
|
# free monthly allowance (Google-authenticated accounts only).
|
|
free = sub is None and metering.free_plan_eligible(user)
|
|
entitled = bool(sub is not None or topup_remaining > 0 or free)
|
|
return CurrentUser(
|
|
id=user.id,
|
|
email=user.email,
|
|
entitled=entitled,
|
|
plan=sub.plan if sub else ("free" if free else None),
|
|
upload_post_profile=profile.profile_username if profile else None,
|
|
)
|
|
|
|
|
|
async def get_current_user_optional(request: Request) -> Optional[CurrentUser]:
|
|
"""Return the authenticated user, or None for anonymous / BYOK requests."""
|
|
auth = request.headers.get("Authorization", "")
|
|
if not auth.lower().startswith("bearer "):
|
|
return None
|
|
payload = _decode_jwt(auth[7:].strip())
|
|
if not payload:
|
|
return None
|
|
try:
|
|
import uuid
|
|
user_id = uuid.UUID(payload["sub"])
|
|
except Exception:
|
|
return None
|
|
async with database.session() as session:
|
|
return await _load_current_user(session, user_id)
|
|
|
|
|
|
async def get_current_user_required(request: Request) -> CurrentUser:
|
|
user = await get_current_user_optional(request)
|
|
if user is None:
|
|
raise HTTPException(status_code=401, detail="Authentication required")
|
|
return user
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# Magic link
|
|
# --------------------------------------------------------------------------- #
|
|
class MagicLinkRequest(BaseModel):
|
|
email: EmailStr
|
|
|
|
|
|
class MagicVerifyRequest(BaseModel):
|
|
token: str
|
|
|
|
|
|
@router.post("/api/auth/magic-link")
|
|
async def request_magic_link(payload: MagicLinkRequest, request: Request):
|
|
from .emails import send_magic_link_email
|
|
|
|
# Block temp-mail before doing anything else — the free plan is open to
|
|
# email accounts now, so disposable domains would be a free-minute farm.
|
|
if email_policy.is_disposable(payload.email):
|
|
raise HTTPException(status_code=400, detail=(
|
|
"That email provider isn't supported. Please use a permanent "
|
|
"address (Gmail, Outlook, iCloud…) or sign in with Google."))
|
|
|
|
# Normalize (strip +tags / Gmail dots) so aliases can't mint extra accounts.
|
|
email = email_policy.normalize_email(payload.email)
|
|
async with database.session() as session:
|
|
async with session.begin():
|
|
window_start = _now() - timedelta(minutes=MAGIC_RATE_WINDOW_MIN)
|
|
recent = (await session.execute(
|
|
select(func.count(MagicLinkToken.id)).where(and_(
|
|
MagicLinkToken.email == email,
|
|
MagicLinkToken.created_at >= window_start,
|
|
))
|
|
)).scalar_one()
|
|
if recent >= MAGIC_RATE_LIMIT:
|
|
raise HTTPException(status_code=429, detail="Too many login attempts. Try again in a few minutes.")
|
|
|
|
raw_token = secrets.token_urlsafe(32)
|
|
token_hash = hashlib.sha256(raw_token.encode()).hexdigest()
|
|
session.add(MagicLinkToken(
|
|
email=email,
|
|
token_hash=token_hash,
|
|
expires_at=_now() + timedelta(minutes=MAGIC_TTL_MINUTES),
|
|
request_ip=request.client.host if request.client else None,
|
|
))
|
|
|
|
link = f"{settings.frontend_url}/#/auth/verify?ml={raw_token}"
|
|
await send_magic_link_email(email, link)
|
|
return {"ok": True}
|
|
|
|
|
|
@router.post("/api/auth/magic-link/verify")
|
|
async def verify_magic_link(payload: MagicVerifyRequest):
|
|
token_hash = hashlib.sha256(payload.token.encode()).hexdigest()
|
|
async with database.session() as session:
|
|
async with session.begin():
|
|
row = (await session.execute(
|
|
select(MagicLinkToken).where(MagicLinkToken.token_hash == token_hash)
|
|
.with_for_update()
|
|
)).scalar_one_or_none()
|
|
if row is None or row.used_at is not None or row.expires_at < _now():
|
|
raise HTTPException(status_code=400, detail="This sign-in link is invalid or expired.")
|
|
row.used_at = _now()
|
|
email = row.email.lower()
|
|
|
|
user = (await session.execute(
|
|
select(User).where(User.email == email)
|
|
)).scalar_one_or_none()
|
|
if user is None:
|
|
user = User(email=email, last_login_at=_now())
|
|
session.add(user)
|
|
await session.flush()
|
|
else:
|
|
user.last_login_at = _now()
|
|
user_id, user_email = user.id, user.email
|
|
|
|
token = issue_jwt(user_id, user_email)
|
|
cu = await _load_current_user(session, user_id)
|
|
return {"token": token, "user": {"id": str(user_id), "email": user_email, "entitled": cu.entitled}}
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# /api/me
|
|
# --------------------------------------------------------------------------- #
|
|
@router.get("/api/me")
|
|
async def get_me(request: Request):
|
|
user = await get_current_user_required(request)
|
|
async with database.session() as session:
|
|
bal = await metering._balance(session, user.id)
|
|
sub = bal["_sub"]
|
|
# Report the entitling subscription when there is one; otherwise fall back to
|
|
# a row that needs the customer's attention (card declined, payment never
|
|
# finished). Without this a past_due customer sees a plain free account and
|
|
# never learns their card failed. `plan` stays as computed — the entitlement
|
|
# really is free — so the UI shows "free plan" plus the warning badge.
|
|
attention = bal["_sub_row"]
|
|
if attention is not None and attention.status not in config.BILLING_ATTENTION_STATES:
|
|
attention = None
|
|
shown = sub or attention
|
|
return {
|
|
"user": {"id": str(user.id), "email": user.email},
|
|
"entitled": user.entitled,
|
|
"plan": bal["plan"],
|
|
"status": shown.status if shown else None,
|
|
"interval": shown.interval if shown else None,
|
|
"period_end": bal["period_end"].isoformat() if bal["period_end"] else None,
|
|
"cancel_at_period_end": shown.cancel_at_period_end if shown else False,
|
|
# Drives the "Manage billing" route in the UI: any Stripe relationship,
|
|
# live or broken, must keep the portal reachable.
|
|
"has_billing_account": bool(sub or attention),
|
|
"minutes": {
|
|
"plan_allowance": bal["plan_allowance"],
|
|
"plan_used": bal["plan_used"],
|
|
"plan_remaining": bal["plan_remaining"],
|
|
"topup_remaining": bal["topup_remaining"],
|
|
"remaining": bal["remaining"],
|
|
},
|
|
"upload_post_profile": user.upload_post_profile,
|
|
}
|