Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 9b7c4f4

Browse files
committed
Merge remote-tracking branch 'origin/develop' into rav/saml_mapping_work
2 parents ed8b92f + 566ac40 commit 9b7c4f4

File tree

7 files changed

+75
-8
lines changed

7 files changed

+75
-8
lines changed

changelog.d/6069.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug which caused SAML attribute maps to be overridden by defaults.

changelog.d/6097.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add sid to next_link for email validation.

changelog.d/6099.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove unused parameter to get_user_id_by_threepid.

synapse/config/saml2_config.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# Copyright 2018 New Vector Ltd
3+
# Copyright 2019 The Matrix.org Foundation C.I.C.
34
#
45
# Licensed under the Apache License, Version 2.0 (the "License");
56
# you may not use this file except in compliance with the License.
@@ -12,17 +13,47 @@
1213
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1314
# See the License for the specific language governing permissions and
1415
# limitations under the License.
16+
1517
import re
1618

1719
from synapse.python_dependencies import DependencyException, check_requirements
1820
from synapse.types import (
1921
map_username_to_mxid_localpart,
2022
mxid_localpart_allowed_characters,
2123
)
24+
from synapse.util.module_loader import load_python_module
2225

2326
from ._base import Config, ConfigError
2427

2528

29+
def _dict_merge(merge_dict, into_dict):
30+
"""Do a deep merge of two dicts
31+
32+
Recursively merges `merge_dict` into `into_dict`:
33+
* For keys where both `merge_dict` and `into_dict` have a dict value, the values
34+
are recursively merged
35+
* For all other keys, the values in `into_dict` (if any) are overwritten with
36+
the value from `merge_dict`.
37+
38+
Args:
39+
merge_dict (dict): dict to merge
40+
into_dict (dict): target dict
41+
"""
42+
for k, v in merge_dict.items():
43+
if k not in into_dict:
44+
into_dict[k] = v
45+
continue
46+
47+
current_val = into_dict[k]
48+
49+
if isinstance(v, dict) and isinstance(current_val, dict):
50+
_dict_merge(v, current_val)
51+
continue
52+
53+
# otherwise we just overwrite
54+
into_dict[k] = v
55+
56+
2657
class SAML2Config(Config):
2758
def read_config(self, config, **kwargs):
2859
self.saml2_enabled = False
@@ -50,15 +81,20 @@ def read_config(self, config, **kwargs):
5081
"grandfathered_mxid_source_attribute", "uid"
5182
)
5283

53-
import saml2.config
54-
55-
self.saml2_sp_config = saml2.config.SPConfig()
56-
self.saml2_sp_config.load(self._default_saml_config_dict())
57-
self.saml2_sp_config.load(saml2_config.get("sp_config", {}))
84+
saml2_config_dict = self._default_saml_config_dict()
85+
_dict_merge(
86+
merge_dict=saml2_config.get("sp_config", {}), into_dict=saml2_config_dict
87+
)
5888

5989
config_path = saml2_config.get("config_path", None)
6090
if config_path is not None:
61-
self.saml2_sp_config.load_file(config_path)
91+
mod = load_python_module(config_path)
92+
_dict_merge(merge_dict=mod.CONFIG, into_dict=saml2_config_dict)
93+
94+
import saml2.config
95+
96+
self.saml2_sp_config = saml2.config.SPConfig()
97+
self.saml2_sp_config.load(saml2_config_dict)
6298

6399
# session lifetime: in milliseconds
64100
self.saml2_session_lifetime = self.parse_duration(

synapse/handlers/identity.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""Utilities for interacting with Identity Servers"""
1919

2020
import logging
21+
import urllib
2122

2223
from canonicaljson import json
2324

@@ -328,6 +329,15 @@ def send_threepid_validation(
328329
# Generate a session id
329330
session_id = random_string(16)
330331

332+
if next_link:
333+
# Manipulate the next_link to add the sid, because the caller won't get
334+
# it until we send a response, by which time we've sent the mail.
335+
if "?" in next_link:
336+
next_link += "&"
337+
else:
338+
next_link += "?"
339+
next_link += "sid=" + urllib.parse.quote(session_id)
340+
331341
# Generate a new validation token
332342
token = random_string(32)
333343

synapse/storage/registration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ def _find_next_generated_user_id(txn):
516516
)
517517

518518
@defer.inlineCallbacks
519-
def get_user_id_by_threepid(self, medium, address, require_verified=False):
519+
def get_user_id_by_threepid(self, medium, address):
520520
"""Returns user id from threepid
521521
522522
Args:

synapse/util/module_loader.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
# limitations under the License.
1515

1616
import importlib
17+
import importlib.util
1718

1819
from synapse.config._base import ConfigError
1920

2021

2122
def load_module(provider):
22-
""" Loads a module with its config
23+
""" Loads a synapse module with its config
2324
Take a dict with keys 'module' (the module name) and 'config'
2425
(the config dict).
2526
@@ -38,3 +39,20 @@ def load_module(provider):
3839
raise ConfigError("Failed to parse config for %r: %r" % (provider["module"], e))
3940

4041
return provider_class, provider_config
42+
43+
44+
def load_python_module(location: str):
45+
"""Load a python module, and return a reference to its global namespace
46+
47+
Args:
48+
location (str): path to the module
49+
50+
Returns:
51+
python module object
52+
"""
53+
spec = importlib.util.spec_from_file_location(location, location)
54+
if spec is None:
55+
raise Exception("Unable to load module at %s" % (location,))
56+
mod = importlib.util.module_from_spec(spec)
57+
spec.loader.exec_module(mod)
58+
return mod

0 commit comments

Comments
 (0)