27 lines
802 B
Python
27 lines
802 B
Python
from fastapi import FastAPI
|
|
from starlette.middleware.authentication import AuthenticationMiddleware
|
|
from starlette.requests import Request
|
|
from fastapi.responses import HTMLResponse, PlainTextResponse
|
|
from starlette.responses import JSONResponse
|
|
|
|
from pillar_tool.middleware.basicauth_backend import BasicAuthBackend
|
|
|
|
|
|
def on_auth_error(request: Request, exc: Exception):
|
|
response = PlainTextResponse(str(exc), status_code=401)
|
|
response.headers["WWW-Authenticate"] = "Basic"
|
|
|
|
return response
|
|
|
|
app = FastAPI()
|
|
app.add_middleware(AuthenticationMiddleware, backend=BasicAuthBackend(), on_error=on_auth_error)
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"message": "Hello World"}
|
|
|
|
@app.get("/pillar/<param>")
|
|
async def pillar(param: str):
|
|
print(param)
|
|
return JSONResponse(content={})
|