Skip to content

Refresh Token 500 Internal Server Error instead of the expected 401 Unauthorized #1318

@discobeta

Description

@discobeta

Describe the bug
While trying to refresh an OAuth token using an older refresh token, the server returns a 500 Internal Server Error instead of the expected 401 Unauthorized response. This occurs when an attempt is made to refresh the token with an older refresh token after it has been invalidated, resulting in a oauth2_provider.models.AccessToken.DoesNotExist error message.

To Reproduce

  1. Obtain a new token and a corresponding refresh token.
  2. Invalidate the new token by removing it (i.e., leave the "Authorization: Bearer" line empty) and attempt to refresh the token using the refresh token obtained in step 1.
  3. Use the new token obtained in step 2 to refresh the token again, successfully this time.
  4. Now, try to refresh the token using the original token obtained in step 1.
  5. At this point, a 500 Internal Server Error is encountered, with the error message oauth2_provider.models.AccessToken.DoesNotExist: AccessToken matching query does not exist.

Expected behavior
Upon attempting to refresh using an older or non-existent token, the server should return a 401 Unauthorized response. The system's current behavior of querying the database to find an AccessToken entry at this stage seems unnecessary and unclear.

Version
Django OAuth Toolkit Version: 2.2.0

  • I have tested with the latest published release and it's still a problem.
  • I have tested with the master branch and it's still a problem.

Additional context
When attempting to refresh an non-existing token the response should be Unauthorized

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions