Skip to content

Commit 089d648

Browse files
committed
pysnippetGH-42: error handling ignores library user code exceptions
- Removed the generic error handling in __call__() - Introduced specific error handling inside authenticate() for jwt decoding. - One use of jwt is in token generation in core, but in this case it won't be a authorization error but maybe a configuration one, we should see the details in logging or debugging platforms. - For sure, other authorization errors caught previously as Exception now run on the wild. Review required on that.
1 parent cb8b445 commit 089d648

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/fastapi_oauth2/middleware.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ async def authenticate(self, request: Request) -> Optional[Tuple[Auth, User]]:
108108
if not scheme or not param:
109109
return Auth(), User()
110110

111-
token_data = Auth.jwt_decode(param)
111+
try:
112+
token_data = Auth.jwt_decode(param)
113+
except JOSEError as e:
114+
raise OAuth2AuthenticationError(401, str(e))
112115
if token_data["exp"] and token_data["exp"] < int(datetime.now(timezone.utc).timestamp()):
113116
raise OAuth2AuthenticationError(401, "Token expired")
114117

@@ -152,9 +155,5 @@ def __init__(
152155

153156
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
154157
if scope["type"] == "http":
155-
try:
156-
return await self.auth_middleware(scope, receive, send)
157-
except (JOSEError, Exception) as e:
158-
middleware = PlainTextResponse(str(e), status_code=401)
159-
return await middleware(scope, receive, send)
158+
return await self.auth_middleware(scope, receive, send)
160159
await self.default_application_middleware(scope, receive, send)

0 commit comments

Comments
 (0)