diff --git a/common/djangoapps/third_party_auth/exceptions.py b/common/djangoapps/third_party_auth/exceptions.py new file mode 100644 index 0000000000..bb0b2c0d5c --- /dev/null +++ b/common/djangoapps/third_party_auth/exceptions.py @@ -0,0 +1,12 @@ +""" +Exceptions for SAML Authentication. +""" +from social_core.exceptions import AuthException + + +class IncorrectConfigurationException(AuthException): + """ + Error caused due to incorrect configuration. + """ + def __str__(self): + return 'There was an error in SAML authentication flow which might be caused by incorrect SAML configuration.' diff --git a/common/djangoapps/third_party_auth/saml.py b/common/djangoapps/third_party_auth/saml.py index 8e5918575a..11281f23ff 100644 --- a/common/djangoapps/third_party_auth/saml.py +++ b/common/djangoapps/third_party_auth/saml.py @@ -19,6 +19,7 @@ from enterprise.models import ( PendingEnterpriseCustomerUser ) +from third_party_auth.exceptions import IncorrectConfigurationException from openedx.core.djangoapps.theming.helpers import get_current_request STANDARD_SAML_PROVIDER_KEY = 'standard_saml_provider' @@ -87,6 +88,21 @@ class SAMLAuthBackend(SAMLAuth): # pylint: disable=abstract-method else: return super(SAMLAuthBackend, self).generate_saml_config() + def get_user_id(self, details, response): + """ + Calling the parent function and handling the exception properly. + """ + try: + return super(SAMLAuthBackend, self).get_user_id(details, response) + except KeyError as ex: + log.warning( + u"Error in SAML authentication flow of IdP '{idp_name}': {message}".format( + message=ex.message, + idp_name=response.get('idp_name') + ) + ) + raise IncorrectConfigurationException(self) + def generate_metadata_xml(self, idp_name=None): # pylint: disable=arguments-differ """ Override of SAMLAuth.generate_metadata_xml to accept an optional idp parameter.