Skip to content

Commit

Permalink
Added new relic metrics to bearerAuthentication
Browse files Browse the repository at this point in the history
  • Loading branch information
jinder1s authored and jinder1s committed Feb 3, 2020
1 parent e7f562f commit 5ffd36f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
10 changes: 8 additions & 2 deletions edx_rest_framework_extensions/auth/bearer/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import requests
from django.contrib.auth import get_user_model
from edx_django_utils.monitoring import set_custom_metric
from rest_framework import exceptions
from rest_framework.authentication import BaseAuthentication, get_authorization_header

Expand Down Expand Up @@ -36,21 +37,26 @@ def get_user_info_url(self):
return get_setting('OAUTH2_USER_INFO_URL')

def authenticate(self, request):
set_custom_metric("BearerAuthentication", "Failed") # default value
if not self.get_user_info_url():
logger.warning('The setting OAUTH2_USER_INFO_URL is invalid!')
set_custom_metric("BearerAuthentication", "NoURL")
return None

set_custom_metric("BearerAuthentication_user_info_url", self.get_user_info_url())
auth = get_authorization_header(request).split()

if not auth or auth[0].lower() != b'bearer':
set_custom_metric("BearerAuthentication", "None")
return None

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

return self.authenticate_credentials(auth[1].decode('utf8'))
output = self.authenticate_credentials(auth[1].decode('utf8'))
set_custom_metric("BearerAuthentication", "Success")
return output

def authenticate_credentials(self, token):
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pyjwt==1.7.1
pylint-celery==0.3
pylint-django==0.11.1
pylint-plugin-utils==0.6
pylint==1.9.5
pylint==1.9.4
pymongo==3.9.0
pyparsing==2.4.5
pytest-cov==2.8.1
Expand Down
2 changes: 1 addition & 1 deletion requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pyjwt==1.7.1
pylint-celery==0.3 # via edx-lint
pylint-django==0.11.1 # via edx-lint
pylint-plugin-utils==0.6 # via pylint-celery, pylint-django
pylint==1.9.5 # via edx-lint, pylint-celery, pylint-django, pylint-plugin-utils
pylint==1.9.4 # via edx-lint, pylint-celery, pylint-django, pylint-plugin-utils
pymongo==3.9.0
pyparsing==2.4.5 # via packaging
pytest-cov==2.8.1
Expand Down

0 comments on commit 5ffd36f

Please sign in to comment.