auth_blocking.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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.api.constants import LimitBlockingTypes, UserTypes
  18. from synapse.api.errors import Codes, ResourceLimitError
  19. from synapse.config.server import is_threepid_reserved
  20. logger = logging.getLogger(__name__)
  21. class AuthBlocking(object):
  22. def __init__(self, hs):
  23. self.store = hs.get_datastore()
  24. self._server_notices_mxid = hs.config.server_notices_mxid
  25. self._hs_disabled = hs.config.hs_disabled
  26. self._hs_disabled_message = hs.config.hs_disabled_message
  27. self._admin_contact = hs.config.admin_contact
  28. self._max_mau_value = hs.config.max_mau_value
  29. self._limit_usage_by_mau = hs.config.limit_usage_by_mau
  30. self._mau_limits_reserved_threepids = hs.config.mau_limits_reserved_threepids
  31. @defer.inlineCallbacks
  32. def check_auth_blocking(self, user_id=None, threepid=None, user_type=None):
  33. """Checks if the user should be rejected for some external reason,
  34. such as monthly active user limiting or global disable flag
  35. Args:
  36. user_id(str|None): If present, checks for presence against existing
  37. MAU cohort
  38. threepid(dict|None): If present, checks for presence against configured
  39. reserved threepid. Used in cases where the user is trying register
  40. with a MAU blocked server, normally they would be rejected but their
  41. threepid is on the reserved list. user_id and
  42. threepid should never be set at the same time.
  43. user_type(str|None): If present, is used to decide whether to check against
  44. certain blocking reasons like MAU.
  45. """
  46. # Never fail an auth check for the server notices users or support user
  47. # This can be a problem where event creation is prohibited due to blocking
  48. if user_id is not None:
  49. if user_id == self._server_notices_mxid:
  50. return
  51. if (yield self.store.is_support_user(user_id)):
  52. return
  53. if self._hs_disabled:
  54. raise ResourceLimitError(
  55. 403,
  56. self._hs_disabled_message,
  57. errcode=Codes.RESOURCE_LIMIT_EXCEEDED,
  58. admin_contact=self._admin_contact,
  59. limit_type=LimitBlockingTypes.HS_DISABLED,
  60. )
  61. if self._limit_usage_by_mau is True:
  62. assert not (user_id and threepid)
  63. # If the user is already part of the MAU cohort or a trial user
  64. if user_id:
  65. timestamp = yield self.store.user_last_seen_monthly_active(user_id)
  66. if timestamp:
  67. return
  68. is_trial = yield self.store.is_trial_user(user_id)
  69. if is_trial:
  70. return
  71. elif threepid:
  72. # If the user does not exist yet, but is signing up with a
  73. # reserved threepid then pass auth check
  74. if is_threepid_reserved(self._mau_limits_reserved_threepids, threepid):
  75. return
  76. elif user_type == UserTypes.SUPPORT:
  77. # If the user does not exist yet and is of type "support",
  78. # allow registration. Support users are excluded from MAU checks.
  79. return
  80. # Else if there is no room in the MAU bucket, bail
  81. current_mau = yield self.store.get_monthly_active_count()
  82. if current_mau >= self._max_mau_value:
  83. raise ResourceLimitError(
  84. 403,
  85. "Monthly Active User Limit Exceeded",
  86. admin_contact=self._admin_contact,
  87. errcode=Codes.RESOURCE_LIMIT_EXCEEDED,
  88. limit_type=LimitBlockingTypes.MONTHLY_ACTIVE_USER,
  89. )