From 7602bb3f7ff8910eae881db33d195f91afa8c1e4 Mon Sep 17 00:00:00 2001 From: olivier Date: Fri, 6 Dec 2024 15:11:01 +0100 Subject: [PATCH] update arg --- README.md | 19 +++++++++++---- synapse_sso_proconnect/proconnect_mapping.py | 18 +++++++------- tests/test_proconnect_mapping.py | 25 +++++++++++++------- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index fb67bf0..b2fe4b3 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,23 @@ pip install synapse-sso-proconnect ## Config -Add the following to your Synapse config: +Add the following to your oidc config: ```yaml -modules: - - module: - config: +oidc_providers: + - idp_id + ... + user_mapping_provider: + module: synapse_sso_proconnect.proconnect_mapping.ProConnectMappingProvider + config: + user_id_lookup_fallback_rules: + - match : user@new_domain.fr + search : user@old_domain.fr + - match : user@another_domain.fr + search : user@an_old_domain.fr + + + ``` ## Development and Testing diff --git a/synapse_sso_proconnect/proconnect_mapping.py b/synapse_sso_proconnect/proconnect_mapping.py index 11f013f..3203b8d 100644 --- a/synapse_sso_proconnect/proconnect_mapping.py +++ b/synapse_sso_proconnect/proconnect_mapping.py @@ -15,7 +15,7 @@ @attr.s(slots=True, frozen=True, auto_attribs=True) class ProConnectMappingConfig: - user_id_lookup_fallback_rules:Dict[str, str]= {} + user_id_lookup_fallback_rules:List[Dict[str, str]]= [] class ProConnectMappingProvider(OidcMappingProvider[ProConnectMappingConfig]): def __init__(self, config: ProConnectMappingConfig, module_api: ModuleApi): @@ -86,16 +86,18 @@ async def search_user_id_by_threepid(self, email: str): # If userId is not found, attempt replacements if not userId: # Iterate through all mappings - for old_value, new_value in self._config.user_id_lookup_fallback_rules.items(): - # Check if the key (old_value) exists within the email - if old_value in email: - # Replace the old value with the new value - replaced_email = email.replace(old_value, new_value) + for rule in self._config.user_id_lookup_fallback_rules: + replaced_email = email - # Retry finding the userId with the replaced email + # Rule: Match by specific email + if "match" in rule and rule["match"] in email: + replaced_email = email.replace(rule["match"], rule["search"]) + + # Retry lookup if the email was modified + if replaced_email != email: userId = await self.module_api._store.get_user_id_by_threepid("email", replaced_email) - # If userId is found, break the loop early + # Stop if a userId is found if userId: break diff --git a/tests/test_proconnect_mapping.py b/tests/test_proconnect_mapping.py index 3b5dff8..da2e507 100644 --- a/tests/test_proconnect_mapping.py +++ b/tests/test_proconnect_mapping.py @@ -23,7 +23,11 @@ class ProConnectMappingTest(aiounittest.AsyncTestCase): #def setUp(self) -> None: async def test_with_map_should_replace(self): - self.module = create_module({"user_id_lookup_fallback_rules":{"very-new.fr": "beta.fr", "new.fr": "beta.fr"}}) + self.module = create_module({"user_id_lookup_fallback_rules": + [ + {"match":"very-new.fr", "search": "beta.fr"}, + { "match":"new.fr","search":"beta.fr"} + ]}) # Call the tested function with an email that requires replacement user_id = await self.module.search_user_id_by_threepid("test@new.fr") # Assertions @@ -32,10 +36,14 @@ async def test_with_map_should_replace(self): self.assertEqual(user_id, "test-beta") # Should match the replaced email - async def test_replace_by_priority(self): - self.module = create_module({"user_id_lookup_fallback_rules":{ - "test@new.fr":"test@old.fr", - "new.fr": "beta.fr"}})#replace by domain leads to a dead-end but it lower in the list + async def test_replace_by_priority(self): + self.module = create_module({"user_id_lookup_fallback_rules": + [{"match":"test@new.fr", "search": "test@old.fr"}, + { "match":"new.fr","search":"beta.fr"} + ]}) + + + #replace by domain leads to a dead-end but it lower in the list # Call the tested function with an email that requires replacement user_id = await self.module.search_user_id_by_threepid("test@new.fr") @@ -45,8 +53,9 @@ async def test_replace_by_priority(self): self.assertEqual(user_id, "test-old") # Should match the replaced email async def test_with_map_should_not_replace(self): - self.module = create_module({"user_id_lookup_fallback_rules":{"new.fr": "beta.fr"}}) - + self.module = create_module({"user_id_lookup_fallback_rules": + [{ "match":"new.fr","search":"beta.fr"}]}) + # Call the tested function with an email that requires replacement user_id = await self.module.search_user_id_by_threepid("test@numerique.fr") @@ -55,7 +64,7 @@ async def test_with_map_should_not_replace(self): async def test_with_empty_map(self): - self.module = create_module({"user_id_lookup_fallback_rules":{}}) + self.module = create_module({"user_id_lookup_fallback_rules":[]}) # Call the tested function with an email that requires replacement user_id = await self.module.search_user_id_by_threepid("test@numerique.fr")