Ticket #6858: legacy_auth.diff
File legacy_auth.diff, 2.9 KB (added by , 17 years ago) |
---|
-
django/contrib/auth/backends.py
1 1 from django.db import connection 2 from django.contrib.auth.models import get_hexdigest 2 3 from django.contrib.auth.models import User 3 4 4 5 try: … … 68 69 return User.objects.get(pk=user_id) 69 70 except User.DoesNotExist: 70 71 return None 72 73 74 class LegacyMD5ModelBackend(ModelBackend): 75 """ 76 Backend which provides legacy support for very old Django 77 installations. Otherwise, identical to ``ModelBackend``. 78 79 Originally, passwords were stored as unsalted MD5 hashes; this 80 backend can authenticate users whose passwords were stored in this 81 fashion, and will transparently change them to use salted SHA1 82 hashes instead. 83 84 This primarily affects sites which have had active users since the 85 Django 0.90 release; sites which launched on Django 0.91 or later 86 will not need this, as the current salted-hash system was 87 introduced in SVN revision 1327, and Django 0.91 was packaged from 88 SVN revision 1908. 89 90 """ 91 def authenticate(self, username=None, password=None): 92 if password is None or '$' in password: 93 return None 94 try: 95 user = User.objects.get(username=username) 96 if user.password == get_hexdigest('md5', '', password): 97 # Legacy unsalted md5 password; convert to new format. 98 user.set_password(password) 99 user.save() 100 return user 101 except User.DoesNotExist: 102 return None -
django/contrib/auth/models.py
51 51 Returns a boolean of whether the raw_password was correct. Handles 52 52 encryption formats behind the scenes. 53 53 """ 54 if '$' not in enc_password: 55 return False 54 56 algo, salt, hsh = enc_password.split('$') 55 57 return hsh == get_hexdigest(algo, salt, raw_password) 56 58 … … 192 194 Returns a boolean of whether the raw_password was correct. Handles 193 195 encryption formats behind the scenes. 194 196 """ 195 # Backwards-compatibility check. Older passwords won't include the196 # algorithm or salt.197 if '$' not in self.password:198 is_correct = (self.password == get_hexdigest('md5', '', raw_password))199 if is_correct:200 # Convert the password to the new, more secure format.201 self.set_password(raw_password)202 self.save()203 return is_correct204 197 return check_password(raw_password, self.password) 205 198 206 199 def set_unusable_password(self):