This commit is contained in:
Linus Vogel 2025-12-21 17:38:38 +01:00
parent 476ad2bbce
commit 036b6de476
4 changed files with 11 additions and 5 deletions

View File

@ -13,14 +13,18 @@ def on_auth_error(request: Request, exc: Exception):
return response return response
def on_general_error(request: Request, exc: Exception):
print("wtf?")
response = PlainTextResponse(str(exc), status_code=500)
app = FastAPI() app = FastAPI()
app.add_middleware(AuthenticationMiddleware, backend=BasicAuthBackend(), on_error=on_auth_error) app.add_middleware(AuthenticationMiddleware, backend=BasicAuthBackend(), on_error=on_auth_error)
app.exception_handler(Exception)(on_general_error)
@app.get("/") @app.get("/")
async def root(): async def root():
return {"message": "Hello World"} return {"message": "Hello World"}
@app.get("/pillar/<param>") @app.get("/pillar/{param}")
async def pillar(param: str): async def pillar(param: str):
print(param) return JSONResponse(content={ "param": param })
return JSONResponse(content={})

View File

@ -1,7 +1,8 @@
import base64 import base64
import binascii import binascii
import hypercorn.logging
from starlette.authentication import AuthenticationBackend, AuthenticationError from starlette.authentication import AuthenticationBackend, AuthenticationError, AuthCredentials, SimpleUser
class BasicAuthBackend(AuthenticationBackend): class BasicAuthBackend(AuthenticationBackend):
@ -18,8 +19,9 @@ class BasicAuthBackend(AuthenticationBackend):
except (ValueError, UnicodeDecodeError, binascii.Error): except (ValueError, UnicodeDecodeError, binascii.Error):
raise AuthenticationError('Invalid basic auth credentials') raise AuthenticationError('Invalid basic auth credentials')
username, _, password = decoded.partition(":") username, _, password = decoded.partition(":")
if username == 'admin' and password == 'password': if username == 'admin' and password == 'password':
return None return AuthCredentials(["authenticated"]), SimpleUser('admin')
raise AuthenticationError('Invalid basic auth credentials') raise AuthenticationError('Invalid basic auth credentials')