Commit d5c85331 by Uman Shahzad

Automatically populate additional fields for SSO scenarios.

When authenticating using an SAML IdP, gather additional user
data besides what is standard. Requires admin to input JSON
in settings to recognize the additional user data.
parent 9ecc2938
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('third_party_auth', '0010_add_skip_hinted_login_dialog_field'),
]
operations = [
migrations.AlterField(
model_name='samlproviderconfig',
name='other_settings',
field=models.TextField(help_text=b'For advanced use cases, enter a JSON object with addtional configuration. The tpa-saml backend supports {"requiredEntitlements": ["urn:..."]}, which can be used to require the presence of a specific eduPersonEntitlement, and {"extra_field_definitions": [{"name": "...", "urn": "..."},...]}, which can be used to define registration form fields and the URNs that can be used to retrieve the relevant values from the SAML response. Custom provider types, as selected in the "Identity Provider Type" field, may make use of the information stored in this field for additional configuration.', verbose_name=b'Advanced settings', blank=True),
),
]
...@@ -31,6 +31,11 @@ from .saml import STANDARD_SAML_PROVIDER_KEY, get_saml_idp_choices, get_saml_idp ...@@ -31,6 +31,11 @@ from .saml import STANDARD_SAML_PROVIDER_KEY, get_saml_idp_choices, get_saml_idp
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
REGISTRATION_FORM_FIELD_BLACKLIST = [
'name',
'username'
]
# A dictionary of {name: class} entries for each python-social-auth backend available. # A dictionary of {name: class} entries for each python-social-auth backend available.
# Because this setting can specify arbitrary code to load and execute, it is set via # Because this setting can specify arbitrary code to load and execute, it is set via
...@@ -241,8 +246,13 @@ class ProviderConfig(ConfigurationModel): ...@@ -241,8 +246,13 @@ class ProviderConfig(ConfigurationModel):
values for that field. Where there is no value, the empty string values for that field. Where there is no value, the empty string
must be used. must be used.
""" """
registration_form_data = {}
# Details about the user sent back from the provider. # Details about the user sent back from the provider.
details = pipeline_kwargs.get('details') details = pipeline_kwargs.get('details').copy()
# Set the registration form to use the `fullname` detail for the `name` field.
registration_form_data['name'] = details.get('fullname', '')
# Get the username separately to take advantage of the de-duping logic # Get the username separately to take advantage of the de-duping logic
# built into the pipeline. The provider cannot de-dupe because it can't # built into the pipeline. The provider cannot de-dupe because it can't
...@@ -250,13 +260,19 @@ class ProviderConfig(ConfigurationModel): ...@@ -250,13 +260,19 @@ class ProviderConfig(ConfigurationModel):
# technically a data race between the creation of this value and the # technically a data race between the creation of this value and the
# creation of the user object, so it is still possible for users to get # creation of the user object, so it is still possible for users to get
# an error on submit. # an error on submit.
suggested_username = pipeline_kwargs.get('username') registration_form_data['username'] = pipeline_kwargs.get('username')
return { # Any other values that are present in the details dict should be copied
'email': details.get('email', ''), # into the registration form details. This may include details that do
'name': details.get('fullname', ''), # not map to a value that exists in the registration form. However,
'username': suggested_username, # because the fields that are actually rendered are not based on this
} # list, only those values that map to a valid registration form field
# will actually be sent to the form as default values.
for blacklisted_field in REGISTRATION_FORM_FIELD_BLACKLIST:
details.pop(blacklisted_field, None)
registration_form_data.update(details)
return registration_form_data
def get_authentication_backend(self): def get_authentication_backend(self):
"""Gets associated Django settings.AUTHENTICATION_BACKEND string.""" """Gets associated Django settings.AUTHENTICATION_BACKEND string."""
...@@ -401,10 +417,13 @@ class SAMLProviderConfig(ProviderConfig): ...@@ -401,10 +417,13 @@ class SAMLProviderConfig(ProviderConfig):
verbose_name="Advanced settings", blank=True, verbose_name="Advanced settings", blank=True,
help_text=( help_text=(
'For advanced use cases, enter a JSON object with addtional configuration. ' 'For advanced use cases, enter a JSON object with addtional configuration. '
'The tpa-saml backend supports only {"requiredEntitlements": ["urn:..."]} ' 'The tpa-saml backend supports {"requiredEntitlements": ["urn:..."]}, '
'which can be used to require the presence of a specific eduPersonEntitlement. ' 'which can be used to require the presence of a specific eduPersonEntitlement, '
'Custom provider types, as selected in the "Identity Provider Type" field, may make ' 'and {"extra_field_definitions": [{"name": "...", "urn": "..."},...]}, which can be '
'use of the information stored in this field for configuration.' 'used to define registration form fields and the URNs that can be used to retrieve '
'the relevant values from the SAML response. Custom provider types, as selected '
'in the "Identity Provider Type" field, may make use of the information stored '
'in this field for additional configuration.'
)) ))
def clean(self): def clean(self):
......
...@@ -100,9 +100,29 @@ class SAMLAuthBackend(SAMLAuth): # pylint: disable=abstract-method ...@@ -100,9 +100,29 @@ class SAMLAuthBackend(SAMLAuth): # pylint: disable=abstract-method
return SAMLConfiguration.current(Site.objects.get_current(get_current_request())) return SAMLConfiguration.current(Site.objects.get_current(get_current_request()))
class SapSuccessFactorsIdentityProvider(SAMLIdentityProvider): class EdXSAMLIdentityProvider(SAMLIdentityProvider):
""" """
Customized version of SAMLIdentityProvider that knows how to retrieve user details Customized version of SAMLIdentityProvider that can retrieve details beyond the standard
details supported by the canonical upstream version.
"""
def get_user_details(self, attributes):
"""
Overrides `get_user_details` from the base class; retrieves those details,
then updates the dict with values from whatever additional fields are desired.
"""
details = super(EdXSAMLIdentityProvider, self).get_user_details(attributes)
extra_field_definitions = self.conf.get('extra_field_definitions', [])
details.update({
field['name']: attributes[field['urn']][0] if field['urn'] in attributes else None
for field in extra_field_definitions
})
return details
class SapSuccessFactorsIdentityProvider(EdXSAMLIdentityProvider):
"""
Customized version of EdXSAMLIdentityProvider that knows how to retrieve user details
from the SAPSuccessFactors OData API, rather than parse them directly off the from the SAPSuccessFactors OData API, rather than parse them directly off the
SAML assertion that we get in response to a login attempt. SAML assertion that we get in response to a login attempt.
""" """
...@@ -244,12 +264,12 @@ def get_saml_idp_class(idp_identifier_string): ...@@ -244,12 +264,12 @@ def get_saml_idp_class(idp_identifier_string):
the SAMLIdentityProvider subclass able to handle requests for that type of identity provider. the SAMLIdentityProvider subclass able to handle requests for that type of identity provider.
""" """
choices = { choices = {
STANDARD_SAML_PROVIDER_KEY: SAMLIdentityProvider, STANDARD_SAML_PROVIDER_KEY: EdXSAMLIdentityProvider,
SAP_SUCCESSFACTORS_SAML_KEY: SapSuccessFactorsIdentityProvider, SAP_SUCCESSFACTORS_SAML_KEY: SapSuccessFactorsIdentityProvider,
} }
if idp_identifier_string not in choices: if idp_identifier_string not in choices:
log.error( log.error(
'%s is not a valid SAMLIdentityProvider subclass; using SAMLIdentityProvider base class.', '%s is not a valid EdXSAMLIdentityProvider subclass; using EdXSAMLIdentityProvider base class.',
idp_identifier_string idp_identifier_string
) )
return choices.get(idp_identifier_string, SAMLIdentityProvider) return choices.get(idp_identifier_string, EdXSAMLIdentityProvider)
...@@ -251,7 +251,7 @@ class IntegrationTest(testutil.TestCase, test.TestCase): ...@@ -251,7 +251,7 @@ class IntegrationTest(testutil.TestCase, test.TestCase):
self.assertEqual(302, response.status_code) self.assertEqual(302, response.status_code)
self.assertTrue(response.has_header('Location')) self.assertTrue(response.has_header('Location'))
def assert_register_response_in_pipeline_looks_correct(self, response, pipeline_kwargs): def assert_register_response_in_pipeline_looks_correct(self, response, pipeline_kwargs, required_fields):
"""Performs spot checks of the rendered register.html page. """Performs spot checks of the rendered register.html page.
When we display the new account registration form after the user signs When we display the new account registration form after the user signs
...@@ -267,9 +267,10 @@ class IntegrationTest(testutil.TestCase, test.TestCase): ...@@ -267,9 +267,10 @@ class IntegrationTest(testutil.TestCase, test.TestCase):
self.assertIn('successfully signed in with <strong>%s</strong>' % self.provider.name, response.content) self.assertIn('successfully signed in with <strong>%s</strong>' % self.provider.name, response.content)
# Expect that each truthy value we've prepopulated the register form # Expect that each truthy value we've prepopulated the register form
# with is actually present. # with is actually present.
for prepopulated_form_value in self.provider.get_register_form_data(pipeline_kwargs).values(): form_field_data = self.provider.get_register_form_data(pipeline_kwargs)
if prepopulated_form_value: for prepopulated_form_data in form_field_data:
self.assertIn(prepopulated_form_value, response.content) if prepopulated_form_data in required_fields:
self.assertIn(form_field_data[prepopulated_form_data], response.content)
# Implementation details and actual tests past this point -- no more # Implementation details and actual tests past this point -- no more
# configuration needed. # configuration needed.
...@@ -823,7 +824,10 @@ class IntegrationTest(testutil.TestCase, test.TestCase): ...@@ -823,7 +824,10 @@ class IntegrationTest(testutil.TestCase, test.TestCase):
# fire off the view that displays the registration form. # fire off the view that displays the registration form.
with self._patch_edxmako_current_request(request): with self._patch_edxmako_current_request(request):
self.assert_register_response_in_pipeline_looks_correct( self.assert_register_response_in_pipeline_looks_correct(
student_views.register_user(strategy.request), pipeline.get(request)['kwargs']) student_views.register_user(strategy.request),
pipeline.get(request)['kwargs'],
['name', 'username', 'email']
)
# Next, we invoke the view that handles the POST. Not all providers # Next, we invoke the view that handles the POST. Not all providers
# supply email. Manually add it as the user would have to; this # supply email. Manually add it as the user would have to; this
...@@ -892,7 +896,10 @@ class IntegrationTest(testutil.TestCase, test.TestCase): ...@@ -892,7 +896,10 @@ class IntegrationTest(testutil.TestCase, test.TestCase):
with self._patch_edxmako_current_request(request): with self._patch_edxmako_current_request(request):
self.assert_register_response_in_pipeline_looks_correct( self.assert_register_response_in_pipeline_looks_correct(
student_views.register_user(strategy.request), pipeline.get(request)['kwargs']) student_views.register_user(strategy.request),
pipeline.get(request)['kwargs'],
['name', 'username', 'email']
)
with self._patch_edxmako_current_request(strategy.request): with self._patch_edxmako_current_request(strategy.request):
strategy.request.POST = self.get_registration_post_vars() strategy.request.POST = self.get_registration_post_vars()
......
...@@ -25,7 +25,7 @@ from third_party_auth.models import ( ...@@ -25,7 +25,7 @@ from third_party_auth.models import (
SAMLConfiguration, SAMLConfiguration,
SAMLProviderConfig SAMLProviderConfig
) )
from third_party_auth.saml import SAMLIdentityProvider, get_saml_idp_class from third_party_auth.saml import EdXSAMLIdentityProvider, get_saml_idp_class
AUTH_FEATURES_KEY = 'ENABLE_THIRD_PARTY_AUTH' AUTH_FEATURES_KEY = 'ENABLE_THIRD_PARTY_AUTH'
AUTH_FEATURE_ENABLED = AUTH_FEATURES_KEY in settings.FEATURES AUTH_FEATURE_ENABLED = AUTH_FEATURES_KEY in settings.FEATURES
...@@ -219,14 +219,14 @@ class SAMLTestCase(TestCase): ...@@ -219,14 +219,14 @@ class SAMLTestCase(TestCase):
error_mock = log_mock.error error_mock = log_mock.error
idp_class = get_saml_idp_class('fake_idp_class_option') idp_class = get_saml_idp_class('fake_idp_class_option')
error_mock.assert_called_once_with( error_mock.assert_called_once_with(
'%s is not a valid SAMLIdentityProvider subclass; using SAMLIdentityProvider base class.', '%s is not a valid EdXSAMLIdentityProvider subclass; using EdXSAMLIdentityProvider base class.',
'fake_idp_class_option' 'fake_idp_class_option'
) )
self.assertIs(idp_class, SAMLIdentityProvider) self.assertIs(idp_class, EdXSAMLIdentityProvider)
@contextmanager @contextmanager
def simulate_running_pipeline(pipeline_target, backend, email=None, fullname=None, username=None): def simulate_running_pipeline(pipeline_target, backend, email=None, fullname=None, username=None, **kwargs):
"""Simulate that a pipeline is currently running. """Simulate that a pipeline is currently running.
You can use this context manager to test packages that rely on third party auth. You can use this context manager to test packages that rely on third party auth.
...@@ -269,6 +269,9 @@ def simulate_running_pipeline(pipeline_target, backend, email=None, fullname=Non ...@@ -269,6 +269,9 @@ def simulate_running_pipeline(pipeline_target, backend, email=None, fullname=Non
app generates itself and should be available by the time the user app generates itself and should be available by the time the user
is authenticating with a third-party provider. is authenticating with a third-party provider.
kwargs (dict): If provided, simulate that the current provider has
included additional user details (useful for filling in the registration form).
Returns: Returns:
None None
...@@ -276,9 +279,10 @@ def simulate_running_pipeline(pipeline_target, backend, email=None, fullname=Non ...@@ -276,9 +279,10 @@ def simulate_running_pipeline(pipeline_target, backend, email=None, fullname=Non
pipeline_data = { pipeline_data = {
"backend": backend, "backend": backend,
"kwargs": { "kwargs": {
"details": {} "details": kwargs
} }
} }
if email is not None: if email is not None:
pipeline_data["kwargs"]["details"]["email"] = email pipeline_data["kwargs"]["details"]["email"] = email
if fullname is not None: if fullname is not None:
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
} %> } %>
<% if ( required ) { %> aria-required="true" required<% } %>> <% if ( required ) { %> aria-required="true" required<% } %>>
<% _.each(options, function(el) { %> <% _.each(options, function(el) { %>
<option value="<%= el.value%>"<% if ( el.default ) { %> data-isdefault="true"<% } %>><%= el.name %></option> <option value="<%= el.value%>"<% if ( el.default ) { %> data-isdefault="true" selected<% } %>><%= el.name %></option>
<% }); %> <% }); %>
</select> </select>
<% if ( instructions ) { %> <span class="tip tip-input" id="<%= form %>-<%= name %>-desc"><%= instructions %></span><% } %> <% if ( instructions ) { %> <span class="tip tip-input" id="<%= form %>-<%= name %>-desc"><%= instructions %></span><% } %>
......
...@@ -234,21 +234,29 @@ class FormDescription(object): ...@@ -234,21 +234,29 @@ class FormDescription(object):
"supplementalText": supplementalText "supplementalText": supplementalText
} }
field_override = self._field_overrides.get(name, {})
if field_type == "select": if field_type == "select":
if options is not None: if options is not None:
field_dict["options"] = [] field_dict["options"] = []
# Include an empty "default" option at the beginning of the list # Get an existing default value from the field override
existing_default_value = field_override.get('defaultValue')
# Include an empty "default" option at the beginning of the list;
# preselect it if there isn't an overriding default.
if include_default_option: if include_default_option:
field_dict["options"].append({ field_dict["options"].append({
"value": "", "value": "",
"name": "--", "name": "--",
"default": True "default": existing_default_value is None
}) })
field_dict["options"].extend([ field_dict["options"].extend([
{"value": option_value, "name": option_name} {
for option_value, option_name in options 'value': option_value,
'name': option_name,
'default': option_value == existing_default_value
} for option_value, option_name in options
]) ])
else: else:
raise InvalidFieldError("You must provide options for a select field.") raise InvalidFieldError("You must provide options for a select field.")
...@@ -270,7 +278,7 @@ class FormDescription(object): ...@@ -270,7 +278,7 @@ class FormDescription(object):
# If there are overrides for this field, apply them now. # If there are overrides for this field, apply them now.
# Any field property can be overwritten (for example, the default value or placeholder) # Any field property can be overwritten (for example, the default value or placeholder)
field_dict.update(self._field_overrides.get(name, {})) field_dict.update(field_override)
self.fields.append(field_dict) self.fields.append(field_dict)
...@@ -291,8 +299,8 @@ class FormDescription(object): ...@@ -291,8 +299,8 @@ class FormDescription(object):
"placeholder": "", "placeholder": "",
"instructions": "", "instructions": "",
"options": [ "options": [
{"value": "cheese", "name": "Cheese"}, {"value": "cheese", "name": "Cheese", "default": False},
{"value": "wine", "name": "Wine"} {"value": "wine", "name": "Wine", "default": False}
] ]
"restrictions": {}, "restrictions": {},
"errorMessages": {}, "errorMessages": {},
......
...@@ -147,6 +147,44 @@ class FormDescriptionTest(TestCase): ...@@ -147,6 +147,44 @@ class FormDescriptionTest(TestCase):
with self.assertRaises(InvalidFieldError): with self.assertRaises(InvalidFieldError):
desc.add_field("name", field_type="text", restrictions={"invalid": 0}) desc.add_field("name", field_type="text", restrictions={"invalid": 0})
def test_option_overrides(self):
desc = FormDescription("post", "/submit")
field = {
"name": "country",
"label": "Country",
"field_type": "select",
"default": "PK",
"required": True,
"error_messages": {
"required": "You must provide a value!"
},
"options": [
("US", "United States of America"),
("PK", "Pakistan")
]
}
desc.override_field_properties(
field["name"],
default="PK"
)
desc.add_field(**field)
self.assertEqual(
desc.fields[0]["options"],
[
{
'default': False,
'name': 'United States of America',
'value': 'US'
},
{
'default': True,
'name': 'Pakistan',
'value': 'PK'
}
]
)
@ddt.ddt @ddt.ddt
class StudentViewShimTest(TestCase): class StudentViewShimTest(TestCase):
......
...@@ -924,7 +924,7 @@ class RegistrationView(APIView): ...@@ -924,7 +924,7 @@ class RegistrationView(APIView):
running_pipeline.get('kwargs') running_pipeline.get('kwargs')
) )
for field_name in self.DEFAULT_FIELDS: for field_name in self.DEFAULT_FIELDS + self.EXTRA_FIELDS:
if field_name in field_overrides: if field_name in field_overrides:
form_desc.override_field_properties( form_desc.override_field_properties(
field_name, default=field_overrides[field_name] field_name, default=field_overrides[field_name]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment