register.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 twisted.internet import defer
  17. from synapse.http.servlet import parse_json_object_from_request
  18. from synapse.replication.http._base import ReplicationEndpoint
  19. logger = logging.getLogger(__name__)
  20. class ReplicationRegisterServlet(ReplicationEndpoint):
  21. """Register a new user
  22. """
  23. NAME = "register_user"
  24. PATH_ARGS = ("user_id",)
  25. def __init__(self, hs):
  26. super(ReplicationRegisterServlet, self).__init__(hs)
  27. self.store = hs.get_datastore()
  28. self.registration_handler = hs.get_registration_handler()
  29. @staticmethod
  30. def _serialize_payload(
  31. user_id,
  32. password_hash,
  33. was_guest,
  34. make_guest,
  35. appservice_id,
  36. create_profile_with_displayname,
  37. admin,
  38. user_type,
  39. address,
  40. ):
  41. """
  42. Args:
  43. user_id (str): The desired user ID to register.
  44. password_hash (str|None): Optional. The password hash for this user.
  45. was_guest (bool): Optional. Whether this is a guest account being
  46. upgraded to a non-guest account.
  47. make_guest (boolean): True if the the new user should be guest,
  48. false to add a regular user account.
  49. appservice_id (str|None): The ID of the appservice registering the user.
  50. create_profile_with_displayname (unicode|None): Optionally create a
  51. profile for the user, setting their displayname to the given value
  52. admin (boolean): is an admin user?
  53. user_type (str|None): type of user. One of the values from
  54. api.constants.UserTypes, or None for a normal user.
  55. address (str|None): the IP address used to perform the regitration.
  56. """
  57. return {
  58. "password_hash": password_hash,
  59. "was_guest": was_guest,
  60. "make_guest": make_guest,
  61. "appservice_id": appservice_id,
  62. "create_profile_with_displayname": create_profile_with_displayname,
  63. "admin": admin,
  64. "user_type": user_type,
  65. "address": address,
  66. }
  67. @defer.inlineCallbacks
  68. def _handle_request(self, request, user_id):
  69. content = parse_json_object_from_request(request)
  70. yield self.registration_handler.register_with_store(
  71. user_id=user_id,
  72. password_hash=content["password_hash"],
  73. was_guest=content["was_guest"],
  74. make_guest=content["make_guest"],
  75. appservice_id=content["appservice_id"],
  76. create_profile_with_displayname=content["create_profile_with_displayname"],
  77. admin=content["admin"],
  78. user_type=content["user_type"],
  79. address=content["address"],
  80. )
  81. return (200, {})
  82. class ReplicationPostRegisterActionsServlet(ReplicationEndpoint):
  83. """Run any post registration actions
  84. """
  85. NAME = "post_register"
  86. PATH_ARGS = ("user_id",)
  87. def __init__(self, hs):
  88. super(ReplicationPostRegisterActionsServlet, self).__init__(hs)
  89. self.store = hs.get_datastore()
  90. self.registration_handler = hs.get_registration_handler()
  91. @staticmethod
  92. def _serialize_payload(user_id, auth_result, access_token, bind_email, bind_msisdn):
  93. """
  94. Args:
  95. user_id (str): The user ID that consented
  96. auth_result (dict): The authenticated credentials of the newly
  97. registered user.
  98. access_token (str|None): The access token of the newly logged in
  99. device, or None if `inhibit_login` enabled.
  100. bind_email (bool): Whether to bind the email with the identity
  101. server
  102. bind_msisdn (bool): Whether to bind the msisdn with the identity
  103. server
  104. """
  105. return {
  106. "auth_result": auth_result,
  107. "access_token": access_token,
  108. "bind_email": bind_email,
  109. "bind_msisdn": bind_msisdn,
  110. }
  111. @defer.inlineCallbacks
  112. def _handle_request(self, request, user_id):
  113. content = parse_json_object_from_request(request)
  114. auth_result = content["auth_result"]
  115. access_token = content["access_token"]
  116. bind_email = content["bind_email"]
  117. bind_msisdn = content["bind_msisdn"]
  118. yield self.registration_handler.post_registration_actions(
  119. user_id=user_id,
  120. auth_result=auth_result,
  121. access_token=access_token,
  122. bind_email=bind_email,
  123. bind_msisdn=bind_msisdn,
  124. )
  125. return (200, {})
  126. def register_servlets(hs, http_server):
  127. ReplicationRegisterServlet(hs).register(http_server)
  128. ReplicationPostRegisterActionsServlet(hs).register(http_server)