|
|
@ -15,26 +15,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import logging
|
|
|
|
import unittest
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
|
|
from . import create_mapping_provider
|
|
|
|
from . import create_mapping_provider
|
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig()
|
|
|
|
logging.basicConfig()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _make_test_saml_response(
|
|
|
|
|
|
|
|
provider_config: dict,
|
|
|
|
|
|
|
|
source_attribute_value: str,
|
|
|
|
|
|
|
|
display_name: Optional[str] = None
|
|
|
|
|
|
|
|
):
|
|
|
|
|
|
|
|
"""Create a fake object based off of saml2.response.AuthnResponse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
|
|
provider_config: The config dictionary used when creating the provider object
|
|
|
|
|
|
|
|
source_attribute_value: The desired value that the mapping provider will
|
|
|
|
|
|
|
|
pull out of the response object to turn into a Matrix UserID.
|
|
|
|
|
|
|
|
display_name: The desired displayname that the mapping provider will pull
|
|
|
|
|
|
|
|
out of the response object to turn into a Matrix user displayname.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
An object masquerading as a saml2.response.AuthnResponse object
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FakeResponse(object):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
self.ava = {
|
|
|
|
|
|
|
|
provider_config["mxid_source_attribute"]: [source_attribute_value],
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if display_name:
|
|
|
|
|
|
|
|
self.ava["displayName"] = display_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return FakeResponse()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SamlUsernameTestCase(unittest.TestCase):
|
|
|
|
class SamlUsernameTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
|
|
def test_normal_user(self):
|
|
|
|
def test_normal_user(self):
|
|
|
|
provider = create_mapping_provider()
|
|
|
|
provider, config = create_mapping_provider()
|
|
|
|
|
|
|
|
response = _make_test_saml_response(config, "john*doe2000#@example.com", None)
|
|
|
|
|
|
|
|
|
|
|
|
username = "john*doe2000#@example.com"
|
|
|
|
attribute_dict = provider.saml_response_to_user_attributes(response)
|
|
|
|
localpart = provider.mxid_source_to_mxid_localpart(username)
|
|
|
|
self.assertEqual(attribute_dict["mxid_localpart"], "john.doe2000")
|
|
|
|
self.assertEqual(localpart, "john.doe2000")
|
|
|
|
self.assertEqual(attribute_dict["displayname"], "john.doe2000")
|
|
|
|
|
|
|
|
|
|
|
|
def test_multiple_adjacent_symbols(self):
|
|
|
|
def test_multiple_adjacent_symbols(self):
|
|
|
|
provider = create_mapping_provider()
|
|
|
|
provider = create_mapping_provider()
|
|
|
|
|
|
|
|
|
|
|
|
username = "bob%^$&#!bobby@example.com"
|
|
|
|
username = "bob%^$&#!bobby@example.com"
|
|
|
|
localpart = provider.mxid_source_to_mxid_localpart(username)
|
|
|
|
localpart = provider.saml_response_to_user_attributes(username)
|
|
|
|
self.assertEqual(localpart, "bob.bobby")
|
|
|
|
self.assertEqual(localpart, "bob.bobby")
|
|
|
|
|
|
|
|
|
|
|
|
def test_username_does_not_end_with_dot(self):
|
|
|
|
def test_username_does_not_end_with_dot(self):
|
|
|
@ -42,29 +75,29 @@ class SamlUsernameTestCase(unittest.TestCase):
|
|
|
|
provider = create_mapping_provider()
|
|
|
|
provider = create_mapping_provider()
|
|
|
|
|
|
|
|
|
|
|
|
username = "bob.bobby$@example.com"
|
|
|
|
username = "bob.bobby$@example.com"
|
|
|
|
localpart = provider.mxid_source_to_mxid_localpart(username)
|
|
|
|
localpart = provider.saml_response_to_user_attributes(username)
|
|
|
|
self.assertEqual(localpart, "bob.bobby")
|
|
|
|
self.assertEqual(localpart, "bob.bobby")
|
|
|
|
|
|
|
|
|
|
|
|
def test_username_no_email(self):
|
|
|
|
def test_username_no_email(self):
|
|
|
|
provider = create_mapping_provider()
|
|
|
|
provider = create_mapping_provider()
|
|
|
|
|
|
|
|
|
|
|
|
username = "bob.bobby"
|
|
|
|
username = "bob.bobby"
|
|
|
|
localpart = provider.mxid_source_to_mxid_localpart(username)
|
|
|
|
localpart = provider.saml_response_to_user_attributes(username)
|
|
|
|
self.assertEqual(localpart, "bob.bobby")
|
|
|
|
self.assertEqual(localpart, "bob.bobby")
|
|
|
|
|
|
|
|
|
|
|
|
def test_username_starting_with_underscore(self):
|
|
|
|
def test_username_starting_with_underscore(self):
|
|
|
|
provider = create_mapping_provider()
|
|
|
|
provider = create_mapping_provider()
|
|
|
|
|
|
|
|
|
|
|
|
username = "_twilight (sparkle)@somewhere.com"
|
|
|
|
username = "_twilight (sparkle)@somewhere.com"
|
|
|
|
localpart = provider.mxid_source_to_mxid_localpart(username)
|
|
|
|
localpart = provider.saml_response_to_user_attributes(username)
|
|
|
|
self.assertEqual(localpart, "twilight.sparkle")
|
|
|
|
self.assertEqual(localpart, "twilight.sparkle")
|
|
|
|
|
|
|
|
|
|
|
|
def test_existing_user(self):
|
|
|
|
def test_existing_user(self):
|
|
|
|
provider = create_mapping_provider()
|
|
|
|
provider = create_mapping_provider()
|
|
|
|
|
|
|
|
|
|
|
|
username = "wibble%@wobble.com"
|
|
|
|
username = "wibble%@wobble.com"
|
|
|
|
localpart = provider.mxid_source_to_mxid_localpart(username)
|
|
|
|
localpart = provider.saml_response_to_user_attributes(username)
|
|
|
|
|
|
|
|
|
|
|
|
# Simulate a failure on the first attempt
|
|
|
|
# Simulate a failure on the first attempt
|
|
|
|
localpart = provider.mxid_source_to_mxid_localpart(username, failures=1)
|
|
|
|
localpart = provider.saml_response_to_user_attributes(username, failures=1)
|
|
|
|
self.assertEqual(localpart, "wibble1")
|
|
|
|
self.assertEqual(localpart, "wibble1")
|
|
|
|