#!/usr/bin/env python3.12 import logging from typing import Optional from fastapi import FastAPI, HTTPException from fastapi.responses import RedirectResponse class Utilities: """ API Utilities """ def __init__(self, app: FastAPI, constants): self.constants = constants self.blocked_redirect_uri = "https://codey.lol" self.app = app def get_blocked_response(self, path: Optional[str] = None): """ Return a redirect response for blocked requests. Args: path (Optional[str]): The requested path (currently unused). Returns: RedirectResponse: A redirect to the blocked URI. """ logging.error("Rejected request: Blocked") return RedirectResponse(url=self.blocked_redirect_uri) def get_no_endpoint_found(self, path: Optional[str] = None): """ Raise an HTTP 404 exception for unknown endpoints. Args: path (Optional[str]): The requested path (currently unused). Raises: HTTPException: With status code 404 and detail "Unknown endpoint". """ logging.error("Rejected request: No such endpoint") raise HTTPException(detail="Unknown endpoint", status_code=404) def check_key(self, path: str, key: str, req_type: int = 0) -> bool: """ Check if the provided API key is valid and meets the requirements. Args: path (str): The request path (reserved for future fine-tuning). key (str): The authorization header value, expected to start with "Bearer ". req_type (int): The type of access required. 0: Basic access. 2: Private access (key must start with "PRV-"). 4: Radio access (key must start with "RAD-"). Returns: bool: True if the key is valid and meets the requirements, False otherwise. """ if not key or not key.startswith("Bearer "): return False _key: str = key.split("Bearer ", maxsplit=1)[1].strip() if _key not in self.constants.API_KEYS: return False if req_type == 2: return _key.startswith("PRV-") elif req_type == 4: return _key.startswith("RAD-") if path.lower().startswith("/xc/") and not key.startswith("XC-"): return False return True