httpserver.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 OpenMarket Ltd
  3. # Copyright 2018 New Vector Ltd
  4. # Copyright 2019 The Matrix.org Foundation C.I.C.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. from __future__ import absolute_import
  18. from twisted.web.server import Site
  19. from twisted.web.resource import Resource
  20. import logging
  21. import twisted.internet.ssl
  22. from sydent.http.servlets.authenticated_bind_threepid_servlet import (
  23. AuthenticatedBindThreePidServlet,
  24. )
  25. from sydent.http.servlets.authenticated_unbind_threepid_servlet import (
  26. AuthenticatedUnbindThreePidServlet,
  27. )
  28. from sydent.http.httpcommon import SizeLimitingRequest
  29. logger = logging.getLogger(__name__)
  30. class ClientApiHttpServer:
  31. def __init__(self, sydent):
  32. self.sydent = sydent
  33. root = Resource()
  34. matrix = Resource()
  35. identity = Resource()
  36. api = Resource()
  37. v1 = self.sydent.servlets.v1
  38. v2 = self.sydent.servlets.v2
  39. validate = Resource()
  40. validate_v2 = Resource()
  41. email = Resource()
  42. email_v2 = Resource()
  43. msisdn = Resource()
  44. msisdn_v2 = Resource()
  45. info = self.sydent.servlets.info
  46. internalInfo = self.sydent.servlets.internalInfo
  47. threepid_v1 = Resource()
  48. threepid_v2 = Resource()
  49. unbind = self.sydent.servlets.threepidUnbind
  50. pubkey = Resource()
  51. ephemeralPubkey = Resource()
  52. userDirectory = Resource()
  53. root.putChild(b'_matrix', matrix)
  54. matrix.putChild(b'identity', identity)
  55. identity.putChild(b'api', api)
  56. identity.putChild(b'v2', v2)
  57. api.putChild(b'v1', v1)
  58. validate.putChild(b'email', email)
  59. validate.putChild(b'msisdn', msisdn)
  60. validate_v2.putChild(b'email', email_v2)
  61. validate_v2.putChild(b'msisdn', msisdn_v2)
  62. v1.putChild(b'validate', validate)
  63. v1.putChild(b'lookup', self.sydent.servlets.lookup)
  64. v1.putChild(b'bulk_lookup', self.sydent.servlets.bulk_lookup)
  65. v1.putChild(b'pubkey', pubkey)
  66. pubkey.putChild(b'isvalid', self.sydent.servlets.pubkeyIsValid)
  67. pubkey.putChild(b'ed25519:0', self.sydent.servlets.pubkey_ed25519)
  68. pubkey.putChild(b'ephemeral', ephemeralPubkey)
  69. ephemeralPubkey.putChild(b'isvalid', self.sydent.servlets.ephemeralPubkeyIsValid)
  70. threepid_v2.putChild(b'getValidated3pid', self.sydent.servlets.getValidated3pidV2)
  71. threepid_v2.putChild(b'bind', self.sydent.servlets.threepidBindV2)
  72. threepid_v2.putChild(b'unbind', unbind)
  73. threepid_v1.putChild(b'getValidated3pid', self.sydent.servlets.getValidated3pid)
  74. threepid_v1.putChild(b'unbind', unbind)
  75. if self.sydent.enable_v1_associations:
  76. threepid_v1.putChild(b'bind', self.sydent.servlets.threepidBind)
  77. v1.putChild(b'3pid', threepid_v1)
  78. v1.putChild(b'info', info)
  79. v1.putChild(b'internal-info', internalInfo)
  80. email.putChild(b'requestToken', self.sydent.servlets.emailRequestCode)
  81. email.putChild(b'submitToken', self.sydent.servlets.emailValidate)
  82. email_v2.putChild(b'requestToken', self.sydent.servlets.emailRequestCodeV2)
  83. email_v2.putChild(b'submitToken', self.sydent.servlets.emailValidateV2)
  84. msisdn.putChild(b'requestToken', self.sydent.servlets.msisdnRequestCode)
  85. msisdn.putChild(b'submitToken', self.sydent.servlets.msisdnValidate)
  86. msisdn_v2.putChild(b'requestToken', self.sydent.servlets.msisdnRequestCodeV2)
  87. msisdn_v2.putChild(b'submitToken', self.sydent.servlets.msisdnValidateV2)
  88. v1.putChild(b'store-invite', self.sydent.servlets.storeInviteServlet)
  89. v1.putChild(b'sign-ed25519', self.sydent.servlets.blindlySignStuffServlet)
  90. v1.putChild(b'user_directory', userDirectory)
  91. userDirectory.putChild(b'search', self.sydent.servlets.userDirectorySearchServlet)
  92. v1.putChild(b'replicate_profiles', self.sydent.servlets.profileReplicationServlet)
  93. # v2
  94. # note v2 loses the /api so goes on 'identity' not 'api'
  95. identity.putChild(b'v2', v2)
  96. # v2 exclusive APIs
  97. v2.putChild(b'terms', self.sydent.servlets.termsServlet)
  98. account = self.sydent.servlets.accountServlet
  99. v2.putChild(b'account', account)
  100. account.putChild(b'register', self.sydent.servlets.registerServlet)
  101. account.putChild(b'logout', self.sydent.servlets.logoutServlet)
  102. # v2 versions of existing APIs
  103. v2.putChild(b'validate', validate_v2)
  104. v2.putChild(b'pubkey', pubkey)
  105. v2.putChild(b'3pid', threepid_v2)
  106. v2.putChild(b'store-invite', self.sydent.servlets.storeInviteServletV2)
  107. v2.putChild(b'sign-ed25519', self.sydent.servlets.blindlySignStuffServletV2)
  108. v2.putChild(b'lookup', self.sydent.servlets.lookup_v2)
  109. v2.putChild(b'hash_details', self.sydent.servlets.hash_details)
  110. self.factory = Site(root)
  111. self.factory.requestFactory = SizeLimitingRequest
  112. self.factory.displayTracebacks = False
  113. def setup(self):
  114. httpPort = int(self.sydent.cfg.get('http', 'clientapi.http.port'))
  115. interface = self.sydent.cfg.get('http', 'clientapi.http.bind_address')
  116. logger.info("Starting Client API HTTP server on %s:%d", interface, httpPort)
  117. self.sydent.reactor.listenTCP(
  118. httpPort, self.factory, interface=interface,
  119. )
  120. class InternalApiHttpServer(object):
  121. def __init__(self, sydent):
  122. self.sydent = sydent
  123. def setup(self, interface, port):
  124. logger.info("Starting Internal API HTTP server on %s:%d", interface, port)
  125. root = Resource()
  126. matrix = Resource()
  127. root.putChild(b'_matrix', matrix)
  128. identity = Resource()
  129. matrix.putChild(b'identity', identity)
  130. internal = Resource()
  131. identity.putChild(b'internal', internal)
  132. authenticated_bind = AuthenticatedBindThreePidServlet(self.sydent)
  133. internal.putChild(b'bind', authenticated_bind)
  134. authenticated_unbind = AuthenticatedUnbindThreePidServlet(self.sydent)
  135. internal.putChild(b'unbind', authenticated_unbind)
  136. factory = Site(root)
  137. factory.displayTracebacks = False
  138. self.sydent.reactor.listenTCP(port, factory, interface=interface)
  139. class ReplicationHttpsServer:
  140. def __init__(self, sydent):
  141. self.sydent = sydent
  142. root = Resource()
  143. matrix = Resource()
  144. identity = Resource()
  145. root.putChild(b'_matrix', matrix)
  146. matrix.putChild(b'identity', identity)
  147. replicate = Resource()
  148. replV1 = Resource()
  149. identity.putChild(b'replicate', replicate)
  150. replicate.putChild(b'v1', replV1)
  151. replV1.putChild(b'push', self.sydent.servlets.replicationPush)
  152. self.factory = Site(root)
  153. self.factory.displayTracebacks = False
  154. def setup(self):
  155. httpPort = int(self.sydent.cfg.get('http', 'replication.https.port'))
  156. interface = self.sydent.cfg.get('http', 'replication.https.bind_address')
  157. if self.sydent.sslComponents.myPrivateCertificate:
  158. # We will already have logged a warn if this is absent, so don't do it again
  159. cert = self.sydent.sslComponents.myPrivateCertificate
  160. certOptions = twisted.internet.ssl.CertificateOptions(privateKey=cert.privateKey.original,
  161. certificate=cert.original,
  162. trustRoot=self.sydent.sslComponents.trustRoot)
  163. logger.info("Loaded server private key and certificate!")
  164. logger.info("Starting Replication HTTPS server on %s:%d", interface, httpPort)
  165. self.sydent.reactor.listenSSL(httpPort, self.factory, certOptions,
  166. interface=interface)