test_auth.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from twisted.internet.defer import succeed
  16. import synapse.rest.admin
  17. from synapse.api.constants import LoginType
  18. from synapse.handlers.ui_auth.checkers import UserInteractiveAuthChecker
  19. from synapse.rest.client.v2_alpha import auth, register
  20. from tests import unittest
  21. class DummyRecaptchaChecker(UserInteractiveAuthChecker):
  22. def __init__(self, hs):
  23. super().__init__(hs)
  24. self.recaptcha_attempts = []
  25. def check_auth(self, authdict, clientip):
  26. self.recaptcha_attempts.append((authdict, clientip))
  27. return succeed(True)
  28. class FallbackAuthTests(unittest.HomeserverTestCase):
  29. servlets = [
  30. auth.register_servlets,
  31. synapse.rest.admin.register_servlets_for_client_rest_resource,
  32. register.register_servlets,
  33. ]
  34. hijack_auth = False
  35. def make_homeserver(self, reactor, clock):
  36. config = self.default_config()
  37. config["enable_registration_captcha"] = True
  38. config["recaptcha_public_key"] = "brokencake"
  39. config["registrations_require_3pid"] = []
  40. hs = self.setup_test_homeserver(config=config)
  41. return hs
  42. def prepare(self, reactor, clock, hs):
  43. self.recaptcha_checker = DummyRecaptchaChecker(hs)
  44. auth_handler = hs.get_auth_handler()
  45. auth_handler.checkers[LoginType.RECAPTCHA] = self.recaptcha_checker
  46. @unittest.INFO
  47. def test_fallback_captcha(self):
  48. request, channel = self.make_request(
  49. "POST",
  50. "register",
  51. {"username": "user", "type": "m.login.password", "password": "bar"},
  52. )
  53. self.render(request)
  54. # Returns a 401 as per the spec
  55. self.assertEqual(request.code, 401)
  56. # Grab the session
  57. session = channel.json_body["session"]
  58. # Assert our configured public key is being given
  59. self.assertEqual(
  60. channel.json_body["params"]["m.login.recaptcha"]["public_key"], "brokencake"
  61. )
  62. request, channel = self.make_request(
  63. "GET", "auth/m.login.recaptcha/fallback/web?session=" + session
  64. )
  65. self.render(request)
  66. self.assertEqual(request.code, 200)
  67. request, channel = self.make_request(
  68. "POST",
  69. "auth/m.login.recaptcha/fallback/web?session="
  70. + session
  71. + "&g-recaptcha-response=a",
  72. )
  73. self.render(request)
  74. self.assertEqual(request.code, 200)
  75. # The recaptcha handler is called with the response given
  76. attempts = self.recaptcha_checker.recaptcha_attempts
  77. self.assertEqual(len(attempts), 1)
  78. self.assertEqual(attempts[0][0]["response"], "a")
  79. # also complete the dummy auth
  80. request, channel = self.make_request(
  81. "POST", "register", {"auth": {"session": session, "type": "m.login.dummy"}}
  82. )
  83. self.render(request)
  84. # Now we should have fulfilled a complete auth flow, including
  85. # the recaptcha fallback step, we can then send a
  86. # request to the register API with the session in the authdict.
  87. request, channel = self.make_request(
  88. "POST", "register", {"auth": {"session": session}}
  89. )
  90. self.render(request)
  91. self.assertEqual(channel.code, 200)
  92. # We're given a registered user.
  93. self.assertEqual(channel.json_body["user_id"], "@user:test")
  94. def test_cannot_change_operation(self):
  95. """
  96. The initial requested operation cannot be modified during the user interactive authentication session.
  97. """
  98. # Make the initial request to register. (Later on a different password
  99. # will be used.)
  100. request, channel = self.make_request(
  101. "POST",
  102. "register",
  103. {"username": "user", "type": "m.login.password", "password": "bar"},
  104. )
  105. self.render(request)
  106. # Returns a 401 as per the spec
  107. self.assertEqual(request.code, 401)
  108. # Grab the session
  109. session = channel.json_body["session"]
  110. # Assert our configured public key is being given
  111. self.assertEqual(
  112. channel.json_body["params"]["m.login.recaptcha"]["public_key"], "brokencake"
  113. )
  114. request, channel = self.make_request(
  115. "GET", "auth/m.login.recaptcha/fallback/web?session=" + session
  116. )
  117. self.render(request)
  118. self.assertEqual(request.code, 200)
  119. request, channel = self.make_request(
  120. "POST",
  121. "auth/m.login.recaptcha/fallback/web?session="
  122. + session
  123. + "&g-recaptcha-response=a",
  124. )
  125. self.render(request)
  126. self.assertEqual(request.code, 200)
  127. # The recaptcha handler is called with the response given
  128. attempts = self.recaptcha_checker.recaptcha_attempts
  129. self.assertEqual(len(attempts), 1)
  130. self.assertEqual(attempts[0][0]["response"], "a")
  131. # also complete the dummy auth
  132. request, channel = self.make_request(
  133. "POST", "register", {"auth": {"session": session, "type": "m.login.dummy"}}
  134. )
  135. self.render(request)
  136. # Now we should have fulfilled a complete auth flow, including
  137. # the recaptcha fallback step. Make the initial request again, but
  138. # with a different password. This causes the request to fail since the
  139. # operaiton was modified during the ui auth session.
  140. request, channel = self.make_request(
  141. "POST",
  142. "register",
  143. {
  144. "username": "user",
  145. "type": "m.login.password",
  146. "password": "foo", # Note this doesn't match the original request.
  147. "auth": {"session": session},
  148. },
  149. )
  150. self.render(request)
  151. self.assertEqual(channel.code, 403)
  152. def test_complete_operation_unknown_session(self):
  153. """
  154. Attempting to mark an invalid session as complete should error.
  155. """
  156. # Make the initial request to register. (Later on a different password
  157. # will be used.)
  158. request, channel = self.make_request(
  159. "POST",
  160. "register",
  161. {"username": "user", "type": "m.login.password", "password": "bar"},
  162. )
  163. self.render(request)
  164. # Returns a 401 as per the spec
  165. self.assertEqual(request.code, 401)
  166. # Grab the session
  167. session = channel.json_body["session"]
  168. # Assert our configured public key is being given
  169. self.assertEqual(
  170. channel.json_body["params"]["m.login.recaptcha"]["public_key"], "brokencake"
  171. )
  172. request, channel = self.make_request(
  173. "GET", "auth/m.login.recaptcha/fallback/web?session=" + session
  174. )
  175. self.render(request)
  176. self.assertEqual(request.code, 200)
  177. # Attempt to complete an unknown session, which should return an error.
  178. unknown_session = session + "unknown"
  179. request, channel = self.make_request(
  180. "POST",
  181. "auth/m.login.recaptcha/fallback/web?session="
  182. + unknown_session
  183. + "&g-recaptcha-response=a",
  184. )
  185. self.render(request)
  186. self.assertEqual(request.code, 400)