2012-10-10 16:59:59 +02:00
|
|
|
import base64
|
2020-06-11 00:54:34 +02:00
|
|
|
import hashlib
|
2016-06-04 20:38:42 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
2016-06-04 20:38:42 +02:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def initial_password(email: str) -> str | None:
|
2012-10-10 16:59:59 +02:00
|
|
|
"""Given an email address, returns the initial password for that account, as
|
2021-02-12 08:19:30 +01:00
|
|
|
created by populate_db."""
|
2012-10-10 16:59:59 +02:00
|
|
|
|
2013-11-12 18:20:05 +01:00
|
|
|
if settings.INITIAL_PASSWORD_SALT is not None:
|
2022-03-21 18:28:18 +01:00
|
|
|
# We check settings.DEVELOPMENT, not settings.PRODUCTION,
|
|
|
|
# because some tests mock settings.PRODUCTION and then use
|
|
|
|
# self.login, which will call this function.
|
|
|
|
assert settings.DEVELOPMENT, "initial_password_salt should not be set in production."
|
2021-08-02 23:20:39 +02:00
|
|
|
encoded_key = (settings.INITIAL_PASSWORD_SALT + email).encode()
|
2016-01-24 05:18:35 +01:00
|
|
|
digest = hashlib.sha256(encoded_key).digest()
|
2021-08-02 23:20:39 +02:00
|
|
|
return base64.b64encode(digest)[:16].decode()
|
2013-11-12 18:20:05 +01:00
|
|
|
else:
|
|
|
|
# None as a password for a user tells Django to set an unusable password
|
2016-01-24 05:18:35 +01:00
|
|
|
return None
|