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

Fix auth api issue - #131967 #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions ninetofiver/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ class ApiKeyAuthentication(BaseTokenAuthentication):

model = models.ApiKey

def authenticate(self, request):
"""Authenticate the request."""
token = request.GET.get('api_key', None)

if not token:
auth = get_authorization_header(request).split()

if auth and auth[0].lower() == self.keyword.lower().encode():
if len(auth) == 1:
msg = _('Invalid token header. No credentials provided.')
raise exceptions.AuthenticationFailed(msg)
elif len(auth) > 2:
msg = _('Invalid token header. Token string should not contain spaces.')
raise exceptions.AuthenticationFailed(msg)

try:
token = auth[1].decode()
except UnicodeError:
msg = _('Invalid token header. Token string should not contain invalid characters.')
raise exceptions.AuthenticationFailed(msg)

if not token:
msg = _('Invalid token. No credentials provided.')
def authenticate(self, request):

# removed the following to prevent the use of query parameters for api_key
# token = request.GET.get('api_key', None)

"""Authenticate the request."""
auth = get_authorization_header(request).split()

if not auth or auth[0].lower() != self.keyword.lower().encode():
msg = _('Invalid token header. No credentials provided.')
raise exceptions.AuthenticationFailed(msg)

if len(auth) == 1:
msg = _('Invalid token header. No credentials provided.')
raise exceptions.AuthenticationFailed(msg)
elif len(auth) > 2:
msg = _('Invalid token header. Token string should not contain spaces.')
raise exceptions.AuthenticationFailed(msg)

try:
token = auth[1].decode()
except UnicodeError:
msg = _('Invalid token header. Token string should not contain invalid characters.')
raise exceptions.AuthenticationFailed(msg)

res = self.authenticate_credentials(token)
Expand All @@ -42,4 +42,4 @@ def authenticate(self, request):
msg = _('The token provided is only valid for read-only requests.')
raise exceptions.AuthenticationFailed(msg)

return res
return res