Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

118 Zeilen
3.6 KiB

  1. # Copyright 2018 New Vector 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. import re
  16. import typing
  17. if typing.TYPE_CHECKING:
  18. from synapse.server import HomeServer
  19. logger = logging.getLogger(__name__)
  20. # it's unclear what the maximum length of an email address is. RFC3696 (as corrected
  21. # by errata) says:
  22. # the upper limit on address lengths should normally be considered to be 254.
  23. #
  24. # In practice, mail servers appear to be more tolerant and allow 400 characters
  25. # or so. Let's allow 500, which should be plenty for everyone.
  26. #
  27. MAX_EMAIL_ADDRESS_LENGTH = 500
  28. async def check_3pid_allowed(
  29. hs: "HomeServer",
  30. medium: str,
  31. address: str,
  32. registration: bool = False,
  33. ) -> bool:
  34. """Checks whether a given format of 3PID is allowed to be used on this HS
  35. Args:
  36. hs: server
  37. medium: 3pid medium - e.g. email, msisdn
  38. address: address within that medium (e.g. "wotan@matrix.org")
  39. msisdns need to first have been canonicalised
  40. registration: whether we want to bind the 3PID as part of registering a new user.
  41. Returns:
  42. whether the 3PID medium/address is allowed to be added to this HS
  43. """
  44. if not await hs.get_password_auth_provider().is_3pid_allowed(
  45. medium, address, registration
  46. ):
  47. return False
  48. if hs.config.registration.allowed_local_3pids:
  49. for constraint in hs.config.registration.allowed_local_3pids:
  50. logger.debug(
  51. "Checking 3PID %s (%s) against %s (%s)",
  52. address,
  53. medium,
  54. constraint["pattern"],
  55. constraint["medium"],
  56. )
  57. if medium == constraint["medium"] and re.match(
  58. constraint["pattern"], address
  59. ):
  60. return True
  61. else:
  62. return True
  63. return False
  64. def canonicalise_email(address: str) -> str:
  65. """'Canonicalise' email address
  66. Case folding of local part of email address and lowercase domain part
  67. See MSC2265, https://github.com/matrix-org/matrix-doc/pull/2265
  68. Args:
  69. address: email address to be canonicalised
  70. Returns:
  71. The canonical form of the email address
  72. Raises:
  73. ValueError if the address could not be parsed.
  74. """
  75. address = address.strip()
  76. parts = address.split("@")
  77. if len(parts) != 2:
  78. logger.debug("Couldn't parse email address %s", address)
  79. raise ValueError("Unable to parse email address")
  80. return parts[0].casefold() + "@" + parts[1].lower()
  81. def validate_email(address: str) -> str:
  82. """Does some basic validation on an email address.
  83. Returns the canonicalised email, as returned by `canonicalise_email`.
  84. Raises a ValueError if the email is invalid.
  85. """
  86. # First we try canonicalising in case that fails
  87. address = canonicalise_email(address)
  88. # Email addresses have to be at least 3 characters.
  89. if len(address) < 3:
  90. raise ValueError("Unable to parse email address")
  91. if len(address) > MAX_EMAIL_ADDRESS_LENGTH:
  92. raise ValueError("Unable to parse email address")
  93. return address