2012-10-10 16:59:59 +02:00
|
|
|
import base64
|
2020-06-11 00:54:34 +02:00
|
|
|
import hashlib
|
2018-05-10 19:13:36 +02:00
|
|
|
from typing import Optional
|
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
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def initial_password(email: str) -> Optional[str]:
|
2012-10-10 16:59:59 +02:00
|
|
|
"""Given an email address, returns the initial password for that account, as
|
|
|
|
created by populate_db."""
|
|
|
|
|
2013-11-12 18:20:05 +01:00
|
|
|
if settings.INITIAL_PASSWORD_SALT is not None:
|
2016-01-24 05:18:35 +01:00
|
|
|
encoded_key = (settings.INITIAL_PASSWORD_SALT + email).encode("utf-8")
|
|
|
|
digest = hashlib.sha256(encoded_key).digest()
|
2016-06-17 17:03:09 +02:00
|
|
|
return base64.b64encode(digest)[:16].decode('utf-8')
|
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
|