Download Backend/fastapi/security/tokens.py from DevDoCode/stremio: direct link, hf CLI and curl.
- Browser
- Download file 3.42 kB
-
https://huggingface.co/spaces/DevDoCode/stremio/resolve/main/Backend/fastapi/security/tokens.py
- Command line
-
hf download hf://spaces/DevDoCode/stremio/Backend/fastapi/security/tokens.py
-
curl -L -o tokens.py https://huggingface.co/spaces/DevDoCode/stremio/resolve/main/Backend/fastapi/security/tokens.py
3.42 kB
| 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 | |