directory.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014, 2015 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. from twisted.internet import defer
  16. from synapse.api.errors import AuthError, SynapseError, Codes
  17. from synapse.types import RoomAlias
  18. from .base import ClientV1RestServlet, client_path_pattern
  19. import simplejson as json
  20. import logging
  21. logger = logging.getLogger(__name__)
  22. def register_servlets(hs, http_server):
  23. ClientDirectoryServer(hs).register(http_server)
  24. class ClientDirectoryServer(ClientV1RestServlet):
  25. PATTERN = client_path_pattern("/directory/room/(?P<room_alias>[^/]*)$")
  26. @defer.inlineCallbacks
  27. def on_GET(self, request, room_alias):
  28. room_alias = RoomAlias.from_string(room_alias)
  29. dir_handler = self.handlers.directory_handler
  30. res = yield dir_handler.get_association(room_alias)
  31. defer.returnValue((200, res))
  32. @defer.inlineCallbacks
  33. def on_PUT(self, request, room_alias):
  34. content = _parse_json(request)
  35. if "room_id" not in content:
  36. raise SynapseError(400, "Missing room_id key",
  37. errcode=Codes.BAD_JSON)
  38. logger.debug("Got content: %s", content)
  39. room_alias = RoomAlias.from_string(room_alias)
  40. logger.debug("Got room name: %s", room_alias.to_string())
  41. room_id = content["room_id"]
  42. servers = content["servers"] if "servers" in content else None
  43. logger.debug("Got room_id: %s", room_id)
  44. logger.debug("Got servers: %s", servers)
  45. # TODO(erikj): Check types.
  46. # TODO(erikj): Check that room exists
  47. dir_handler = self.handlers.directory_handler
  48. try:
  49. # try to auth as a user
  50. user, _ = yield self.auth.get_user_by_req(request)
  51. try:
  52. user_id = user.to_string()
  53. yield dir_handler.create_association(
  54. user_id, room_alias, room_id, servers
  55. )
  56. yield dir_handler.send_room_alias_update_event(user_id, room_id)
  57. except SynapseError as e:
  58. raise e
  59. except:
  60. logger.exception("Failed to create association")
  61. raise
  62. except AuthError:
  63. # try to auth as an application service
  64. service = yield self.auth.get_appservice_by_req(request)
  65. yield dir_handler.create_appservice_association(
  66. service, room_alias, room_id, servers
  67. )
  68. logger.info(
  69. "Application service at %s created alias %s pointing to %s",
  70. service.url,
  71. room_alias.to_string(),
  72. room_id
  73. )
  74. defer.returnValue((200, {}))
  75. @defer.inlineCallbacks
  76. def on_DELETE(self, request, room_alias):
  77. dir_handler = self.handlers.directory_handler
  78. try:
  79. service = yield self.auth.get_appservice_by_req(request)
  80. room_alias = RoomAlias.from_string(room_alias)
  81. yield dir_handler.delete_appservice_association(
  82. service, room_alias
  83. )
  84. logger.info(
  85. "Application service at %s deleted alias %s",
  86. service.url,
  87. room_alias.to_string()
  88. )
  89. defer.returnValue((200, {}))
  90. except AuthError:
  91. # fallback to default user behaviour if they aren't an AS
  92. pass
  93. user, _ = yield self.auth.get_user_by_req(request)
  94. is_admin = yield self.auth.is_server_admin(user)
  95. if not is_admin:
  96. raise AuthError(403, "You need to be a server admin")
  97. room_alias = RoomAlias.from_string(room_alias)
  98. yield dir_handler.delete_association(
  99. user.to_string(), room_alias
  100. )
  101. logger.info(
  102. "User %s deleted alias %s",
  103. user.to_string(),
  104. room_alias.to_string()
  105. )
  106. defer.returnValue((200, {}))
  107. def _parse_json(request):
  108. try:
  109. content = json.loads(request.content.read())
  110. if type(content) != dict:
  111. raise SynapseError(400, "Content must be a JSON object.",
  112. errcode=Codes.NOT_JSON)
  113. return content
  114. except ValueError:
  115. raise SynapseError(400, "Content not JSON.", errcode=Codes.NOT_JSON)