Skip to content

Commit

Permalink
update arg
Browse files Browse the repository at this point in the history
  • Loading branch information
odelcroi committed Dec 6, 2024
1 parent b64c397 commit 7602bb3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions synapse_sso_proconnect/proconnect_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down
25 changes: 17 additions & 8 deletions tests/test_proconnect_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]")
# Assertions
Expand All @@ -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":{
"[email protected]":"[email protected]",
"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":"[email protected]", "search": "[email protected]"},
{ "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("[email protected]")
Expand All @@ -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("[email protected]")

Expand All @@ -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("[email protected]")
Expand Down

0 comments on commit 7602bb3

Please sign in to comment.