You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

135 lines
4.5 KiB

  1. # Copyright 2019 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import List
  15. from unittest.mock import Mock, patch
  16. from parameterized import parameterized
  17. from twisted.test.proto_helpers import MemoryReactor
  18. from synapse.app.generic_worker import GenericWorkerServer
  19. from synapse.app.homeserver import SynapseHomeServer
  20. from synapse.config.server import parse_listener_def
  21. from synapse.server import HomeServer
  22. from synapse.types import JsonDict
  23. from synapse.util import Clock
  24. from tests.server import make_request
  25. from tests.unittest import HomeserverTestCase
  26. class FederationReaderOpenIDListenerTests(HomeserverTestCase):
  27. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  28. hs = self.setup_test_homeserver(homeserver_to_use=GenericWorkerServer)
  29. return hs
  30. def default_config(self) -> JsonDict:
  31. conf = super().default_config()
  32. # we're using GenericWorkerServer, which uses a GenericWorkerStore, so we
  33. # have to tell the FederationHandler not to try to access stuff that is only
  34. # in the primary store.
  35. conf["worker_app"] = "yes"
  36. conf["instance_map"] = {"main": {"host": "127.0.0.1", "port": 0}}
  37. return conf
  38. @parameterized.expand(
  39. [
  40. (["federation"], "auth_fail"),
  41. ([], "no_resource"),
  42. (["openid", "federation"], "auth_fail"),
  43. (["openid"], "auth_fail"),
  44. ]
  45. )
  46. def test_openid_listener(self, names: List[str], expectation: str) -> None:
  47. """
  48. Test different openid listener configurations.
  49. 401 is success here since it means we hit the handler and auth failed.
  50. """
  51. config = {
  52. "port": 8080,
  53. "type": "http",
  54. "bind_addresses": ["0.0.0.0"],
  55. "resources": [{"names": names}],
  56. }
  57. # Listen with the config
  58. hs = self.hs
  59. assert isinstance(hs, GenericWorkerServer)
  60. hs._listen_http(parse_listener_def(0, config))
  61. # Grab the resource from the site that was told to listen
  62. site = self.reactor.tcpServers[0][1]
  63. try:
  64. site.resource.children[b"_matrix"].children[b"federation"]
  65. except KeyError:
  66. if expectation == "no_resource":
  67. return
  68. raise
  69. channel = make_request(
  70. self.reactor, site, "GET", "/_matrix/federation/v1/openid/userinfo"
  71. )
  72. self.assertEqual(channel.code, 401)
  73. @patch("synapse.app.homeserver.KeyResource", new=Mock())
  74. class SynapseHomeserverOpenIDListenerTests(HomeserverTestCase):
  75. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  76. hs = self.setup_test_homeserver(homeserver_to_use=SynapseHomeServer)
  77. return hs
  78. @parameterized.expand(
  79. [
  80. (["federation"], "auth_fail"),
  81. ([], "no_resource"),
  82. (["openid", "federation"], "auth_fail"),
  83. (["openid"], "auth_fail"),
  84. ]
  85. )
  86. def test_openid_listener(self, names: List[str], expectation: str) -> None:
  87. """
  88. Test different openid listener configurations.
  89. 401 is success here since it means we hit the handler and auth failed.
  90. """
  91. config = {
  92. "port": 8080,
  93. "type": "http",
  94. "bind_addresses": ["0.0.0.0"],
  95. "resources": [{"names": names}],
  96. }
  97. # Listen with the config
  98. hs = self.hs
  99. assert isinstance(hs, SynapseHomeServer)
  100. hs._listener_http(self.hs.config, parse_listener_def(0, config))
  101. # Grab the resource from the site that was told to listen
  102. site = self.reactor.tcpServers[0][1]
  103. try:
  104. site.resource.children[b"_matrix"].children[b"federation"]
  105. except KeyError:
  106. if expectation == "no_resource":
  107. return
  108. raise
  109. channel = make_request(
  110. self.reactor, site, "GET", "/_matrix/federation/v1/openid/userinfo"
  111. )
  112. self.assertEqual(channel.code, 401)