Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Handled error in OAuth2ExtraTokenMiddleware when authheader has Bearer with no token-string following up #1502

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,4 @@ Wouter Klein Heerenbrink
Yaroslav Halchenko
Yuri Savin
Miriam Forner
Tuhin Mitra
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security
-->

## [unreleased]
### Fixed
* #1496 Fix error when Bearer token string is empty but preceded by `Bearer` keyword.

## [3.0.1] - 2024-09-07
### Fixed
* #1491 Fix migration error when there are pre-existing Access Tokens.
Expand Down
4 changes: 2 additions & 2 deletions oauth2_provider/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def __init__(self, get_response):

def __call__(self, request):
authheader = request.META.get("HTTP_AUTHORIZATION", "")
if authheader.startswith("Bearer"):
tokenstring = authheader.split()[1]
if authheader.startswith("Bearer") and len(authheader.split(maxsplit=1)) == 2:
tokenstring = authheader.split(maxsplit=1)[1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest not calling split() twice:

Suggested change
if authheader.startswith("Bearer") and len(authheader.split(maxsplit=1)) == 2:
tokenstring = authheader.split(maxsplit=1)[1]
splits = autheader.split(maxsplit=1)
if authheader.startswith("Bearer") and len(splits) == 2:
tokenstring = splits[1]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your suggestion, I will make the changes by today!

AccessToken = get_access_token_model()
try:
token_checksum = hashlib.sha256(tokenstring.encode("utf-8")).hexdigest()
Expand Down
Loading