auth.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. import logging
  16. from twisted.internet import defer
  17. from synapse.api.constants import LoginType
  18. from synapse.api.errors import SynapseError
  19. from synapse.api.urls import CLIENT_V2_ALPHA_PREFIX
  20. from synapse.http.server import finish_request
  21. from synapse.http.servlet import RestServlet
  22. from ._base import client_v2_patterns
  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) {
  73. window.onAuthDone();
  74. } else if (window.opener && window.opener.postMessage) {
  75. window.opener.postMessage("authDone", "*");
  76. }
  77. </script>
  78. </head>
  79. <body>
  80. <div>
  81. <p>Thank you</p>
  82. <p>You may now close this window and return to the application</p>
  83. </div>
  84. </body>
  85. </html>
  86. """
  87. class AuthRestServlet(RestServlet):
  88. """
  89. Handles Client / Server API authentication in any situations where it
  90. cannot be handled in the normal flow (with requests to the same endpoint).
  91. Current use is for web fallback auth.
  92. """
  93. PATTERNS = client_v2_patterns(r"/auth/(?P<stagetype>[\w\.]*)/fallback/web")
  94. def __init__(self, hs):
  95. super(AuthRestServlet, self).__init__()
  96. self.hs = hs
  97. self.auth = hs.get_auth()
  98. self.auth_handler = hs.get_auth_handler()
  99. self.registration_handler = hs.get_handlers().registration_handler
  100. @defer.inlineCallbacks
  101. def on_GET(self, request, stagetype):
  102. yield
  103. if stagetype == LoginType.RECAPTCHA:
  104. if ('session' not in request.args or
  105. len(request.args['session']) == 0):
  106. raise SynapseError(400, "No session supplied")
  107. session = request.args["session"][0]
  108. html = RECAPTCHA_TEMPLATE % {
  109. 'session': session,
  110. 'myurl': "%s/auth/%s/fallback/web" % (
  111. CLIENT_V2_ALPHA_PREFIX, LoginType.RECAPTCHA
  112. ),
  113. 'sitekey': self.hs.config.recaptcha_public_key,
  114. }
  115. html_bytes = html.encode("utf8")
  116. request.setResponseCode(200)
  117. request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
  118. request.setHeader(b"Content-Length", b"%d" % (len(html_bytes),))
  119. request.write(html_bytes)
  120. finish_request(request)
  121. defer.returnValue(None)
  122. else:
  123. raise SynapseError(404, "Unknown auth stage type")
  124. @defer.inlineCallbacks
  125. def on_POST(self, request, stagetype):
  126. yield
  127. if stagetype == "m.login.recaptcha":
  128. if ('g-recaptcha-response' not in request.args or
  129. len(request.args['g-recaptcha-response'])) == 0:
  130. raise SynapseError(400, "No captcha response supplied")
  131. if ('session' not in request.args or
  132. len(request.args['session'])) == 0:
  133. raise SynapseError(400, "No session supplied")
  134. session = request.args['session'][0]
  135. authdict = {
  136. 'response': request.args['g-recaptcha-response'][0],
  137. 'session': session,
  138. }
  139. success = yield self.auth_handler.add_oob_auth(
  140. LoginType.RECAPTCHA,
  141. authdict,
  142. self.hs.get_ip_from_request(request)
  143. )
  144. if success:
  145. html = SUCCESS_TEMPLATE
  146. else:
  147. html = RECAPTCHA_TEMPLATE % {
  148. 'session': session,
  149. 'myurl': "%s/auth/%s/fallback/web" % (
  150. CLIENT_V2_ALPHA_PREFIX, LoginType.RECAPTCHA
  151. ),
  152. 'sitekey': self.hs.config.recaptcha_public_key,
  153. }
  154. html_bytes = html.encode("utf8")
  155. request.setResponseCode(200)
  156. request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
  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)