diff options
-rw-r--r-- | backend/__init__.py | 6 | ||||
-rw-r--r-- | backend/authentication/user.py | 5 | ||||
-rw-r--r-- | backend/discord.py | 5 |
3 files changed, 15 insertions, 1 deletions
diff --git a/backend/__init__.py b/backend/__init__.py index f8b0afe..b5360ef 100644 --- a/backend/__init__.py +++ b/backend/__init__.py @@ -8,12 +8,15 @@ from starlette.middleware.authentication import AuthenticationMiddleware from starlette.middleware.cors import CORSMiddleware from backend import constants -from backend import logs # This has to be imported before other logging statements +from backend import logs # This has to be imported before logging statements are used from backend.authentication import JWTAuthenticationBackend from backend.middleware import DatabaseMiddleware, ProtectedDocsMiddleware from backend.route_manager import create_route_map from backend.validation import api +logger = logging.getLogger(__name__) + +# Setup CORS Origins ORIGINS = [ r"(https://[^.?#]*--pydis-forms\.netlify\.app)", # Netlify Previews r"(https?://[^.?#]*.forms-frontend.pages.dev)", # Cloudflare Previews @@ -21,6 +24,7 @@ ORIGINS = [ if not constants.PRODUCTION: # Allow all hosts on non-production deployments + logger.info("Allowing all CORS origins.") ORIGINS.append(r"(.*)") ALLOW_ORIGIN_REGEX = "|".join(ORIGINS) diff --git a/backend/authentication/user.py b/backend/authentication/user.py index 857c2ed..18fa935 100644 --- a/backend/authentication/user.py +++ b/backend/authentication/user.py @@ -1,3 +1,4 @@ +import logging import typing as t import jwt @@ -7,6 +8,8 @@ from starlette.requests import Request from backend.constants import SECRET_KEY from backend.discord import fetch_user_details +logger = logging.getLogger(__name__) + class User(BaseUser): """Starlette BaseUser implementation for JWT authentication.""" @@ -35,6 +38,7 @@ class User(BaseUser): return jwt.decode(self.token, SECRET_KEY, algorithms=["HS256"]) async def fetch_admin_status(self, request: Request) -> bool: + logger.debug(f"Checking admin status for user with ID: {self.payload['id']}") self.admin = await request.state.db.admins.find_one( {"_id": self.payload["id"]} ) is not None @@ -43,6 +47,7 @@ class User(BaseUser): async def refresh_data(self) -> None: """Fetches user data from discord, and updates the instance.""" + logger.debug(f"Updating user info for user with ID: {self.payload['id']}") self.payload = await fetch_user_details(self.decoded_token.get("token")) updated_info = self.decoded_token diff --git a/backend/discord.py b/backend/discord.py index 8cb602c..5a4e0fb 100644 --- a/backend/discord.py +++ b/backend/discord.py @@ -1,4 +1,6 @@ """Various utilities for working with the Discord API.""" +import logging + import httpx from backend.constants import ( @@ -7,8 +9,11 @@ from backend.constants import ( API_BASE_URL = "https://discord.com/api/v8" +logger = logging.getLogger(__name__) + async def fetch_bearer_token(code: str, redirect: str, *, refresh: bool) -> dict: + logger.debug(f"Fetching bearer token with code: {code}") async with httpx.AsyncClient() as client: data = { "client_id": OAUTH2_CLIENT_ID, |