fix
This commit is contained in:
parent
476ad2bbce
commit
036b6de476
@ -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/<param>")
|
||||
@app.get("/pillar/{param}")
|
||||
async def pillar(param: str):
|
||||
print(param)
|
||||
return JSONResponse(content={})
|
||||
return JSONResponse(content={ "param": param })
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@ -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')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user