25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

73 satır
2.5 KiB

  1. # Copyright 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.http.servlet import RestServlet
  16. from synapse.rest.client._base import client_patterns
  17. logger = logging.getLogger(__name__)
  18. class LogoutRestServlet(RestServlet):
  19. PATTERNS = client_patterns("/logout$", v1=True)
  20. def __init__(self, hs):
  21. super().__init__()
  22. self.auth = hs.get_auth()
  23. self._auth_handler = hs.get_auth_handler()
  24. self._device_handler = hs.get_device_handler()
  25. async def on_POST(self, request):
  26. requester = await self.auth.get_user_by_req(request, allow_expired=True)
  27. if requester.device_id is None:
  28. # The access token wasn't associated with a device.
  29. # Just delete the access token
  30. access_token = self.auth.get_access_token_from_request(request)
  31. await self._auth_handler.delete_access_token(access_token)
  32. else:
  33. await self._device_handler.delete_device(
  34. requester.user.to_string(), requester.device_id
  35. )
  36. return 200, {}
  37. class LogoutAllRestServlet(RestServlet):
  38. PATTERNS = client_patterns("/logout/all$", v1=True)
  39. def __init__(self, hs):
  40. super().__init__()
  41. self.auth = hs.get_auth()
  42. self._auth_handler = hs.get_auth_handler()
  43. self._device_handler = hs.get_device_handler()
  44. async def on_POST(self, request):
  45. requester = await self.auth.get_user_by_req(request, allow_expired=True)
  46. user_id = requester.user.to_string()
  47. # first delete all of the user's devices
  48. await self._device_handler.delete_all_devices_for_user(user_id)
  49. # .. and then delete any access tokens which weren't associated with
  50. # devices.
  51. await self._auth_handler.delete_access_tokens_for_user(user_id)
  52. return 200, {}
  53. def register_servlets(hs, http_server):
  54. LogoutRestServlet(hs).register(http_server)
  55. LogoutAllRestServlet(hs).register(http_server)