Skip to content

Commit

Permalink
Added metrics to auth class (#91)
Browse files Browse the repository at this point in the history
We are currently in the middle to removing the deprecated oauth2authentication from edx-platform and this is one of the last libraries that uses it. These changes would allow us to see if this class is currently in use or not.
  • Loading branch information
jinder1s authored Feb 11, 2020
1 parent d0ebb58 commit a67ec7e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion organizations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
edx-organizations app initialization module
"""
__version__ = '2.2.0' # pragma: no cover
__version__ = '2.2.1' # pragma: no cover
24 changes: 24 additions & 0 deletions organizations/authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Temp auth class to see how often oauth2authentication class is used in organizations"""
from rest_framework_oauth.authentication import OAuth2Authentication
from edx_django_utils.monitoring import set_custom_metric


class OAuth2AuthenticationDeprecated(OAuth2Authentication):
"""
This child class was added to add new_relic metrics to OAuth2Authentication. This should be very temporary.
"""

def authenticate(self, request):
"""
Returns two-tuple of (user, token) if access token authentication
succeeds, None if the user did not try to authenticate using an access
token, or raises an AuthenticationFailed (HTTP 401) if authentication
fails.
"""
set_custom_metric("OAuth2AuthenticationDeprecated", "Failed")
output = super(OAuth2AuthenticationDeprecated, self).authenticate(request)
if output is None:
set_custom_metric("OAuth2AuthenticationDeprecated", "None")
else:
set_custom_metric("OAuth2AuthenticationDeprecated", "Success")
return output
4 changes: 2 additions & 2 deletions organizations/v0/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework_oauth.authentication import OAuth2Authentication

from organizations.models import Organization
from organizations.permissions import UserIsStaff
from organizations.serializers import OrganizationSerializer
from organizations.authentication import OAuth2AuthenticationDeprecated


class OrganizationsViewSet(mixins.UpdateModelMixin, viewsets.ReadOnlyModelViewSet):
Expand All @@ -26,7 +26,7 @@ class OrganizationsViewSet(mixins.UpdateModelMixin, viewsets.ReadOnlyModelViewSe
queryset = Organization.objects.filter(active=True) # pylint: disable=no-member
serializer_class = OrganizationSerializer
lookup_field = 'short_name'
authentication_classes = (OAuth2Authentication, JwtAuthentication, SessionAuthentication)
authentication_classes = (JwtAuthentication, OAuth2AuthenticationDeprecated, SessionAuthentication)
permission_classes = (IsAuthenticated, UserIsStaff)

def update(self, request, *args, **kwargs):
Expand Down

0 comments on commit a67ec7e

Please sign in to comment.