Skip to content

Commit 7d82987

Browse files
authored
Allows CAS SSO flow to provide user IDs composed of numbers only (#17098)
1 parent bd8d886 commit 7d82987

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

changelog.d/17098.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add the ability to allow numeric user IDs with a specific prefix when in the CAS flow. Contributed by Aurélien Grimpard.

docs/usage/configuration/config_documentation.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3558,6 +3558,15 @@ Has the following sub-options:
35583558
users. This allows the CAS SSO flow to be limited to sign in only, rather than
35593559
automatically registering users that have a valid SSO login but do not have
35603560
a pre-registered account. Defaults to true.
3561+
* `allow_numeric_ids`: set to 'true' allow numeric user IDs (default false).
3562+
This allows CAS SSO flow to provide user IDs composed of numbers only.
3563+
These identifiers will be prefixed by the letter "u" by default.
3564+
The prefix can be configured using the "numeric_ids_prefix" option.
3565+
Be careful to choose the prefix correctly to avoid any possible conflicts
3566+
(e.g. user 1234 becomes u1234 when a user u1234 already exists).
3567+
* `numeric_ids_prefix`: the prefix you wish to add in front of a numeric user ID
3568+
when the "allow_numeric_ids" option is set to "true".
3569+
By default, the prefix is the letter "u" and only alphanumeric characters are allowed.
35613570

35623571
*Added in Synapse 1.93.0.*
35633572

@@ -3572,6 +3581,8 @@ cas_config:
35723581
userGroup: "staff"
35733582
department: None
35743583
enable_registration: true
3584+
allow_numeric_ids: true
3585+
numeric_ids_prefix: "numericuser"
35753586
```
35763587
---
35773588
### `sso`

synapse/config/cas.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
6666

6767
self.cas_enable_registration = cas_config.get("enable_registration", True)
6868

69+
self.cas_allow_numeric_ids = cas_config.get("allow_numeric_ids")
70+
self.cas_numeric_ids_prefix = cas_config.get("numeric_ids_prefix")
71+
if (
72+
self.cas_numeric_ids_prefix is not None
73+
and self.cas_numeric_ids_prefix.isalnum() is False
74+
):
75+
raise ConfigError(
76+
"Only alphanumeric characters are allowed for numeric IDs prefix",
77+
("cas_config", "numeric_ids_prefix"),
78+
)
79+
6980
self.idp_name = cas_config.get("idp_name", "CAS")
7081
self.idp_icon = cas_config.get("idp_icon")
7182
self.idp_brand = cas_config.get("idp_brand")
@@ -77,6 +88,8 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
7788
self.cas_displayname_attribute = None
7889
self.cas_required_attributes = []
7990
self.cas_enable_registration = False
91+
self.cas_allow_numeric_ids = False
92+
self.cas_numeric_ids_prefix = "u"
8093

8194

8295
# CAS uses a legacy required attributes mapping, not the one provided by

synapse/handlers/cas.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def __init__(self, hs: "HomeServer"):
7878
self._cas_displayname_attribute = hs.config.cas.cas_displayname_attribute
7979
self._cas_required_attributes = hs.config.cas.cas_required_attributes
8080
self._cas_enable_registration = hs.config.cas.cas_enable_registration
81+
self._cas_allow_numeric_ids = hs.config.cas.cas_allow_numeric_ids
82+
self._cas_numeric_ids_prefix = hs.config.cas.cas_numeric_ids_prefix
8183

8284
self._http_client = hs.get_proxied_http_client()
8385

@@ -188,6 +190,9 @@ def _parse_cas_response(self, cas_response_body: bytes) -> CasResponse:
188190
for child in root[0]:
189191
if child.tag.endswith("user"):
190192
user = child.text
193+
# if numeric user IDs are allowed and username is numeric then we add the prefix so Synapse can handle it
194+
if self._cas_allow_numeric_ids and user is not None and user.isdigit():
195+
user = f"{self._cas_numeric_ids_prefix}{user}"
191196
if child.tag.endswith("attributes"):
192197
for attribute in child:
193198
# ElementTree library expands the namespace in

0 commit comments

Comments
 (0)