try-except auth_header

This commit is contained in:
adamscmRH
2018-06-21 16:29:40 -04:00
parent 01a1d6ffd0
commit b7f5161835
2 changed files with 16 additions and 16 deletions

View File

@@ -46,17 +46,14 @@ class SessionAuthentication(authentication.SessionAuthentication):
class LoggedOAuth2Authentication(OAuth2Authentication): class LoggedOAuth2Authentication(OAuth2Authentication):
def authenticate(self, request): def authenticate(self, request):
if 'Bearer' in request.META['HTTP_AUTHORIZATION']:
ret = super(LoggedOAuth2Authentication, self).authenticate(request) ret = super(LoggedOAuth2Authentication, self).authenticate(request)
if ret: if ret:
user, token = ret user, token = ret
username = user.username if user else '<none>' username = user.username if user else '<none>'
logger.debug(smart_text( logger.info(smart_text(
u"User {} performed a {} to {} through the API using OAuth token {}.".format( u"User {} performed a {} to {} through the API using OAuth token {}.".format(
username, request.method, request.path, token.pk username, request.method, request.path, token.pk
) )
)) ))
setattr(user, 'oauth_scopes', [x for x in token.scope.split() if x]) setattr(user, 'oauth_scopes', [x for x in token.scope.split() if x])
return ret return ret
else:
return None

View File

@@ -234,9 +234,12 @@ class APIView(views.APIView):
was attempted. was attempted.
""" """
for authenticator in self.get_authenticators(): for authenticator in self.get_authenticators():
try:
resp_hdr = authenticator.authenticate_header(request) resp_hdr = authenticator.authenticate_header(request)
if not resp_hdr: if not resp_hdr:
continue continue
except AttributeError:
continue
req_hdr = get_authorization_header(request) req_hdr = get_authorization_header(request)
if not req_hdr: if not req_hdr:
continue continue