threepids.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 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. import re
  17. logger = logging.getLogger(__name__)
  18. def check_3pid_allowed(hs, medium, address):
  19. """Checks whether a given format of 3PID is allowed to be used on this HS
  20. Args:
  21. hs (synapse.server.HomeServer): server
  22. medium (str): 3pid medium - e.g. email, msisdn
  23. address (str): address within that medium (e.g. "wotan@matrix.org")
  24. msisdns need to first have been canonicalised
  25. Returns:
  26. bool: whether the 3PID medium/address is allowed to be added to this HS
  27. """
  28. if hs.config.allowed_local_3pids:
  29. for constraint in hs.config.allowed_local_3pids:
  30. logger.debug(
  31. "Checking 3PID %s (%s) against %s (%s)",
  32. address,
  33. medium,
  34. constraint["pattern"],
  35. constraint["medium"],
  36. )
  37. if medium == constraint["medium"] and re.match(
  38. constraint["pattern"], address
  39. ):
  40. return True
  41. else:
  42. return True
  43. return False