|
10 | 10 | from typing import Union
|
11 | 11 |
|
12 | 12 | from fastapi.security.utils import get_authorization_scheme_param
|
| 13 | +from jose.exceptions import JOSEError |
13 | 14 | from jose.jwt import decode as jwt_decode
|
14 | 15 | from jose.jwt import encode as jwt_encode
|
15 | 16 | from starlette.authentication import AuthCredentials
|
16 | 17 | from starlette.authentication import AuthenticationBackend
|
17 | 18 | from starlette.authentication import BaseUser
|
18 | 19 | from starlette.middleware.authentication import AuthenticationMiddleware
|
19 | 20 | from starlette.requests import Request
|
| 21 | +from starlette.responses import PlainTextResponse |
20 | 22 | from starlette.types import ASGIApp
|
21 | 23 | from starlette.types import Receive
|
22 | 24 | from starlette.types import Scope
|
@@ -144,5 +146,9 @@ def __init__(
|
144 | 146 |
|
145 | 147 | async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
146 | 148 | if scope["type"] == "http":
|
147 |
| - return await self.auth_middleware(scope, receive, send) |
| 149 | + try: |
| 150 | + return await self.auth_middleware(scope, receive, send) |
| 151 | + except (JOSEError, Exception) as e: |
| 152 | + middleware = PlainTextResponse(str(e), status_code=401) |
| 153 | + return await middleware(scope, receive, send) |
148 | 154 | await self.default_application_middleware(scope, receive, send)
|
0 commit comments