-
- Notifications
You must be signed in to change notification settings - Fork 8k
Closed
Labels
Description
I am trying to validate token in fastapi middleware but this seems impossible. As i am thinking middleware needs to make next call although its not required. I am not able to find any good solution to handle token in one go in this python-fastapi backend. Any help is appreciated.
@app.middleware("http") async def add_middleware_here(request: Request, call_next): token = request.headers["Authorization"] try: verification_of_token = verify_token(token) if verification_of_token: response = await call_next(request) return response except InvalidSignatureError as er: raise HTTPException(status_code=401)
My expectation is that it should throw 401 error once wrong token is provided.
Instead it throws 500 with below Exception traceback:
Traceback (most recent call last): File "c:\users\covered\appdata\local\programs\python\python38\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 385, in run_asgi result = await app(self.scope, self.receive, self.send) File "c:\users\covered\appdata\local\programs\python\python38\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 45, in __call__ return await self.app(scope, receive, send) File "c:\users\covered\appdata\local\programs\python\python38\lib\site-packages\fastapi\applications.py", line 140, in __call__ await super().__call__(scope, receive, send) File "c:\users\covered\appdata\local\programs\python\python38\lib\site-packages\starlette\applications.py", line 134, in __call__ await self.error_middleware(scope, receive, send) File "c:\users\covered\appdata\local\programs\python\python38\lib\site-packages\starlette\middleware\errors.py", line 178, in __call__ raise exc from None File "c:\users\covered\appdata\local\programs\python\python38\lib\site-packages\starlette\middleware\errors.py", line 156, in __call__ await self.app(scope, receive, _send) File "c:\users\covered\appdata\local\programs\python\python38\lib\site-packages\starlette\middleware\base.py", line 26, in __call__ await response(scope, receive, send) TypeError: 'NoneType' object is not callable