Skip to content

Commit 8b10999

Browse files
committed
Add SHA1 password migration.
1 parent 70455d1 commit 8b10999

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

profiles/hashers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from django.contrib.auth.hashers import (
2+
PBKDF2PasswordHasher, SHA1PasswordHasher,
3+
)
4+
5+
6+
class PBKDF2WrappedSHA1PasswordHasher(PBKDF2PasswordHasher):
7+
algorithm = 'pbkdf2_wrapped_sha1'
8+
9+
def encode_sha1_hash(self, sha1_hash, salt, iterations=None):
10+
return super().encode(sha1_hash, salt, iterations)
11+
12+
def encode(self, password, salt, iterations=None):
13+
_, _, sha1_hash = SHA1PasswordHasher().encode(password, salt).split('$', 2)
14+
return self.encode_sha1_hash(sha1_hash, salt, iterations)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from django.db import migrations
2+
3+
from ..hashers import PBKDF2WrappedSHA1PasswordHasher
4+
5+
6+
def forwards_func(apps, schema_editor):
7+
User = apps.get_model('profiles', 'User')
8+
users = User.objects.filter(password__startswith='sha1$')
9+
hasher = PBKDF2WrappedSHA1PasswordHasher()
10+
for user in users:
11+
print('Migrating', user.email)
12+
algorithm, salt, sha1_hash = user.password.split('$', 2)
13+
user.password = hasher.encode_sha1_hash(sha1_hash, salt)
14+
user.save(update_fields=['password'])
15+
16+
17+
class Migration(migrations.Migration):
18+
19+
dependencies = [
20+
('profiles', '0004_auto_20190310_1750'),
21+
]
22+
23+
operations = [
24+
migrations.RunPython(forwards_func),
25+
]

theatricalia/settings/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@
178178
if DEBUG:
179179
INSTALLED_APPS.append('debug_toolbar')
180180

181+
PASSWORD_HASHERS = [
182+
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
183+
'profiles.hashers.PBKDF2WrappedSHA1PasswordHasher',
184+
]
185+
181186
from django.db.utils import OperationalError
182187

183188

0 commit comments

Comments
 (0)