Ensure usernames and domains are lowercase

This commit is contained in:
Andrew Godwin 2022-11-24 16:27:21 -07:00
parent ec634f2ad3
commit 3a608c2012
2 changed files with 6 additions and 4 deletions

View File

@ -59,13 +59,14 @@ class Domain(models.Model):
@classmethod @classmethod
def get_remote_domain(cls, domain: str) -> "Domain": def get_remote_domain(cls, domain: str) -> "Domain":
return cls.objects.get_or_create(domain=domain, local=False)[0] return cls.objects.get_or_create(domain=domain.lower(), local=False)[0]
@classmethod @classmethod
def get_domain(cls, domain: str) -> Optional["Domain"]: def get_domain(cls, domain: str) -> Optional["Domain"]:
try: try:
return cls.objects.get( return cls.objects.get(
models.Q(domain=domain) | models.Q(service_domain=domain) models.Q(domain=domain.lower())
| models.Q(service_domain=domain.lower())
) )
except cls.DoesNotExist: except cls.DoesNotExist:
return None return None

View File

@ -153,6 +153,7 @@ class Identity(StatorModel):
if username.startswith("@"): if username.startswith("@"):
raise ValueError("Username must not start with @") raise ValueError("Username must not start with @")
username = username.lower() username = username.lower()
domain = domain.lower()
try: try:
if local: if local:
return cls.objects.get(username=username, domain_id=domain, local=True) return cls.objects.get(username=username, domain_id=domain, local=True)
@ -300,7 +301,7 @@ class Identity(StatorModel):
Given a username@domain handle, returns a tuple of Given a username@domain handle, returns a tuple of
(actor uri, canonical handle) or None, None if it does not resolve. (actor uri, canonical handle) or None, None if it does not resolve.
""" """
domain = handle.split("@")[1] domain = handle.split("@")[1].lower()
try: try:
response = await SystemActor().signed_request( response = await SystemActor().signed_request(
method="get", method="get",
@ -381,7 +382,7 @@ class Identity(StatorModel):
) )
if webfinger_handle: if webfinger_handle:
webfinger_username, webfinger_domain = webfinger_handle.split("@") webfinger_username, webfinger_domain = webfinger_handle.split("@")
self.username = webfinger_username self.username = webfinger_username.lower()
self.domain = await get_domain(webfinger_domain) self.domain = await get_domain(webfinger_domain)
else: else:
self.domain = await get_domain(actor_url_parts.hostname) self.domain = await get_domain(actor_url_parts.hostname)