fastapi-oauth2 icon indicating copy to clipboard operation
fastapi-oauth2 copied to clipboard

GH-42: Fix: Middleware overhandling exceptions

Open vokimon opened this issue 1 year ago • 0 comments
trafficstars

Move middleware error handling from __call__ to authenticate and make it more specific.

Motivation:

This pull request attempts to fix issue GH-42: Middleware exception handling now intercepts any exception raised by user code as AuthenticationError 401, even when the entry point requires no Authentication. This behavior masks coding errors in user code while developing, and bug detection in logs while in production.

The behaviour was reintroduced in https://github.com/pysnippet/fastapi-oauth2/commit/aa8f4b318816f73749c8cdca4d7ad60c2a61e6c4

Removing it from __call__ fixes the problem, but then exceptions really related to authentication must be handled. My first proposal is to move the handling to autenticate(). Instead of wide range handling, I opted to specific handling, just not to maks bugs and issues. But i just detected the JWT problems handling. Not sure if we have to deal any other but as authentication.

A second problem is how it is to be handled. I took as reference the previous PR on JWT expiration, and raised OAuth2AuthenticationError. The problem with that is that such exception is not HTTPException and FastApi / Starlette does not handle it as intended.

All Submissions:

  • [x] Have you followed the guidelines in our Contributing document?
  • [x] Have you checked to ensure there aren't other open Pull Requests for the same update/change?

Changes to Core Features:

  • [x] Have you added an explanation of what your changes do and why you'd like us to include them?
  • [ ] Have you updated the documentation related to the changes you have made? -> Wait to have a final solution.
  • [x] Have you written new tests for your core changes, as applicable?
  • [x] Have you successfully run tests with your changes locally?

vokimon avatar Jul 25 '24 09:07 vokimon