httpserver.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. logger = logging.getLogger(__name__)
  29. class ClientApiHttpServer:
  30. def __init__(self, sydent):
  31. self.sydent = sydent
  32. root = Resource()
  33. matrix = Resource()
  34. identity = Resource()
  35. api = Resource()
  36. v1 = self.sydent.servlets.v1
  37. v2 = self.sydent.servlets.v2
  38. validate = Resource()
  39. email = Resource()
  40. msisdn = Resource()
  41. emailReqCode = self.sydent.servlets.emailRequestCode
  42. emailValCode = self.sydent.servlets.emailValidate
  43. msisdnReqCode = self.sydent.servlets.msisdnRequestCode
  44. msisdnValCode = self.sydent.servlets.msisdnValidate
  45. getValidated3pid = self.sydent.servlets.getValidated3pid
  46. lookup = self.sydent.servlets.lookup
  47. bulk_lookup = self.sydent.servlets.bulk_lookup
  48. info = self.sydent.servlets.info
  49. internalInfo = self.sydent.servlets.internalInfo
  50. hash_details = self.sydent.servlets.hash_details
  51. lookup_v2 = self.sydent.servlets.lookup_v2
  52. threepid_v1 = Resource()
  53. threepid_v2 = Resource()
  54. bind = self.sydent.servlets.threepidBind
  55. unbind = self.sydent.servlets.threepidUnbind
  56. pubkey = Resource()
  57. ephemeralPubkey = Resource()
  58. pk_ed25519 = self.sydent.servlets.pubkey_ed25519
  59. root.putChild(b'_matrix', matrix)
  60. matrix.putChild(b'identity', identity)
  61. identity.putChild(b'api', api)
  62. identity.putChild(b'v2', v2)
  63. api.putChild(b'v1', v1)
  64. validate.putChild(b'email', email)
  65. validate.putChild(b'msisdn', msisdn)
  66. v1.putChild(b'validate', validate)
  67. v1.putChild(b'lookup', lookup)
  68. v1.putChild(b'bulk_lookup', bulk_lookup)
  69. v1.putChild(b'info', info)
  70. v1.putChild(b'internal-info', internalInfo)
  71. v1.putChild(b'pubkey', pubkey)
  72. pubkey.putChild(b'isvalid', self.sydent.servlets.pubkeyIsValid)
  73. pubkey.putChild(b'ed25519:0', pk_ed25519)
  74. pubkey.putChild(b'ephemeral', ephemeralPubkey)
  75. ephemeralPubkey.putChild(b'isvalid', self.sydent.servlets.ephemeralPubkeyIsValid)
  76. threepid_v2.putChild(b'getValidated3pid', getValidated3pid)
  77. threepid_v2.putChild(b'bind', bind)
  78. threepid_v2.putChild(b'unbind', unbind)
  79. threepid_v1.putChild(b'getValidated3pid', getValidated3pid)
  80. threepid_v1.putChild(b'unbind', unbind)
  81. if self.sydent.enable_v1_associations:
  82. threepid_v1.putChild(b'bind', bind)
  83. v1.putChild(b'3pid', threepid_v1)
  84. email.putChild(b'requestToken', emailReqCode)
  85. email.putChild(b'submitToken', emailValCode)
  86. msisdn.putChild(b'requestToken', msisdnReqCode)
  87. msisdn.putChild(b'submitToken', msisdnValCode)
  88. v1.putChild(b'store-invite', self.sydent.servlets.storeInviteServlet)
  89. v1.putChild(b'sign-ed25519', self.sydent.servlets.blindlySignStuffServlet)
  90. # v2
  91. # note v2 loses the /api so goes on 'identity' not 'api'
  92. identity.putChild(b'v2', v2)
  93. # v2 exclusive APIs
  94. v2.putChild(b'terms', self.sydent.servlets.termsServlet)
  95. account = self.sydent.servlets.accountServlet
  96. v2.putChild(b'account', account)
  97. account.putChild(b'register', self.sydent.servlets.registerServlet)
  98. account.putChild(b'logout', self.sydent.servlets.logoutServlet)
  99. # v2 versions of existing APIs
  100. v2.putChild(b'validate', validate)
  101. v2.putChild(b'pubkey', pubkey)
  102. v2.putChild(b'3pid', threepid_v2)
  103. v2.putChild(b'store-invite', self.sydent.servlets.storeInviteServlet)
  104. v2.putChild(b'sign-ed25519', self.sydent.servlets.blindlySignStuffServlet)
  105. v2.putChild(b'lookup', lookup_v2)
  106. v2.putChild(b'hash_details', hash_details)
  107. self.factory = Site(root)
  108. self.factory.displayTracebacks = False
  109. def setup(self):
  110. httpPort = int(self.sydent.cfg.get('http', 'clientapi.http.port'))
  111. interface = self.sydent.cfg.get('http', 'clientapi.http.bind_address')
  112. logger.info("Starting Client API HTTP server on %s:%d", interface, httpPort)
  113. self.sydent.reactor.listenTCP(
  114. httpPort, self.factory, interface=interface,
  115. )
  116. class InternalApiHttpServer(object):
  117. def __init__(self, sydent):
  118. self.sydent = sydent
  119. def setup(self, interface, port):
  120. logger.info("Starting Internal API HTTP server on %s:%d", interface, port)
  121. root = Resource()
  122. matrix = Resource()
  123. root.putChild(b'_matrix', matrix)
  124. identity = Resource()
  125. matrix.putChild(b'identity', identity)
  126. internal = Resource()
  127. identity.putChild(b'internal', internal)
  128. authenticated_bind = AuthenticatedBindThreePidServlet(self.sydent)
  129. internal.putChild(b'bind', authenticated_bind)
  130. authenticated_unbind = AuthenticatedUnbindThreePidServlet(self.sydent)
  131. internal.putChild(b'unbind', authenticated_unbind)
  132. factory = Site(root)
  133. factory.displayTracebacks = False
  134. self.sydent.reactor.listenTCP(port, factory, interface=interface)
  135. class ReplicationHttpsServer:
  136. def __init__(self, sydent):
  137. self.sydent = sydent
  138. root = Resource()
  139. matrix = Resource()
  140. identity = Resource()
  141. root.putChild(b'_matrix', matrix)
  142. matrix.putChild(b'identity', identity)
  143. replicate = Resource()
  144. replV1 = Resource()
  145. identity.putChild(b'replicate', replicate)
  146. replicate.putChild(b'v1', replV1)
  147. replV1.putChild(b'push', self.sydent.servlets.replicationPush)
  148. self.factory = Site(root)
  149. self.factory.displayTracebacks = False
  150. def setup(self):
  151. httpPort = int(self.sydent.cfg.get('http', 'replication.https.port'))
  152. interface = self.sydent.cfg.get('http', 'replication.https.bind_address')
  153. if self.sydent.sslComponents.myPrivateCertificate:
  154. # We will already have logged a warn if this is absent, so don't do it again
  155. cert = self.sydent.sslComponents.myPrivateCertificate
  156. certOptions = twisted.internet.ssl.CertificateOptions(privateKey=cert.privateKey.original,
  157. certificate=cert.original,
  158. trustRoot=self.sydent.sslComponents.trustRoot)
  159. logger.info("Loaded server private key and certificate!")
  160. logger.info("Starting Replication HTTPS server on %s:%d", interface, httpPort)
  161. self.sydent.reactor.listenSSL(httpPort, self.factory, certOptions,
  162. interface=interface)