Skip to content

Commit

Permalink
feat: add a default display name suffix parameter ✨ (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ad2ien authored Jun 20, 2024
1 parent 6fea5fa commit 7e969e6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
![Matrix](https://img.shields.io/badge/matrix-000000?logo=Matrix&logoColor=white)
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/eimis-ans/eimis-prosante-connect-module/lint.yml?label=lint&logo=github)
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/eimis-ans/eimis-prosante-connect-module/test.yml?label=test&logo=github)
![License](https://img.shields.io/badge/license-Apache%202-blue.svg)
![License](https://img.shields.io/badge/license-Apache%202-blue.svg?logo=apache)

A synapse module used by EIMIS to filter and map users registered through Pro Santé Connect
A Synapse module used by EIMIS to map users registered through Pro Santé Connect. It will mainly add the main activity to display name. It can be used with other ID provider, the display name will then be suffixed with `default_display_name_suffix` config parameter.

## OIDC configuration

Todo: how to configure keycloak to have the same token than PSC?
Todo: how to configure Keycloak to have the same token than PSC?

## Synapse configuration

Expand All @@ -33,9 +33,12 @@ Todo: how to configure keycloak to have the same token than PSC?
localpart_template: "{{ user.preferred_username }}"
display_name_template: "{{ user.given_name }} {{ user.family_name }}"
email_template: "{{ user.email }}"
default_display_name_suffix: " - not a doctor"
backchannel_logout_enabled: true # Optional
```
Usually used with `enable_set_displayname` set to false.

## User info

<https://industriels.esante.gouv.fr/produits-et-services/pro-sante-connect/userinfo>
Expand Down
4 changes: 2 additions & 2 deletions docker-test-config/mx-conf/homeserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ macaroon_secret_key: "PaA8zb1+pl+8R:us^A7.u~arj-7kEgiinJ4nyPH^^t#PR9Z1YI"
form_secret: "IHQ3ArqBn&Rv=rS*MP,5a#dii@;;85b4a5AQd-v4XmDLY2bd4X"
signing_key_path: "/data/matrix.local.signing.key"
use_insecure_ssl_client_just_for_testing_do_not_use: true
trusted_key_servers:
- server_name: "matrix.org"
enable_set_displayname: false
oidc_providers:
- idp_id: keycloak
idp_name: "EIMIS Connect"
Expand All @@ -55,6 +54,7 @@ oidc_providers:
config:
localpart_template: "{{ user.preferred_username }}"
display_name_template: "{{ user.name }}"
default_display_name_suffix: " - not a doctor"
backchannel_logout_enabled: true # Optional

# vim:ft=yaml
5 changes: 5 additions & 0 deletions psc_mapping_provider/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ProsanteConnectMappingConfig:
email_template: Optional[Template]
extra_attributes: Dict[str, Template]
confirm_localpart: bool = False
default_display_name_suffix: str = ""


class ProsanteConnectMappingProvider(OidcMappingProvider[ProsanteConnectMappingConfig]):
Expand Down Expand Up @@ -90,6 +91,7 @@ def parse_template_config(option_name: str) -> Optional[Template]:
if not isinstance(confirm_localpart, bool):
raise ConfigError("must be a bool", path=["confirm_localpart"])

default_display_name_suffix = config.get("default_display_name_suffix") or ""
return ProsanteConnectMappingConfig(
subject_template=subject_template,
picture_template=picture_template,
Expand All @@ -98,6 +100,7 @@ def parse_template_config(option_name: str) -> Optional[Template]:
email_template=email_template,
extra_attributes=extra_attributes,
confirm_localpart=confirm_localpart,
default_display_name_suffix=default_display_name_suffix,
)

def get_remote_user_id(self, userinfo: UserInfo) -> str:
Expand Down Expand Up @@ -137,6 +140,8 @@ def render_template_field(template: Optional[Template]) -> Optional[str]:
display_name += " - " + get_activity_from_code(
userinfo["SubjectRefPro"]["exercices"][0]["codeProfession"]
)
else:
display_name += self._config.default_display_name_suffix

emails: List[str] = []
email = render_template_field(self._config.email_template)
Expand Down
23 changes: 20 additions & 3 deletions tests/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

class ProsanteConnectMappingProviderTestCase(aiounittest.AsyncTestCase):
def setUp(self) -> None:
self.user_info = get_a_legit_psc_userinfo()
self.config = ProsanteConnectMappingProvider.parse_config(
{
"subject_template": "{{ user.sub }}",
Expand All @@ -15,15 +14,22 @@ def setUp(self) -> None:
"email_template": "{{ user.email }}",
"extra_attributes": {},
"confirm_localpart": False,
"default_display_name_suffix": " - not a doctor",
}
)

async def test_real_test(self) -> None:
async def test_real_psc_test(self) -> None:
mapper = ProsanteConnectMappingProvider(self.config)
result = await mapper.map_user_attributes(self.user_info, None, 0)
result = await mapper.map_user_attributes(get_a_legit_psc_userinfo(), None, 0)
assert result["display_name"] == "Fistinien Grominoch - Pédicure-Podologue"
assert result["localpart"] == "ans20231122132732"

async def test_real_non_psc_test(self) -> None:
mapper = ProsanteConnectMappingProvider(self.config)
result = await mapper.map_user_attributes(get_a_regular_userinfo(), None, 0)
assert result["display_name"] == "Fistinien Grominoch - not a doctor"
assert result["localpart"] == "ans20231122132732"


def get_a_legit_psc_userinfo():
return {
Expand Down Expand Up @@ -94,3 +100,14 @@ def get_a_legit_psc_userinfo():
"SubjectNameID": "ANS20231122132732",
"family_name": "Grominoch",
}


def get_a_regular_userinfo():
return {
"sub": "f:550dc1c8-d97b-4b1e-ac8c-8eb4471cf9dd:ANS20231122132732",
"preferred_username": "ANS20231122132732",
"codeCivilite": "M",
"given_name": "Fistinien",
"SubjectNameID": "ANS20231122132732",
"family_name": "Grominoch",
}

0 comments on commit 7e969e6

Please sign in to comment.