What I want to do is to protect the openapi docs in sanic-ext.So I add a session auth on app.middleware(“request”). Code is below
@app.middleware("request")
async def basic_auth(request:Request):
print(request.path)
if request.path.startswith(app.config["OAS_URL_PREFIX"]):
is_auth = request.ctx.session.get("doc_auth",None)
if not is_auth:
return text("no auth doc")
but it seems the request.ctx don’t have session on it .error is below
[2022-10-22 20:56:33 +0800] [1964165] [DEBUG] Starting a process: Sanic-Server-0-0
[2022-10-22 20:56:33 +0800] [1964179] [INFO] Sanic Extensions:
[2022-10-22 20:56:33 +0800] [1964179] [INFO] > injection [0 added]
[2022-10-22 20:56:33 +0800] [1964179] [INFO] > openapi [http://0.0.0.0:8000/py/docs]
[2022-10-22 20:56:33 +0800] [1964179] [INFO] > http
[2022-10-22 20:56:33 +0800] [1964179] [INFO] > templating [jinja2==3.1.2]
[2022-10-22 20:56:34 +0800] [1964179] [INFO] Starting worker [1964179]
/py/docs/
[2022-10-22 20:56:36 +0800] [1964179] [ERROR] Exception occurred while handling uri: 'https://0.0.0.0:8000/py/docs/'
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/sanic/app.py", line 901, in handle_request
response = await self._run_request_middleware(
File "/usr/local/lib/python3.8/dist-packages/sanic/app.py", line 1071, in _run_request_middleware
response = await response
File "server.py", line 70, in basic_auth
is_auth = request.ctx.session.get("doc_auth",None)
AttributeError: 'types.SimpleNamespace' object has no attribute 'session'
Is there any solution?
the token auth maybe a work way but i don’t want to use it