Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/12212.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a new Jinja2 template filter to extract the local part of an email address.
3 changes: 2 additions & 1 deletion docs/sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1948,7 +1948,8 @@ saml2_config:
# localpart_template: Jinja2 template for the localpart of the MXID.
# If this is not set, the user will be prompted to choose their
# own username (see the documentation for the
# 'sso_auth_account_details.html' template).
# 'sso_auth_account_details.html' template). This template can
# use the 'localpart_from_email' filter.
#
# confirm_localpart: Whether to prompt the user to validate (or
# change) the generated localpart (see the documentation for the
Expand Down
7 changes: 7 additions & 0 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ Turns a `mxc://` URL for media content into an HTTP(S) one using the homeserver'

Example: `message.sender_avatar_url|mxc_to_http(32,32)`

```python
localpart_from_email(address: str) -> str
```

Returns the local part of an email address (e.g. `alice` in `[email protected]`).

Example: `user.email_address|localpart_from_email`
Comment on lines +39 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is only available in the SSO templates, we should probably say so.

That said, is there any reason for it only to be available in the SSO templates?


## Email templates

Expand Down
3 changes: 2 additions & 1 deletion synapse/config/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ def generate_config_section(self, config_dir_path, server_name, **kwargs) -> str
# localpart_template: Jinja2 template for the localpart of the MXID.
# If this is not set, the user will be prompted to choose their
# own username (see the documentation for the
# 'sso_auth_account_details.html' template).
# 'sso_auth_account_details.html' template). This template can
# use the 'localpart_from_email' filter.
#
# confirm_localpart: Whether to prompt the user to validate (or
# change) the generated localpart (see the documentation for the
Expand Down
6 changes: 6 additions & 0 deletions synapse/handlers/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from synapse.util import Clock, json_decoder
from synapse.util.caches.cached_call import RetryOnExceptionCachedCall
from synapse.util.macaroons import get_value_from_macaroon, satisfy_expiry
from synapse.util.templates import _localpart_from_email_filter

if TYPE_CHECKING:
from synapse.server import HomeServer
Expand Down Expand Up @@ -1308,6 +1309,11 @@ def jinja_finalize(thing: Any) -> Any:


env = Environment(finalize=jinja_finalize)
env.filters.update(
{
"localpart_from_email": _localpart_from_email_filter,
}
)


@attr.s(slots=True, frozen=True, auto_attribs=True)
Expand Down
5 changes: 5 additions & 0 deletions synapse/util/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def build_jinja_env(
{
"format_ts": _format_ts_filter,
"mxc_to_http": _create_mxc_to_http_filter(config.server.public_baseurl),
"localpart_from_email": _localpart_from_email_filter,
}
)

Expand Down Expand Up @@ -112,3 +113,7 @@ def mxc_to_http_filter(

def _format_ts_filter(value: int, format: str) -> str:
return time.strftime(format, time.localtime(value / 1000))


def _localpart_from_email_filter(address: str) -> str:
return address.rsplit("@", 1)[0]