auth.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  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 import defer
  16. from synapse.api.constants import LoginType
  17. from synapse.api.errors import SynapseError
  18. from synapse.api.urls import CLIENT_V2_ALPHA_PREFIX
  19. from synapse.http.server import finish_request
  20. from synapse.http.servlet import RestServlet
  21. from ._base import client_v2_patterns
  22. import logging
  23. logger = logging.getLogger(__name__)
  24. RECAPTCHA_TEMPLATE = """
  25. <html>
  26. <head>
  27. <title>Authentication</title>
  28. <meta name='viewport' content='width=device-width, initial-scale=1,
  29. user-scalable=no, minimum-scale=1.0, maximum-scale=1.0'>
  30. <script src="https://www.google.com/recaptcha/api.js"
  31. async defer></script>
  32. <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
  33. <link rel="stylesheet" href="/_matrix/static/client/register/style.css">
  34. <script>
  35. function captchaDone() {
  36. $('#registrationForm').submit();
  37. }
  38. </script>
  39. </head>
  40. <body>
  41. <form id="registrationForm" method="post" action="%(myurl)s">
  42. <div>
  43. <p>
  44. Hello! We need to prevent computer programs and other automated
  45. things from creating accounts on this server.
  46. </p>
  47. <p>
  48. Please verify that you're not a robot.
  49. </p>
  50. <input type="hidden" name="session" value="%(session)s" />
  51. <div class="g-recaptcha"
  52. data-sitekey="%(sitekey)s"
  53. data-callback="captchaDone">
  54. </div>
  55. <noscript>
  56. <input type="submit" value="All Done" />
  57. </noscript>
  58. </div>
  59. </div>
  60. </form>
  61. </body>
  62. </html>
  63. """
  64. SUCCESS_TEMPLATE = """
  65. <html>
  66. <head>
  67. <title>Success!</title>
  68. <meta name='viewport' content='width=device-width, initial-scale=1,
  69. user-scalable=no, minimum-scale=1.0, maximum-scale=1.0'>
  70. <link rel="stylesheet" href="/_matrix/static/client/register/style.css">
  71. <script>
  72. if (window.onAuthDone != undefined) {
  73. window.onAuthDone();
  74. }
  75. </script>
  76. </head>
  77. <body>
  78. <div>
  79. <p>Thank you</p>
  80. <p>You may now close this window and return to the application</p>
  81. </div>
  82. </body>
  83. </html>
  84. """
  85. class AuthRestServlet(RestServlet):
  86. """
  87. Handles Client / Server API authentication in any situations where it
  88. cannot be handled in the normal flow (with requests to the same endpoint).
  89. Current use is for web fallback auth.
  90. """
  91. PATTERNS = client_v2_patterns("/auth/(?P<stagetype>[\w\.]*)/fallback/web")
  92. def __init__(self, hs):
  93. super(AuthRestServlet, self).__init__()
  94. self.hs = hs
  95. self.auth = hs.get_auth()
  96. self.auth_handler = hs.get_auth_handler()
  97. self.registration_handler = hs.get_handlers().registration_handler
  98. @defer.inlineCallbacks
  99. def on_GET(self, request, stagetype):
  100. yield
  101. if stagetype == LoginType.RECAPTCHA:
  102. if ('session' not in request.args or
  103. len(request.args['session']) == 0):
  104. raise SynapseError(400, "No session supplied")
  105. session = request.args["session"][0]
  106. html = RECAPTCHA_TEMPLATE % {
  107. 'session': session,
  108. 'myurl': "%s/auth/%s/fallback/web" % (
  109. CLIENT_V2_ALPHA_PREFIX, LoginType.RECAPTCHA
  110. ),
  111. 'sitekey': self.hs.config.recaptcha_public_key,
  112. }
  113. html_bytes = html.encode("utf8")
  114. request.setResponseCode(200)
  115. request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
  116. request.setHeader(b"Server", self.hs.version_string)
  117. request.setHeader(b"Content-Length", b"%d" % (len(html_bytes),))
  118. request.write(html_bytes)
  119. finish_request(request)
  120. defer.returnValue(None)
  121. else:
  122. raise SynapseError(404, "Unknown auth stage type")
  123. @defer.inlineCallbacks
  124. def on_POST(self, request, stagetype):
  125. yield
  126. if stagetype == "m.login.recaptcha":
  127. if ('g-recaptcha-response' not in request.args or
  128. len(request.args['g-recaptcha-response'])) == 0:
  129. raise SynapseError(400, "No captcha response supplied")
  130. if ('session' not in request.args or
  131. len(request.args['session'])) == 0:
  132. raise SynapseError(400, "No session supplied")
  133. session = request.args['session'][0]
  134. authdict = {
  135. 'response': request.args['g-recaptcha-response'][0],
  136. 'session': session,
  137. }
  138. success = yield self.auth_handler.add_oob_auth(
  139. LoginType.RECAPTCHA,
  140. authdict,
  141. self.hs.get_ip_from_request(request)
  142. )
  143. if success:
  144. html = SUCCESS_TEMPLATE
  145. else:
  146. html = RECAPTCHA_TEMPLATE % {
  147. 'session': session,
  148. 'myurl': "%s/auth/%s/fallback/web" % (
  149. CLIENT_V2_ALPHA_PREFIX, LoginType.RECAPTCHA
  150. ),
  151. 'sitekey': self.hs.config.recaptcha_public_key,
  152. }
  153. html_bytes = html.encode("utf8")
  154. request.setResponseCode(200)
  155. request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
  156. request.setHeader(b"Server", self.hs.version_string)
  157. request.setHeader(b"Content-Length", b"%d" % (len(html_bytes),))
  158. request.write(html_bytes)
  159. finish_request(request)
  160. defer.returnValue(None)
  161. else:
  162. raise SynapseError(404, "Unknown auth stage type")
  163. def on_OPTIONS(self, _):
  164. return 200, {}
  165. def register_servlets(hs, http_server):
  166. AuthRestServlet(hs).register(http_server)