diff --git a/pillar_tool/__init__.py b/pillar_tool/__init__.py index da7095d..8ea4286 100644 --- a/pillar_tool/__init__.py +++ b/pillar_tool/__init__.py @@ -13,14 +13,18 @@ def on_auth_error(request: Request, exc: Exception): return response +def on_general_error(request: Request, exc: Exception): + print("wtf?") + response = PlainTextResponse(str(exc), status_code=500) + app = FastAPI() app.add_middleware(AuthenticationMiddleware, backend=BasicAuthBackend(), on_error=on_auth_error) +app.exception_handler(Exception)(on_general_error) @app.get("/") async def root(): return {"message": "Hello World"} -@app.get("/pillar/") +@app.get("/pillar/{param}") async def pillar(param: str): - print(param) - return JSONResponse(content={}) + return JSONResponse(content={ "param": param }) diff --git a/pillar_tool/__pycache__/__init__.cpython-313.pyc b/pillar_tool/__pycache__/__init__.cpython-313.pyc index de3857e..6908bf3 100644 Binary files a/pillar_tool/__pycache__/__init__.cpython-313.pyc and b/pillar_tool/__pycache__/__init__.cpython-313.pyc differ diff --git a/pillar_tool/middleware/__pycache__/basicauth_backend.cpython-313.pyc b/pillar_tool/middleware/__pycache__/basicauth_backend.cpython-313.pyc index ea77e96..db5ed03 100644 Binary files a/pillar_tool/middleware/__pycache__/basicauth_backend.cpython-313.pyc and b/pillar_tool/middleware/__pycache__/basicauth_backend.cpython-313.pyc differ diff --git a/pillar_tool/middleware/basicauth_backend.py b/pillar_tool/middleware/basicauth_backend.py index 6a35025..726fe6c 100644 --- a/pillar_tool/middleware/basicauth_backend.py +++ b/pillar_tool/middleware/basicauth_backend.py @@ -1,7 +1,8 @@ import base64 import binascii +import hypercorn.logging -from starlette.authentication import AuthenticationBackend, AuthenticationError +from starlette.authentication import AuthenticationBackend, AuthenticationError, AuthCredentials, SimpleUser class BasicAuthBackend(AuthenticationBackend): @@ -18,8 +19,9 @@ class BasicAuthBackend(AuthenticationBackend): except (ValueError, UnicodeDecodeError, binascii.Error): raise AuthenticationError('Invalid basic auth credentials') + username, _, password = decoded.partition(":") if username == 'admin' and password == 'password': - return None + return AuthCredentials(["authenticated"]), SimpleUser('admin') raise AuthenticationError('Invalid basic auth credentials')