test_auth.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 fufilled 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")