Ticket #5600: django_auth_enhancements.diff
File django_auth_enhancements.diff, 6.0 KB (added by , 17 years ago) |
---|
-
django/conf/global_settings.py
326 326 327 327 AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',) 328 328 329 AUTH_CRYPTO_ALGORITHM = 'sha1' 330 329 331 LOGIN_URL = '/accounts/login/' 330 332 331 333 LOGOUT_URL = '/accounts/logout/' -
django/contrib/auth/models.py
1 from django.conf import settings 1 2 from django.contrib import auth 2 3 from django.core import validators 3 4 from django.core.exceptions import ImproperlyConfigured … … 8 9 from django.utils.translation import ugettext_lazy as _ 9 10 import datetime 10 11 import urllib 12 import base64 11 13 12 14 UNUSABLE_PASSWORD = '!' # This will never be a valid hash 15 SALT_CHOICES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' 13 16 14 17 try: 15 18 set … … 19 22 def get_hexdigest(algorithm, salt, raw_password): 20 23 """ 21 24 Returns a string of the hexdigest of the given plaintext password and salt 22 using the given algorithm ('md5', 'sha1' or 'crypt').25 using the given algorithm ('md5', 'sha1', 'sha2', 'sha256', or 'crypt'). 23 26 """ 24 27 raw_password, salt = smart_str(raw_password), smart_str(salt) 25 28 if algorithm == 'crypt': … … 39 42 elif algorithm == 'sha1': 40 43 import sha 41 44 return sha.new(salt + raw_password).hexdigest() 45 else: 46 raise ValueError("Got unsupported password algorithm type in password.") 42 47 else: 43 48 if algorithm == 'md5': 44 49 return hashlib.md5(salt + raw_password).hexdigest() 45 50 elif algorithm == 'sha1': 46 51 return hashlib.sha1(salt + raw_password).hexdigest() 52 elif algorithm == 'sha256' or algorithm == 'sha2': 53 return hashlib.sha256(salt + raw_password).hexdigest() 47 54 raise ValueError("Got unknown password algorithm type in password.") 48 55 49 56 def check_password(raw_password, enc_password): … … 182 189 183 190 def set_password(self, raw_password): 184 191 import random 185 algo = 'sha1'186 salt = get_hexdigest(algo, str(random.random()), str(random.random()))[:5]192 algo = settings.AUTH_CRYPTO_ALGORITHM 193 salt = ''.join([random.choice(SALT_CHOICES) for n in range(10)]) 187 194 hsh = get_hexdigest(algo, salt, raw_password) 188 195 self.password = '%s$%s$%s' % (algo, salt, hsh) 189 196 … … 196 203 # algorithm or salt. 197 204 if '$' not in self.password: 198 205 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 return check_password(raw_password, self.password)206 else: 207 is_correct = check_password(raw_password, self.password) 208 if is_correct: 209 # Convert the password to the new, more secure format. 210 self.convert_password(raw_password) 211 return is_correct 205 212 206 213 def set_unusable_password(self): 207 214 # Sets a value that will never be a valid hash … … 210 217 def has_usable_password(self): 211 218 return self.password != UNUSABLE_PASSWORD 212 219 220 def convert_password(self, raw_password): 221 """ 222 Convert, in place, the user's password (raw_password). This will 223 convert from any earlier version of password form, or to a new 224 crypto algorithm as set in AUTH_CRYPTO_ALGORITHM. This will also 225 expand the potential size of the salt. 226 """ 227 algo, salt, password = self.password.split('$') 228 if algo != settings.AUTH_CRYPTO_ALGORITHM or len(salt) < 10: 229 self.set_password(raw_password) 230 self.save() 231 213 232 def get_group_permissions(self): 214 233 """ 215 234 Returns a list of permission strings that this user has through -
docs/settings.txt
225 225 ``CommonMiddleware`` is installed (see the `middleware docs`_). See also 226 226 ``PREPEND_WWW``. 227 227 228 AUTH_CRYPTO_ALGORITHM 229 --------------------- 230 231 Default: ``sha1`` 232 233 Determines the cryptograph algorithm to use in storing user passwords in the 234 database. Available options are ``sha1``, ``sha2``, ``sha256``, ``md5`` and 235 ``crypto``. Note that ``sha2`` and ``sha256`` are synonyms. 236 228 237 CACHE_BACKEND 229 238 ------------- 230 239 -
docs/authentication.txt
218 218 219 219 That's hashtype, salt and hash, separated by the dollar-sign character. 220 220 221 Hashtype is either ``sha1`` (default), ``md5`` or ``crypt`` -- the algorithm 222 used to perform a one-way hash of the password. Salt is a random string used 223 to salt the raw password to create the hash. Note that the ``crypt`` method is 224 only supported on platforms that have the standard Python ``crypt`` module 225 available, and ``crypt`` support is only available in the Django development 226 version. 221 Hashtype is either ``sha1`` (default), ``sha2``, ``sha256``, ``md5`` or 222 ``crypt`` -- the algorithm used, based on the `AUTH_CRYPTO_ALGORITHM`_ 223 setting, to perform a one-way hash of the password. Salt is a random string 224 used to salt the raw password to create the hash. Note that the ``crypt`` 225 method is only supported on platforms that have the standard Python ``crypt`` 226 module available, and ``crypt`` support is only available in the Django 227 development version. 227 228 228 229 For example:: 229 230 … … 237 238 converted automatically to the new style the first time ``User.check_password()`` 238 239 works correctly for a given user. 239 240 241 .. _AUTH_CRYPTO_ALGORITHM: ../settings/#auth-crypto-algorithm 242 240 243 Anonymous users 241 244 --------------- 242 245