fix: clean accent characters from username (#28157)

Also cleaned username for Authn MFE while registering using SSO/SAML.

VAN-483
This commit is contained in:
Waheed Ahmed
2021-07-13 20:33:22 +05:00
committed by GitHub
parent a68bfe28b9
commit 74e3b664eb
3 changed files with 6 additions and 2 deletions

View File

@@ -68,7 +68,7 @@ def clean_json(value, of_type):
def clean_username(username=''):
""" Simple helper method to ensure a username is compatible with our system requirements. """
return re.sub(r'[^-\w]+', '_', username)[:USERNAME_MAX_LENGTH]
return ('_').join(re.findall(r'[a-zA-Z0-9\-]+', username))[:USERNAME_MAX_LENGTH]
class AuthNotConfigured(SocialAuthBaseException):

View File

@@ -75,7 +75,7 @@ class PipelineOverridesTest(SamlIntegrationTestUtilities, IntegrationTestMixin,
('S', 'S9fe2', False),
('S', 'S9fe2', True),
('S.K', 'S_K', False),
('S.K.', 'S_K_', False),
('S.K.', 'S_K', False),
('S.K.', 'S_K_9fe2', True),
('usernamewithcharacterlengthofmorethan30chars', 'usernamewithcharacterlengthofm', False),
('usernamewithcharacterlengthofmorethan30chars', 'usernamewithcharacterlengt9fe2', True),

View File

@@ -8,6 +8,7 @@ from ipware.ip import get_client_ip
from common.djangoapps import third_party_auth
from common.djangoapps.third_party_auth import pipeline
from common.djangoapps.third_party_auth.models import clean_username
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
from openedx.core.djangoapps.geoinfo.api import country_code_from_ip
@@ -70,6 +71,9 @@ def third_party_auth_context(request, redirect_to, tpa_hint=None):
current_provider = third_party_auth.provider.Registry.get_from_pipeline(running_pipeline)
user_details = running_pipeline['kwargs']['details']
if user_details:
username = running_pipeline['kwargs'].get('username') or user_details.get('username')
if username:
user_details['username'] = clean_username(username)
context['pipeline_user_details'] = user_details
if current_provider is not None: