File size: 3,424 Bytes
36dde81 d3a7f5b 36dde81 d3a7f5b 36dde81 07f63a8 d3a7f5b e581a8c d3a7f5b 36dde81 d3a7f5b e581a8c 36dde81 d3a7f5b 36dde81 d3a7f5b 36dde81 d3a7f5b 36dde81 e581a8c d3a7f5b | 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 | from datetime import datetime, timezone
from fastapi import HTTPException
from Backend import db
from Backend.config import Telegram
from Backend.helper.settings_manager import SettingsManager
DAILY_LIMIT_VIDEO = "https://bit.ly/3YZFKT5"
MONTHLY_LIMIT_VIDEO = "https://bit.ly/4rfjtgd"
SUBSCRIPTION_EXPIRED_VIDEO = "https://bit.ly/4rfjtgd"
#----- Validate an API token and annotate it with subscription/limit status
async def verify_token(token: str):
token_data = await db.get_api_token(token)
if not token_data:
raise HTTPException(status_code=401, detail="Invalid or expired API token")
limits = token_data.get("limits", {})
usage = token_data.get("usage", {})
token_data["limit_exceeded"] = None
token_data["limit_video"] = None
token_data["subscription_expired"] = False
#----- Owner-linked tokens are admins (honour persisted flag, else derive)
_uid = token_data.get("user_id")
try:
token_data["is_admin"] = bool(token_data.get("is_admin")) or (
_uid is not None and int(_uid) == int(Telegram.OWNER_ID)
)
except (TypeError, ValueError):
token_data["is_admin"] = bool(token_data.get("is_admin"))
#----- Access expiry checks. Admin and never-expires tokens always bypass.
if not token_data["is_admin"] and not token_data.get("subscription_exempt"):
#----- Compare correctly regardless of timezone awareness
def _expired(when):
ref = datetime.utcnow()
try:
if when.tzinfo is not None:
ref = datetime.now(timezone.utc)
except AttributeError:
pass
return when < ref
#----- A token's own expiry is enforced in every mode. With no token expiry,
#----- an active subscription is required only while subscription mode is on.
#----- (When valid, fall through so data-limit checks still apply.)
token_expiry = token_data.get("expires_at")
if token_expiry is not None:
if _expired(token_expiry):
token_data["subscription_expired"] = True
return token_data
elif SettingsManager.current().subscription:
user_id = token_data.get("user_id")
user = await db.get_user(int(user_id)) if user_id else None
expiry = user.get("subscription_expiry") if user else None
if (not user_id or not user
or user.get("subscription_status") != "active"
or not expiry or _expired(expiry)):
token_data["subscription_expired"] = True
return token_data
if daily_limit := limits.get("daily_limit_gb"):
if daily_limit > 0:
current_daily_gb = usage.get("daily", {}).get("bytes", 0) / (1024 ** 3)
if current_daily_gb >= daily_limit:
token_data["limit_exceeded"] = "daily"
token_data["limit_video"] = DAILY_LIMIT_VIDEO
return token_data
if monthly_limit := limits.get("monthly_limit_gb"):
if monthly_limit > 0:
current_monthly_gb = usage.get("monthly", {}).get("bytes", 0) / (1024 ** 3)
if current_monthly_gb >= monthly_limit:
token_data["limit_exceeded"] = "monthly"
token_data["limit_video"] = MONTHLY_LIMIT_VIDEO
return token_data
return token_data
|