cleanup
This commit is contained in:
35
util.py
35
util.py
@ -1,29 +1,36 @@
|
||||
#!/usr/bin/env python3.12
|
||||
|
||||
import logging
|
||||
|
||||
from typing import Optional
|
||||
from fastapi import FastAPI, Response, HTTPException
|
||||
from fastapi.responses import RedirectResponse
|
||||
|
||||
|
||||
class Utilities:
|
||||
"""API 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: str | None = None): # pylint: disable=unused-argument
|
||||
"""Get Blocked HTTP Response"""
|
||||
def get_blocked_response(self, path: Optional[str] = None):
|
||||
"""
|
||||
Get Blocked HTTP Response
|
||||
"""
|
||||
logging.error("Rejected request: Blocked")
|
||||
return RedirectResponse(url=self.blocked_redirect_uri)
|
||||
|
||||
def get_no_endpoint_found(self, path: str | None = None): # pylint: disable=unused-argument
|
||||
"""Get 404 Response"""
|
||||
def get_no_endpoint_found(self, path: Optional[str] = None):
|
||||
"""
|
||||
Get 404 Response
|
||||
"""
|
||||
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):
|
||||
def check_key(self, path: str, key: str,
|
||||
req_type: int = 0) -> bool:
|
||||
"""
|
||||
Accepts path as an argument to allow fine tuning access for each API key, not currently in use.
|
||||
"""
|
||||
@ -31,19 +38,19 @@ class Utilities:
|
||||
if not key or not key.startswith("Bearer "):
|
||||
return False
|
||||
|
||||
key = key.split("Bearer ", maxsplit=1)[1].strip()
|
||||
_key: str = key.split("Bearer ", maxsplit=1)[1].strip()
|
||||
|
||||
if not key in self.constants.API_KEYS:
|
||||
if not _key in self.constants.API_KEYS:
|
||||
return False
|
||||
|
||||
if req_type == 2:
|
||||
return key.startswith("PRV-")
|
||||
return _key.startswith("PRV-")
|
||||
elif req_type == 4:
|
||||
return key.startswith("RAD-")
|
||||
return _key.startswith("RAD-")
|
||||
|
||||
if path.lower().startswith("/xc/") and not key.startswith("XC-"):
|
||||
return False
|
||||
|
||||
if path.lower().startswith("/xc/")\
|
||||
and not key.startswith("XC-"):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
Reference in New Issue
Block a user