Przeglądaj źródła

Remaining fixes during ldo development (#4)

Casper van der Wel 1 rok temu
rodzic
commit
e09ed2a9ea

+ 4 - 1
CHANGES.md

@@ -4,7 +4,10 @@
 0.2.1 (unreleased)
 ------------------
 
-- Nothing changed yet.
+- Add HTTP Bearer to OpenAPI security schema.
+
+- Import debugpy at module level on setup_debugger import. Don't check for DEBUG
+  environment variable when setting up.
 
 
 0.2.0 (2023-08-03)

+ 4 - 1
clean_python/fastapi/service.py

@@ -12,6 +12,7 @@ from fastapi import Depends
 from fastapi import FastAPI
 from fastapi import Request
 from fastapi.exceptions import RequestValidationError
+from fastapi.security import HTTPBearer
 from fastapi.security import OAuth2AuthorizationCodeBearer
 from starlette.types import ASGIApp
 
@@ -58,6 +59,7 @@ class OAuth2WithClientDependable(OAuth2AuthorizationCodeBearer):
     ):
         self.verifier = sync_to_async(TokenVerifier(settings), thread_sensitive=False)
         super().__init__(
+            scheme_name="OAuth2 Authorization Code Flow with PKCE",
             authorizationUrl=str(client.authorization_url),
             tokenUrl=str(client.token_url),
         )
@@ -66,7 +68,7 @@ class OAuth2WithClientDependable(OAuth2AuthorizationCodeBearer):
         ctx.claims = await self.verifier(request.headers.get("Authorization"))
 
 
-class OAuth2WithoutClientDependable:
+class OAuth2WithoutClientDependable(HTTPBearer):
     """A fastapi 'dependable' configuring OAuth2.
 
     This does one thing:
@@ -75,6 +77,7 @@ class OAuth2WithoutClientDependable:
 
     def __init__(self, settings: TokenVerifierSettings):
         self.verifier = sync_to_async(TokenVerifier(settings), thread_sensitive=False)
+        super().__init__(scheme_name="JWT Bearer token", bearerFormat="JWT")
 
     async def __call__(self, request: Request) -> None:
         ctx.claims = await self.verifier(request.headers.get("Authorization"))

+ 0 - 1
clean_python/testing/__init__.py

@@ -1,2 +1 @@
 from .attr_dict import *  # NOQA
-from .debugger import *  # NOQA

+ 6 - 14
clean_python/testing/debugger.py

@@ -1,21 +1,13 @@
 import os
 
+import debugpy
+
 __all__ = ["setup_debugger"]
 
 
 def setup_debugger(*, host: str = "0.0.0.0", port: int = 5678):
     """Configure debugging via debugpy."""
-
-    # Only to be used in development. Should someone inadvertently set DEBUG to True in
-    # staging or production, a ModuleNotFoundError will be raised, because debugpy is
-    # only available via requirements-dev.txt - this is intentionally.
-    if os.environ.get("DEBUG") or os.environ.get("DEBUG_WAIT_FOR_CLIENT"):
-        try:
-            import debugpy
-
-            debugpy.listen((host, port))
-            if os.environ.get("DEBUG_WAIT_FOR_CLIENT"):
-                print("🔌 debugpy waiting for a client to attach 🔌", flush=True)
-                debugpy.wait_for_client()
-        except (ModuleNotFoundError, RuntimeError) as e:
-            print(e, flush=True)
+    debugpy.listen((host, port))
+    if os.environ.get("DEBUG_WAIT_FOR_CLIENT"):
+        print("🔌 debugpy waiting for a client to attach 🔌", flush=True)
+        debugpy.wait_for_client()

+ 4 - 3
integration_tests/conftest.py

@@ -5,15 +5,16 @@ import os
 
 import pytest
 
-from clean_python.testing import setup_debugger
-
 
 def pytest_sessionstart(session):
     """
     Called after the Session object has been created and
     before performing collection and entering the run test loop.
     """
-    setup_debugger()
+    if os.environ.get("DEBUG") or os.environ.get("DEBUG_WAIT_FOR_CLIENT"):
+        from clean_python.testing.debugger import setup_debugger
+
+        setup_debugger()
 
 
 @pytest.fixture(scope="session")