diff --git a/common/djangoapps/third_party_auth/saml.py b/common/djangoapps/third_party_auth/saml.py index 483532701512e6e31671329764d3c55e3259bcc1..a475e4924f521f1ebdb32efe5f064e73ec726ee5 100644 --- a/common/djangoapps/third_party_auth/saml.py +++ b/common/djangoapps/third_party_auth/saml.py @@ -249,8 +249,7 @@ class SapSuccessFactorsIdentityProvider(EdXSAMLIdentityProvider): # Define the relationships between SAPSF record fields and Open edX logistration fields. default_field_mapping = { - 'username': 'username', - 'firstName': 'first_name', + 'firstName': ['username', 'first_name'], 'lastName': 'last_name', 'defaultFullName': 'fullname', 'email': 'email', @@ -285,10 +284,14 @@ class SapSuccessFactorsIdentityProvider(EdXSAMLIdentityProvider): field_mapping = self.field_mappings value_defaults = self.conf.get('attr_defaults', {}) value_defaults = {key: value_defaults.get(value, '') for key, value in self.defaults_value_mapping.items()} - registration_fields = { - edx_name: response['d'].get(odata_name, value_defaults.get(odata_name, '')) - for odata_name, edx_name in field_mapping.items() - } + registration_fields = {} + for odata_name, edx_name in field_mapping.items(): + if isinstance(edx_name, list): + for value in edx_name: + registration_fields[value] = response['d'].get(odata_name, value_defaults.get(odata_name, '')) + else: + registration_fields[edx_name] = response['d'].get(odata_name, value_defaults.get(odata_name, '')) + value_mapping = self.value_mappings for field, value in registration_fields.items(): if field in value_mapping and value in value_mapping[field]: @@ -488,6 +491,8 @@ class SapSuccessFactorsIdentityProvider(EdXSAMLIdentityProvider): if self.invalid_configuration(): return basic_details user_id = basic_details['username'] + # endpoint_url is constructed from field_mappings setting of SAML Provider config. + # We convert field_mappings to make comma separated list of the fields which needs to be pulled from BizX fields = ','.join(self.field_mappings) endpoint_url = '{root_url}User(userId=\'{user_id}\')?$select={fields}'.format( root_url=self.odata_api_root_url, @@ -497,9 +502,7 @@ class SapSuccessFactorsIdentityProvider(EdXSAMLIdentityProvider): client = self.get_bizx_odata_api_client(user_id=user_id) if not client: return basic_details - transaction_data = { - 'token_data': client.token_data - } + try: response = client.get( endpoint_url, @@ -517,14 +520,9 @@ class SapSuccessFactorsIdentityProvider(EdXSAMLIdentityProvider): } self.log_bizx_api_exception(transaction_data, err) return basic_details - registration_fields = self.get_registration_fields(response) - # This statement is here for debugging purposes and should be removed when ENT-1500 is resolved. - if user_id != registration_fields.get('username'): - log.info(u'loggedinuser_id %s is different from BizX username %s', - user_id, - registration_fields.get('username')) - return registration_fields + log.info(u'[THIRD_PARTY_AUTH] BizX Odata response for user [%s] %s', user_id, response) + return self.get_registration_fields(response) def get_saml_idp_choices(): diff --git a/common/djangoapps/third_party_auth/tests/specs/test_testshib.py b/common/djangoapps/third_party_auth/tests/specs/test_testshib.py index be47b9cfd35bc9f419c83b7c2f4b111a7ae6d9dc..8018bc1068a7e2c1f9ccba5a340d5fee1081ba35 100644 --- a/common/djangoapps/third_party_auth/tests/specs/test_testshib.py +++ b/common/djangoapps/third_party_auth/tests/specs/test_testshib.py @@ -375,7 +375,7 @@ class SuccessFactorsIntegrationTest(SamlIntegrationTestUtilities, IntegrationTes # assertion metadata. Rather, they will be fetched from the mocked SAPSuccessFactors API. USER_EMAIL = "john@smith.com" USER_NAME = "John Smith" - USER_USERNAME = "jsmith" + USER_USERNAME = "John" def setUp(self): """ @@ -426,7 +426,7 @@ class SuccessFactorsIntegrationTest(SamlIntegrationTestUtilities, IntegrationTes # Mock the call to the SAP SuccessFactors OData user endpoint ODATA_USER_URL = ( 'http://api.successfactors.com/odata/v2/User(userId=\'myself\')' - '?$select=username,firstName,lastName,defaultFullName,email' + '?$select=firstName,lastName,defaultFullName,email' ) def user_callback(request, _uri, headers): @@ -525,7 +525,7 @@ class SuccessFactorsIntegrationTest(SamlIntegrationTestUtilities, IntegrationTes # Mock the call to the SAP SuccessFactors OData user endpoint ODATA_USER_URL = ( 'http://api.successfactors.com/odata/v2/User(userId=\'myself\')' - '?$select=username,firstName,country,lastName,defaultFullName,email' + '?$select=firstName,country,lastName,defaultFullName,email' ) def user_callback(request, _uri, headers):