thirdparty.py 3.4 KB

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