thirdparty.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 ThirdPartyEntityKind
  18. from synapse.http.servlet import RestServlet
  19. from ._base import client_patterns
  20. logger = logging.getLogger(__name__)
  21. class ThirdPartyProtocolsServlet(RestServlet):
  22. PATTERNS = client_patterns("/thirdparty/protocols")
  23. def __init__(self, hs):
  24. super(ThirdPartyProtocolsServlet, self).__init__()
  25. self.auth = hs.get_auth()
  26. self.appservice_handler = hs.get_application_service_handler()
  27. @defer.inlineCallbacks
  28. def on_GET(self, request):
  29. yield self.auth.get_user_by_req(request, allow_guest=True)
  30. protocols = yield self.appservice_handler.get_3pe_protocols()
  31. return (200, protocols)
  32. class ThirdPartyProtocolServlet(RestServlet):
  33. PATTERNS = client_patterns("/thirdparty/protocol/(?P<protocol>[^/]+)$")
  34. def __init__(self, hs):
  35. super(ThirdPartyProtocolServlet, self).__init__()
  36. self.auth = hs.get_auth()
  37. self.appservice_handler = hs.get_application_service_handler()
  38. @defer.inlineCallbacks
  39. def on_GET(self, request, protocol):
  40. yield self.auth.get_user_by_req(request, allow_guest=True)
  41. protocols = yield self.appservice_handler.get_3pe_protocols(
  42. only_protocol=protocol
  43. )
  44. if protocol in protocols:
  45. return (200, protocols[protocol])
  46. else:
  47. return (404, {"error": "Unknown protocol"})
  48. class ThirdPartyUserServlet(RestServlet):
  49. PATTERNS = client_patterns("/thirdparty/user(/(?P<protocol>[^/]+))?$")
  50. def __init__(self, hs):
  51. super(ThirdPartyUserServlet, self).__init__()
  52. self.auth = hs.get_auth()
  53. self.appservice_handler = hs.get_application_service_handler()
  54. @defer.inlineCallbacks
  55. def on_GET(self, request, protocol):
  56. yield self.auth.get_user_by_req(request, allow_guest=True)
  57. fields = request.args
  58. fields.pop(b"access_token", None)
  59. results = yield self.appservice_handler.query_3pe(
  60. ThirdPartyEntityKind.USER, protocol, fields
  61. )
  62. return (200, results)
  63. class ThirdPartyLocationServlet(RestServlet):
  64. PATTERNS = client_patterns("/thirdparty/location(/(?P<protocol>[^/]+))?$")
  65. def __init__(self, hs):
  66. super(ThirdPartyLocationServlet, self).__init__()
  67. self.auth = hs.get_auth()
  68. self.appservice_handler = hs.get_application_service_handler()
  69. @defer.inlineCallbacks
  70. def on_GET(self, request, protocol):
  71. yield self.auth.get_user_by_req(request, allow_guest=True)
  72. fields = request.args
  73. fields.pop(b"access_token", None)
  74. results = yield self.appservice_handler.query_3pe(
  75. ThirdPartyEntityKind.LOCATION, protocol, fields
  76. )
  77. return (200, results)
  78. def register_servlets(hs, http_server):
  79. ThirdPartyProtocolsServlet(hs).register(http_server)
  80. ThirdPartyProtocolServlet(hs).register(http_server)
  81. ThirdPartyUserServlet(hs).register(http_server)
  82. ThirdPartyLocationServlet(hs).register(http_server)