__init__.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 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. from twisted.internet import defer
  16. from synapse.types import UserID
  17. class ModuleApi(object):
  18. """A proxy object that gets passed to password auth providers so they
  19. can register new users etc if necessary.
  20. """
  21. def __init__(self, hs, auth_handler):
  22. self.hs = hs
  23. self._store = hs.get_datastore()
  24. self._auth = hs.get_auth()
  25. self._auth_handler = auth_handler
  26. def get_user_by_req(self, req, allow_guest=False):
  27. """Check the access_token provided for a request
  28. Args:
  29. req (twisted.web.server.Request): Incoming HTTP request
  30. allow_guest (bool): True if guest users should be allowed. If this
  31. is False, and the access token is for a guest user, an
  32. AuthError will be thrown
  33. Returns:
  34. twisted.internet.defer.Deferred[synapse.types.Requester]:
  35. the requester for this request
  36. Raises:
  37. synapse.api.errors.AuthError: if no user by that token exists,
  38. or the token is invalid.
  39. """
  40. return self._auth.get_user_by_req(req, allow_guest)
  41. def get_qualified_user_id(self, username):
  42. """Qualify a user id, if necessary
  43. Takes a user id provided by the user and adds the @ and :domain to
  44. qualify it, if necessary
  45. Args:
  46. username (str): provided user id
  47. Returns:
  48. str: qualified @user:id
  49. """
  50. if username.startswith('@'):
  51. return username
  52. return UserID(username, self.hs.hostname).to_string()
  53. def check_user_exists(self, user_id):
  54. """Check if user exists.
  55. Args:
  56. user_id (str): Complete @user:id
  57. Returns:
  58. Deferred[str|None]: Canonical (case-corrected) user_id, or None
  59. if the user is not registered.
  60. """
  61. return self._auth_handler.check_user_exists(user_id)
  62. def register(self, localpart):
  63. """Registers a new user with given localpart
  64. Returns:
  65. Deferred: a 2-tuple of (user_id, access_token)
  66. """
  67. reg = self.hs.get_handlers().registration_handler
  68. return reg.register(localpart=localpart)
  69. @defer.inlineCallbacks
  70. def invalidate_access_token(self, access_token):
  71. """Invalidate an access token for a user
  72. Args:
  73. access_token(str): access token
  74. Returns:
  75. twisted.internet.defer.Deferred - resolves once the access token
  76. has been removed.
  77. Raises:
  78. synapse.api.errors.AuthError: the access token is invalid
  79. """
  80. # see if the access token corresponds to a device
  81. user_info = yield self._auth.get_user_by_access_token(access_token)
  82. device_id = user_info.get("device_id")
  83. user_id = user_info["user"].to_string()
  84. if device_id:
  85. # delete the device, which will also delete its access tokens
  86. yield self.hs.get_device_handler().delete_device(user_id, device_id)
  87. else:
  88. # no associated device. Just delete the access token.
  89. yield self._auth_handler.delete_access_token(access_token)
  90. def run_db_interaction(self, desc, func, *args, **kwargs):
  91. """Run a function with a database connection
  92. Args:
  93. desc (str): description for the transaction, for metrics etc
  94. func (func): function to be run. Passed a database cursor object
  95. as well as *args and **kwargs
  96. *args: positional args to be passed to func
  97. **kwargs: named args to be passed to func
  98. Returns:
  99. Deferred[object]: result of func
  100. """
  101. return self._store.runInteraction(desc, func, *args, **kwargs)