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.
 
 
 
 
 
 

138 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(
  29. federation_http_client=None, homeserver_to_use=GenericWorkerServer
  30. )
  31. return hs
  32. def default_config(self) -> JsonDict:
  33. conf = super().default_config()
  34. # we're using FederationReaderServer, which uses a SlavedStore, so we
  35. # have to tell the FederationHandler not to try to access stuff that is only
  36. # in the primary store.
  37. conf["worker_app"] = "yes"
  38. return conf
  39. @parameterized.expand(
  40. [
  41. (["federation"], "auth_fail"),
  42. ([], "no_resource"),
  43. (["openid", "federation"], "auth_fail"),
  44. (["openid"], "auth_fail"),
  45. ]
  46. )
  47. def test_openid_listener(self, names: List[str], expectation: str) -> None:
  48. """
  49. Test different openid listener configurations.
  50. 401 is success here since it means we hit the handler and auth failed.
  51. """
  52. config = {
  53. "port": 8080,
  54. "type": "http",
  55. "bind_addresses": ["0.0.0.0"],
  56. "resources": [{"names": names}],
  57. }
  58. # Listen with the config
  59. hs = self.hs
  60. assert isinstance(hs, GenericWorkerServer)
  61. hs._listen_http(parse_listener_def(0, config))
  62. # Grab the resource from the site that was told to listen
  63. site = self.reactor.tcpServers[0][1]
  64. try:
  65. site.resource.children[b"_matrix"].children[b"federation"]
  66. except KeyError:
  67. if expectation == "no_resource":
  68. return
  69. raise
  70. channel = make_request(
  71. self.reactor, site, "GET", "/_matrix/federation/v1/openid/userinfo"
  72. )
  73. self.assertEqual(channel.code, 401)
  74. @patch("synapse.app.homeserver.KeyResource", new=Mock())
  75. class SynapseHomeserverOpenIDListenerTests(HomeserverTestCase):
  76. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  77. hs = self.setup_test_homeserver(
  78. federation_http_client=None, homeserver_to_use=SynapseHomeServer
  79. )
  80. return hs
  81. @parameterized.expand(
  82. [
  83. (["federation"], "auth_fail"),
  84. ([], "no_resource"),
  85. (["openid", "federation"], "auth_fail"),
  86. (["openid"], "auth_fail"),
  87. ]
  88. )
  89. def test_openid_listener(self, names: List[str], expectation: str) -> None:
  90. """
  91. Test different openid listener configurations.
  92. 401 is success here since it means we hit the handler and auth failed.
  93. """
  94. config = {
  95. "port": 8080,
  96. "type": "http",
  97. "bind_addresses": ["0.0.0.0"],
  98. "resources": [{"names": names}],
  99. }
  100. # Listen with the config
  101. hs = self.hs
  102. assert isinstance(hs, SynapseHomeServer)
  103. hs._listener_http(self.hs.config, parse_listener_def(0, config))
  104. # Grab the resource from the site that was told to listen
  105. site = self.reactor.tcpServers[0][1]
  106. try:
  107. site.resource.children[b"_matrix"].children[b"federation"]
  108. except KeyError:
  109. if expectation == "no_resource":
  110. return
  111. raise
  112. channel = make_request(
  113. self.reactor, site, "GET", "/_matrix/federation/v1/openid/userinfo"
  114. )
  115. self.assertEqual(channel.code, 401)