Skip to content

Commit

Permalink
Merge pull request #52 from Medality-Health/pce-poc
Browse files Browse the repository at this point in the history
Changes needed for Premiere CE OAuth Integration
  • Loading branch information
tramck authored Oct 31, 2024
2 parents 89bbeb1 + 5a7b25b commit 47f0a89
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
3 changes: 2 additions & 1 deletion common/djangoapps/entitlements/rest_api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class CourseEntitlementSupportDetailSerializer(serializers.ModelSerializer):
slug_field='username',
default=serializers.CurrentUserDefault()
)
unenrolled_run = CourseKeyField('unenrolled_run.id')
# @medality_custom: this was a fix to a syntax error
unenrolled_run = CourseKeyField(source='unenrolled_run.id')

class Meta:
model = CourseEntitlementSupportDetail
Expand Down
6 changes: 4 additions & 2 deletions openedx/core/djangoapps/oauth_dispatch/adapters/dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ def get_access_token(self, token_string):
"""
Given a token string, return the matching AccessToken object.
"""
return models.AccessToken.objects.get(token=token_string)
# @medality_custom
return models.get_access_token_model().objects.get(token=token_string)

def create_access_token_for_test(self, token_string, client, user, expires):
"""
Returns a new AccessToken object created from the given arguments.
This method is currently used only by tests.
"""
return models.AccessToken.objects.create(
# @medality_custom
return models.get_access_token_model().objects.create(
token=token_string,
application=client,
user=user,
Expand Down
4 changes: 2 additions & 2 deletions openedx/core/djangoapps/oauth_dispatch/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def decorator(cls):

return decorator


@reregister(models.AccessToken)
# @medality_custom
@reregister(models.get_access_token_model())
class DOTAccessTokenAdmin(ModelAdmin):
"""
Custom AccessToken Admin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from django.contrib.auth import authenticate, get_user_model
from django.db.models.signals import pre_save
from django.dispatch import receiver
from oauth2_provider.models import AccessToken
# @medality_custom
from oauth2_provider import models
from oauth2_provider.oauth2_validators import OAuth2Validator
from oauth2_provider.scopes import get_scopes_backend
from pytz import utc
Expand All @@ -17,7 +18,8 @@
# pylint: disable=W0223


@receiver(pre_save, sender=AccessToken)
# @medality_custom
@receiver(pre_save, sender=models.get_access_token_model())
def on_access_token_presave(sender, instance, *args, **kwargs): # pylint: disable=unused-argument
"""
Mark AccessTokens as expired for 'restricted applications' if required.
Expand Down Expand Up @@ -108,7 +110,8 @@ def _update_token_expiry_if_restricted_client(self, token, client):
# and calculate expires_in (in seconds) from the database value. This
# value should be a negative value, meaning that it is already expired.
if RestrictedApplication.should_expire_access_token(client):
access_token = AccessToken.objects.get(token=token['access_token'])
# @medality_custom
access_token = models.get_access_token_model().objects.get(token=token['access_token'])
expires_in = (access_token.expires - _get_utc_now()).total_seconds()
assert expires_in < 0
token['expires_in'] = expires_in
Expand All @@ -126,7 +129,8 @@ def _update_token_expiry_if_overridden_in_request(self, token, request):
"""
expires_in = getattr(request, 'expires_in', None)
if expires_in:
access_token = AccessToken.objects.get(token=token['access_token'])
# @medality_custom
access_token = models.get_access_token_model().objects.get(token=token['access_token'])
access_token.expires = _get_utc_now() + timedelta(seconds=expires_in)
access_token.save()
token['expires_in'] = expires_in
Expand Down
5 changes: 4 additions & 1 deletion openedx/core/djangoapps/user_authn/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from django.utils.http import http_date, parse_http_date
from edx_rest_framework_extensions.auth.jwt import cookies as jwt_cookies
from edx_rest_framework_extensions.auth.jwt.constants import JWT_DELIMITER
from oauth2_provider.models import Application
# @medality_custom
from oauth2_provider import models as oauth_models
from common.djangoapps.student.models import UserProfile

from openedx.core.djangoapps.oauth_dispatch.adapters import DOTAdapter
Expand Down Expand Up @@ -354,6 +355,8 @@ def _get_login_oauth_client():
Returns the configured OAuth Client/Application used for Login.
"""
login_client_id = settings.JWT_AUTH['JWT_LOGIN_CLIENT_ID']
# @medality_custom
Application = oauth_models.get_application_model()
try:
return Application.objects.get(client_id=login_client_id)
except Application.DoesNotExist:
Expand Down
12 changes: 10 additions & 2 deletions openedx/core/lib/api/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ def authenticate_credentials(self, access_token):
})
else:
user = token.user
has_application = dot_models.Application.objects.filter(user_id=user.id)
# @medality_custom start
if not token.is_valid():
raise AuthenticationFailed({
'error_code': OAUTH2_TOKEN_ERROR,
'developer_message': 'The provided access token is not valid.'
})
has_application = dot_models.get_application_model().objects.filter(user_id=user.id)
# @medality_custom end
if not user.has_usable_password() and not has_application:
msg = 'User disabled by admin: %s' % user.get_username()
raise AuthenticationFailed({
Expand All @@ -116,7 +123,8 @@ def get_access_token(self, access_token):
Return a valid access token stored by django-oauth-toolkit (DOT), or
None if no matching token is found.
"""
token_query = dot_models.AccessToken.objects.select_related('user')
# @medality_custom
token_query = dot_models.get_access_token_model().objects.select_related('user')
return token_query.filter(token=access_token).first()

def authenticate_header(self, request):
Expand Down

0 comments on commit 47f0a89

Please sign in to comment.