login.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 New Vector 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.http.servlet import parse_json_object_from_request
  17. from synapse.replication.http._base import ReplicationEndpoint
  18. logger = logging.getLogger(__name__)
  19. class RegisterDeviceReplicationServlet(ReplicationEndpoint):
  20. """Ensure a device is registered, generating a new access token for the
  21. device.
  22. Used during registration and login.
  23. """
  24. NAME = "device_check_registered"
  25. PATH_ARGS = ("user_id",)
  26. def __init__(self, hs):
  27. super().__init__(hs)
  28. self.registration_handler = hs.get_registration_handler()
  29. @staticmethod
  30. async def _serialize_payload(
  31. user_id, device_id, initial_display_name, is_guest, is_appservice_ghost
  32. ):
  33. """
  34. Args:
  35. device_id (str|None): Device ID to use, if None a new one is
  36. generated.
  37. initial_display_name (str|None)
  38. is_guest (bool)
  39. """
  40. return {
  41. "device_id": device_id,
  42. "initial_display_name": initial_display_name,
  43. "is_guest": is_guest,
  44. "is_appservice_ghost": is_appservice_ghost,
  45. }
  46. async def _handle_request(self, request, user_id):
  47. content = parse_json_object_from_request(request)
  48. device_id = content["device_id"]
  49. initial_display_name = content["initial_display_name"]
  50. is_guest = content["is_guest"]
  51. is_appservice_ghost = content["is_appservice_ghost"]
  52. device_id, access_token = await self.registration_handler.register_device(
  53. user_id,
  54. device_id,
  55. initial_display_name,
  56. is_guest,
  57. is_appservice_ghost=is_appservice_ghost,
  58. )
  59. return 200, {"device_id": device_id, "access_token": access_token}
  60. def register_servlets(hs, http_server):
  61. RegisterDeviceReplicationServlet(hs).register(http_server)