Ticket #273: auth.py.diff

File auth.py.diff, 2.8 KB (added by GomoX <gomo AT datafull DOT com>, 19 years ago)

Patch for trunk/django/models/auth.py

  • auth.py

    old new  
    2929    first_name = meta.CharField(maxlength=30, blank=True)
    3030    last_name = meta.CharField(maxlength=30, blank=True)
    3131    email = meta.EmailField('e-mail address', blank=True)
    32     password_md5 = meta.CharField('password', maxlength=32, help_text="Use an MD5 hash -- not the raw password.")
     32    password = meta.CharField('password', maxlength=128, help_text="Use a hash like '[algo]$[salt]$[hexdigest]'")
    3333    is_staff = meta.BooleanField('staff status', help_text="Designates whether the user can log into this admin site.")
    3434    is_active = meta.BooleanField('active', default=True)
    3535    is_superuser = meta.BooleanField('superuser status')
     
    4646        exceptions = ('SiteProfileNotAvailable',)
    4747        admin = meta.Admin(
    4848            fields = (
    49                 (None, {'fields': ('username', 'password_md5')}),
     49                (None, {'fields': ('username', 'password')}),
    5050                ('Personal info', {'fields': ('first_name', 'last_name', 'email')}),
    5151                ('Permissions', {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}),
    5252                ('Important dates', {'fields': ('last_login', 'date_joined')}),
     
    5959
    6060    def __repr__(self):
    6161        return self.username
    62 
     62               
    6363    def get_absolute_url(self):
    6464        return "/users/%s/" % self.username
    65 
     65               
    6666    def is_anonymous(self):
    6767        return False
    68 
     68               
    6969    def get_full_name(self):
    7070        full_name = '%s %s' % (self.first_name, self.last_name)
    7171        return full_name.strip()
    72 
     72               
    7373    def set_password(self, raw_password):
    74         import md5
    75         self.password_md5 = md5.new(raw_password).hexdigest()
     74        import sha, random
     75        algo = 'sha1'
     76        salt = sha.new(str(random.random())).hexdigest()[:5]
     77        hash = sha.new(salt+raw_password).hexdigest()
     78        self.password = '%s$%s$%s' % (algo, salt, hash)
    7679
    7780    def check_password(self, raw_password):
    78         "Returns a boolean of whether the raw_password was correct."
    79         import md5
    80         return self.password_md5 == md5.new(raw_password).hexdigest()
     81        '''Returns a boolean of whether the raw_password was correct,
     82         while considering other encryption formats, and salt. A typical
     83         password hash looks like <algo>$<salt>$<hash>'''
     84        pass_string = self.password                     
     85
     86        (algo, salt, hash) = pass_string.split('$')
     87        if algo == 'md5':
     88            import md5
     89            return hash == md5.new(salt+raw_password).hexdigest()
     90        elif algo == 'sha1':
     91            import sha
     92            return hash == sha.new(salt+raw_password).hexdigest()
    8193
    8294    def get_group_permissions(self):
    8395        "Returns a list of permission strings that this user has through his/her groups."
Back to Top