from fastapi import Depends, FastAPI, HTTPException
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from starlette.status import HTTP_401_UNAUTHORIZED
from fastapi.openapi.docs import get_swagger_ui_html

# Here only implemented docs url, for redoc depend requirement
app = FastAPI(docs_url=None)

security = HTTPBasic()

@app.get("/docs",include_in_schema=False)
async def get_documentation(credentials: HTTPBasicCredentials = Depends(security)):
    if credentials.username != "username" or credentials.password != "password":
        raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Incorrect username or password", headers={"WWW-Authenticate": "Basic"},)
    else:
        return get_swagger_ui_html(openapi_url="/openapi.json", title="docs")


Comments

comments powered by Disqus