jazzband / django-oauth-toolkit

OAuth2 goodies for the Djangonauts!
https://django-oauth-toolkit.readthedocs.io
Other
3.16k stars 793 forks source link

Raise InvalidGrantError if no associated grant exists when invalidating an authorization code #1476

Closed mforner13 closed 2 months ago

mforner13 commented 2 months ago

Description of the Change

This change raises an InvalidGrantError from the OAuth2Validator.invalidate_authorization_code method if the Grant object intended to be deleted does not exist.

Currently, when invalidating an authorization code after it has been used, if for whatever reason the associated grant object no longer exists, an uncaught Grant.DoesNotExist exception is raised. This leads to 500 responses being returned to clients. This could, for example, be caused by concurrent requests being made using the same authorization code.

The change in this PR handles this scenario gracefully by catching Grant.DoesNotExist and raising an InvalidGrantError which will return a 400 'invalid_grant' response to the client.

Checklist