You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

112 lines
3.4 KiB

  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from synapse.api.constants import ThirdPartyEntityKind
  16. from synapse.http.servlet import RestServlet
  17. from ._base import client_patterns
  18. logger = logging.getLogger(__name__)
  19. class ThirdPartyProtocolsServlet(RestServlet):
  20. PATTERNS = client_patterns("/thirdparty/protocols")
  21. def __init__(self, hs):
  22. super().__init__()
  23. self.auth = hs.get_auth()
  24. self.appservice_handler = hs.get_application_service_handler()
  25. async def on_GET(self, request):
  26. await self.auth.get_user_by_req(request, allow_guest=True)
  27. protocols = await self.appservice_handler.get_3pe_protocols()
  28. return 200, protocols
  29. class ThirdPartyProtocolServlet(RestServlet):
  30. PATTERNS = client_patterns("/thirdparty/protocol/(?P<protocol>[^/]+)$")
  31. def __init__(self, hs):
  32. super().__init__()
  33. self.auth = hs.get_auth()
  34. self.appservice_handler = hs.get_application_service_handler()
  35. async def on_GET(self, request, protocol):
  36. await self.auth.get_user_by_req(request, allow_guest=True)
  37. protocols = await self.appservice_handler.get_3pe_protocols(
  38. only_protocol=protocol
  39. )
  40. if protocol in protocols:
  41. return 200, protocols[protocol]
  42. else:
  43. return 404, {"error": "Unknown protocol"}
  44. class ThirdPartyUserServlet(RestServlet):
  45. PATTERNS = client_patterns("/thirdparty/user(/(?P<protocol>[^/]+))?$")
  46. def __init__(self, hs):
  47. super().__init__()
  48. self.auth = hs.get_auth()
  49. self.appservice_handler = hs.get_application_service_handler()
  50. async def on_GET(self, request, protocol):
  51. await self.auth.get_user_by_req(request, allow_guest=True)
  52. fields = request.args
  53. fields.pop(b"access_token", None)
  54. results = await self.appservice_handler.query_3pe(
  55. ThirdPartyEntityKind.USER, protocol, fields
  56. )
  57. return 200, results
  58. class ThirdPartyLocationServlet(RestServlet):
  59. PATTERNS = client_patterns("/thirdparty/location(/(?P<protocol>[^/]+))?$")
  60. def __init__(self, hs):
  61. super().__init__()
  62. self.auth = hs.get_auth()
  63. self.appservice_handler = hs.get_application_service_handler()
  64. async def on_GET(self, request, protocol):
  65. await self.auth.get_user_by_req(request, allow_guest=True)
  66. fields = request.args
  67. fields.pop(b"access_token", None)
  68. results = await self.appservice_handler.query_3pe(
  69. ThirdPartyEntityKind.LOCATION, protocol, fields
  70. )
  71. return 200, results
  72. def register_servlets(hs, http_server):
  73. ThirdPartyProtocolsServlet(hs).register(http_server)
  74. ThirdPartyProtocolServlet(hs).register(http_server)
  75. ThirdPartyUserServlet(hs).register(http_server)
  76. ThirdPartyLocationServlet(hs).register(http_server)