2020-06-11 00:54:34 +02:00
|
|
|
import hashlib
|
|
|
|
|
2016-09-28 00:12:18 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
2017-11-27 05:27:04 +01:00
|
|
|
from zerver.models import UserProfile
|
2017-03-05 05:24:47 +01:00
|
|
|
|
2016-09-28 00:12:18 +02:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def gravatar_hash(email: str) -> str:
|
2016-09-28 00:12:18 +02:00
|
|
|
"""Compute the Gravatar hash for an email address."""
|
|
|
|
# Non-ASCII characters aren't permitted by the currently active e-mail
|
|
|
|
# RFCs. However, the IETF has published https://tools.ietf.org/html/rfc4952,
|
|
|
|
# outlining internationalization of email addresses, and regardless if we
|
|
|
|
# typo an address or someone manages to give us a non-ASCII address, let's
|
|
|
|
# not error out on it.
|
2023-07-19 00:44:51 +02:00
|
|
|
return hashlib.md5(email.lower().encode()).hexdigest()
|
2016-09-28 00:12:18 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-06-13 14:57:18 +02:00
|
|
|
def user_avatar_hash(uid: str, version: str) -> str:
|
2017-06-27 00:12:15 +02:00
|
|
|
# WARNING: If this method is changed, you may need to do a migration
|
|
|
|
# similar to zerver/migrations/0060_move_avatars_to_be_uid_based.py .
|
|
|
|
|
2024-06-13 14:57:18 +02:00
|
|
|
# The salt prevents unauthenticated clients from enumerating the
|
|
|
|
# avatars of all users.
|
|
|
|
user_key = uid + ":" + version + ":" + settings.AVATAR_SALT
|
|
|
|
return hashlib.sha256(user_key.encode()).hexdigest()[:40]
|
2017-03-02 23:45:57 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-06-13 14:57:18 +02:00
|
|
|
def user_avatar_path(user_profile: UserProfile, future: bool = False) -> str:
|
|
|
|
# 'future' is if this is for the current avatar version, of the next one.
|
|
|
|
return user_avatar_base_path_from_ids(
|
|
|
|
user_profile.id, user_profile.avatar_version + (1 if future else 0), user_profile.realm_id
|
|
|
|
)
|
2017-05-10 06:40:07 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-06-13 14:57:18 +02:00
|
|
|
def user_avatar_base_path_from_ids(user_profile_id: int, version: int, realm_id: int) -> str:
|
|
|
|
user_id_hash = user_avatar_hash(str(user_profile_id), str(version))
|
2023-05-27 02:30:33 +02:00
|
|
|
return f"{realm_id}/{user_id_hash}"
|
2019-06-28 00:10:58 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-06-28 00:10:58 +02:00
|
|
|
def user_avatar_content_hash(ldap_avatar: bytes) -> str:
|
|
|
|
return hashlib.sha256(ldap_avatar).hexdigest()
|