From 33bcb76a2690b294a44ab6e5e66c44edf7ebad67 Mon Sep 17 00:00:00 2001 From: Ayub khan Date: Thu, 11 Jul 2019 14:35:16 +0500 Subject: [PATCH] INCR-400 python3 compatibility --- .../djangoapps/third_party_auth/decorators.py | 4 ++- common/djangoapps/third_party_auth/lti.py | 5 +++- .../djangoapps/third_party_auth/pipeline.py | 28 +++++++++++-------- .../djangoapps/third_party_auth/provider.py | 2 ++ common/djangoapps/third_party_auth/saml.py | 10 +++---- .../djangoapps/third_party_auth/strategy.py | 4 ++- common/djangoapps/third_party_auth/views.py | 22 ++++++++------- 7 files changed, 44 insertions(+), 31 deletions(-) diff --git a/common/djangoapps/third_party_auth/decorators.py b/common/djangoapps/third_party_auth/decorators.py index c897004a95..d568afc030 100644 --- a/common/djangoapps/third_party_auth/decorators.py +++ b/common/djangoapps/third_party_auth/decorators.py @@ -1,13 +1,15 @@ """ Decorators that can be used to interact with third_party_auth. """ +from __future__ import absolute_import + from functools import wraps from django.conf import settings from django.shortcuts import redirect from django.utils.decorators import available_attrs - from six.moves.urllib.parse import urlencode, urlparse + from third_party_auth.models import LTIProviderConfig from third_party_auth.provider import Registry diff --git a/common/djangoapps/third_party_auth/lti.py b/common/djangoapps/third_party_auth/lti.py index f6bd4a87a0..3408880533 100644 --- a/common/djangoapps/third_party_auth/lti.py +++ b/common/djangoapps/third_party_auth/lti.py @@ -1,10 +1,13 @@ """ Third-party-auth module for Learning Tools Interoperability """ +from __future__ import absolute_import + import calendar import logging import time +import six from django.contrib.auth import REDIRECT_FIELD_NAME from oauthlib.common import Request from oauthlib.oauth1.rfc5849.signature import ( @@ -158,7 +161,7 @@ class LTIAuthBackend(BaseAuth): parameters_string = normalize_parameters(parameters) base_string = construct_base_string(request.http_method, base_uri, parameters_string) - computed_signature = sign_hmac_sha1(base_string, unicode(lti_consumer_secret), '') + computed_signature = sign_hmac_sha1(base_string, six.text_type(lti_consumer_secret), '') submitted_signature = request.oauth_signature data = {parameter_value_pair[0]: parameter_value_pair[1] for parameter_value_pair in parameters} diff --git a/common/djangoapps/third_party_auth/pipeline.py b/common/djangoapps/third_party_auth/pipeline.py index b55b616154..4a06c9b2cb 100644 --- a/common/djangoapps/third_party_auth/pipeline.py +++ b/common/djangoapps/third_party_auth/pipeline.py @@ -57,37 +57,41 @@ rather than spreading them across two functions in the pipeline. See https://python-social-auth.readthedocs.io/en/latest/pipeline.html for more docs. """ +from __future__ import absolute_import + import base64 import hashlib import hmac import json -import urllib from collections import OrderedDict from logging import getLogger from smtplib import SMTPException from uuid import uuid4 +import six +import six.moves.urllib.error # pylint: disable=import-error +import six.moves.urllib.parse # pylint: disable=import-error +import six.moves.urllib.request # pylint: disable=import-error +import social_django from django.conf import settings from django.contrib.auth.models import User from django.core.mail.message import EmailMessage -from django.urls import reverse from django.http import HttpResponseBadRequest from django.shortcuts import redirect -import social_django +from django.urls import reverse from social_core.exceptions import AuthException from social_core.pipeline import partial from social_core.pipeline.social_auth import associate_by_email -from social_core.utils import slugify, module_member +from social_core.utils import module_member, slugify from edxmako.shortcuts import render_to_string - -from util.json_request import JsonResponse -from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers -from openedx.core.djangoapps.user_authn import cookies as user_authn_cookies from lms.djangoapps.verify_student.models import SSOVerification from lms.djangoapps.verify_student.utils import earliest_allowed_verification_date +from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers +from openedx.core.djangoapps.user_authn import cookies as user_authn_cookies from third_party_auth.utils import user_exists from track import segment +from util.json_request import JsonResponse from . import provider @@ -149,7 +153,7 @@ _AUTH_ENTRY_CHOICES = frozenset([ AUTH_ENTRY_ACCOUNT_SETTINGS, AUTH_ENTRY_LOGIN_API, AUTH_ENTRY_REGISTER_API, -] + AUTH_ENTRY_CUSTOM.keys()) +] + list(AUTH_ENTRY_CUSTOM.keys())) USER_FIELDS = ['username', 'email'] @@ -305,7 +309,7 @@ def _get_url(view_name, backend_name, auth_entry=None, redirect_url=None, return u"{url}?{params}".format( url=url, - params=urllib.urlencode(query_params) + params=six.moves.urllib.parse.urlencode(query_params) ) @@ -496,7 +500,7 @@ def redirect_to_custom_form(request, auth_entry, details, kwargs): provider_id = provider.Registry.get_from_pipeline({'backend': backend_name, 'kwargs': kwargs}).provider_id form_info = AUTH_ENTRY_CUSTOM[auth_entry] secret_key = form_info['secret_key'] - if isinstance(secret_key, unicode): + if isinstance(secret_key, six.text_type): secret_key = secret_key.encode('utf-8') custom_form_url = form_info['url'] data_str = json.dumps({ @@ -757,7 +761,7 @@ def user_details_force_sync(auth_entry, strategy, details, user=None, *args, **k if changed: logger.info( u"User [%s] performed SSO through [%s] who synchronizes profile data, and the " - u"following fields were changed: %s", user.username, current_provider.name, changed.keys(), + u"following fields were changed: %s", user.username, current_provider.name, list(changed.keys()), ) # Save changes to user and user.profile models. diff --git a/common/djangoapps/third_party_auth/provider.py b/common/djangoapps/third_party_auth/provider.py index 7ebfd110de..ded07b0352 100644 --- a/common/djangoapps/third_party_auth/provider.py +++ b/common/djangoapps/third_party_auth/provider.py @@ -1,6 +1,8 @@ """ Third-party auth provider configuration API. """ +from __future__ import absolute_import + from django.contrib.sites.models import Site from openedx.core.djangoapps.theming.helpers import get_current_request diff --git a/common/djangoapps/third_party_auth/saml.py b/common/djangoapps/third_party_auth/saml.py index 2773255c10..8fe3194559 100644 --- a/common/djangoapps/third_party_auth/saml.py +++ b/common/djangoapps/third_party_auth/saml.py @@ -1,6 +1,8 @@ """ Slightly customized python-social-auth backend for SAML 2.0 support """ +from __future__ import absolute_import + import logging from copy import deepcopy @@ -9,18 +11,14 @@ from django.contrib.sites.models import Site from django.http import Http404 from django.utils.functional import cached_property from django_countries import countries +from enterprise.models import EnterpriseCustomerIdentityProvider, EnterpriseCustomerUser, PendingEnterpriseCustomerUser from onelogin.saml2.settings import OneLogin_Saml2_Settings from six import text_type from social_core.backends.saml import OID_EDU_PERSON_ENTITLEMENT, SAMLAuth, SAMLIdentityProvider from social_core.exceptions import AuthForbidden -from enterprise.models import ( - EnterpriseCustomerUser, - EnterpriseCustomerIdentityProvider, - PendingEnterpriseCustomerUser -) -from third_party_auth.exceptions import IncorrectConfigurationException from openedx.core.djangoapps.theming.helpers import get_current_request +from third_party_auth.exceptions import IncorrectConfigurationException STANDARD_SAML_PROVIDER_KEY = 'standard_saml_provider' SAP_SUCCESSFACTORS_SAML_KEY = 'sap_success_factors' diff --git a/common/djangoapps/third_party_auth/strategy.py b/common/djangoapps/third_party_auth/strategy.py index af5bb10fbf..1947d056a6 100644 --- a/common/djangoapps/third_party_auth/strategy.py +++ b/common/djangoapps/third_party_auth/strategy.py @@ -3,12 +3,14 @@ A custom Strategy for python-social-auth that allows us to fetch configuration f ConfigurationModels rather than django.settings """ +from __future__ import absolute_import + from social_core.backends.oauth import OAuthAuth from social_django.strategy import DjangoStrategy from .models import OAuth2ProviderConfig -from .pipeline import get as get_pipeline_from_request from .pipeline import AUTH_ENTRY_CUSTOM +from .pipeline import get as get_pipeline_from_request from .provider import Registry diff --git a/common/djangoapps/third_party_auth/views.py b/common/djangoapps/third_party_auth/views.py index b13ff7bda0..438e8ec016 100644 --- a/common/djangoapps/third_party_auth/views.py +++ b/common/djangoapps/third_party_auth/views.py @@ -1,20 +1,22 @@ """ Extra views required for SSO """ -from django.conf import settings -from django.urls import reverse -from django.http import Http404, HttpResponse, HttpResponseNotAllowed, HttpResponseServerError, HttpResponseNotFound -from django.shortcuts import redirect, render -from django.views.generic.base import View -from django.views.decorators.csrf import csrf_exempt -from social_django.utils import load_strategy, load_backend, psa -from social_django.views import complete -from social_core.utils import setting_name +from __future__ import absolute_import +from django.conf import settings +from django.http import Http404, HttpResponse, HttpResponseNotAllowed, HttpResponseNotFound, HttpResponseServerError +from django.shortcuts import redirect, render +from django.urls import reverse +from django.views.decorators.csrf import csrf_exempt +from django.views.generic.base import View +from social_core.utils import setting_name +from social_django.utils import load_backend, load_strategy, psa +from social_django.views import complete + +import third_party_auth from student.helpers import get_next_url_for_login_page from student.models import UserProfile from student.views import compose_and_send_activation_email -import third_party_auth from third_party_auth import pipeline, provider from .models import SAMLConfiguration, SAMLProviderConfig