Compare commits

..

No commits in common. "6b77f3e8655dd9920dd4f26ba329bd04779d09fc" and "003b25e9ffed2e3129e928555f91ea8688bbf436" have entirely different histories.

3 changed files with 36 additions and 29 deletions

View File

@ -1 +1 @@
Add additional error checking for OpenID Connect and SAML mapping providers.
Add tests for SAML integration.

View File

@ -23,7 +23,7 @@ import pymacaroons
from twisted.python.failure import Failure
from twisted.web._newclient import ResponseDone
from synapse.handlers.oidc_handler import OidcError, OidcMappingProvider
from synapse.handlers.oidc_handler import OidcError, OidcHandler, OidcMappingProvider
from synapse.handlers.sso import MappingException
from synapse.types import UserID
@ -127,8 +127,13 @@ async def get_json(url):
class OidcHandlerTestCase(HomeserverTestCase):
def default_config(self):
config = super().default_config()
def make_homeserver(self, reactor, clock):
self.http_client = Mock(spec=["get_json"])
self.http_client.get_json.side_effect = get_json
self.http_client.user_agent = "Synapse Test"
config = self.default_config()
config["public_baseurl"] = BASE_URL
oidc_config = {
"enabled": True,
@ -144,24 +149,19 @@ class OidcHandlerTestCase(HomeserverTestCase):
oidc_config.update(config.get("oidc_config", {}))
config["oidc_config"] = oidc_config
return config
hs = self.setup_test_homeserver(
http_client=self.http_client,
proxied_http_client=self.http_client,
config=config,
)
def make_homeserver(self, reactor, clock):
self.http_client = Mock(spec=["get_json"])
self.http_client.get_json.side_effect = get_json
self.http_client.user_agent = "Synapse Test"
hs = self.setup_test_homeserver(proxied_http_client=self.http_client)
self.handler = hs.get_oidc_handler()
sso_handler = hs.get_sso_handler()
self.handler = OidcHandler(hs)
# Mock the render error method.
self.render_error = Mock(return_value=None)
sso_handler.render_error = self.render_error
self.handler._sso_handler.render_error = self.render_error
# Reduce the number of attempts when generating MXIDs.
sso_handler._MAP_USERNAME_RETRIES = 3
self.handler._sso_handler._MAP_USERNAME_RETRIES = 3
return hs
@ -832,7 +832,7 @@ class OidcHandlerTestCase(HomeserverTestCase):
# test_user is already taken, so test_user1 gets registered instead.
self.assertEqual(mxid, "@test_user1:test")
# Register all of the potential mxids for a particular OIDC username.
# Register all of the potential usernames for a particular username.
self.get_success(
store.register_user(user_id="@tester:test", password_hash=None)
)

View File

@ -12,8 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from mock import Mock
import attr
from synapse.handlers.saml_handler import SamlHandler
from synapse.handlers.sso import MappingException
from tests.unittest import HomeserverTestCase
@ -50,8 +53,12 @@ class TestMappingProvider:
class SamlHandlerTestCase(HomeserverTestCase):
def default_config(self):
config = super().default_config()
def make_homeserver(self, reactor, clock):
self.http_client = Mock(spec=["get_json"])
self.http_client.user_agent = "Synapse Test"
config = self.default_config()
config["public_baseurl"] = BASE_URL
saml_config = {
"sp_config": {"metadata": {}},
@ -61,16 +68,16 @@ class SamlHandlerTestCase(HomeserverTestCase):
}
config["saml2_config"] = saml_config
return config
hs = self.setup_test_homeserver(
http_client=self.http_client,
proxied_http_client=self.http_client,
config=config,
)
def make_homeserver(self, reactor, clock):
hs = self.setup_test_homeserver()
self.handler = hs.get_saml_handler()
self.handler = SamlHandler(hs)
# Reduce the number of attempts when generating MXIDs.
sso_handler = hs.get_sso_handler()
sso_handler._MAP_USERNAME_RETRIES = 3
self.handler._sso_handler._MAP_USERNAME_RETRIES = 3
return hs
@ -98,7 +105,7 @@ class SamlHandlerTestCase(HomeserverTestCase):
)
self.assertEqual(str(e.value), "localpart is invalid: föö")
def test_map_saml_response_to_user_retries(self):
def test_map_userinfo_to_user_retries(self):
"""The mapping provider can retry generating an MXID if the MXID is already in use."""
store = self.hs.get_datastore()
self.get_success(
@ -114,7 +121,7 @@ class SamlHandlerTestCase(HomeserverTestCase):
# test_user is already taken, so test_user1 gets registered instead.
self.assertEqual(mxid, "@test_user1:test")
# Register all of the potential mxids for a particular SAML username.
# Register all of the potential usernames for a particular username.
self.get_success(
store.register_user(user_id="@tester:test", password_hash=None)
)