Code

Ticket #273: auth.py.diff

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

Patch for trunk/django/models/auth.py

Line 
1--- auth.py.original    2005-09-09 12:17:19.000000000 -0300
2+++ auth.py     2005-09-09 12:50:29.000000000 -0300
3@@ -29,7 +29,7 @@
4     first_name = meta.CharField(maxlength=30, blank=True)
5     last_name = meta.CharField(maxlength=30, blank=True)
6     email = meta.EmailField('e-mail address', blank=True)
7-    password_md5 = meta.CharField('password', maxlength=32, help_text="Use an MD5 hash -- not the raw password.")
8+    password = meta.CharField('password', maxlength=128, help_text="Use a hash like '[algo]$[salt]$[hexdigest]'")
9     is_staff = meta.BooleanField('staff status', help_text="Designates whether the user can log into this admin site.")
10     is_active = meta.BooleanField('active', default=True)
11     is_superuser = meta.BooleanField('superuser status')
12@@ -46,7 +46,7 @@
13         exceptions = ('SiteProfileNotAvailable',)
14         admin = meta.Admin(
15             fields = (
16-                (None, {'fields': ('username', 'password_md5')}),
17+                (None, {'fields': ('username', 'password')}),
18                 ('Personal info', {'fields': ('first_name', 'last_name', 'email')}),
19                 ('Permissions', {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}),
20                 ('Important dates', {'fields': ('last_login', 'date_joined')}),
21@@ -59,25 +59,37 @@
22 
23     def __repr__(self):
24         return self.username
25-
26+               
27     def get_absolute_url(self):
28         return "/users/%s/" % self.username
29-
30+               
31     def is_anonymous(self):
32         return False
33-
34+               
35     def get_full_name(self):
36         full_name = '%s %s' % (self.first_name, self.last_name)
37         return full_name.strip()
38-
39+               
40     def set_password(self, raw_password):
41-        import md5
42-        self.password_md5 = md5.new(raw_password).hexdigest()
43+        import sha, random
44+        algo = 'sha1'
45+        salt = sha.new(str(random.random())).hexdigest()[:5]
46+        hash = sha.new(salt+raw_password).hexdigest()
47+        self.password = '%s$%s$%s' % (algo, salt, hash)
48 
49     def check_password(self, raw_password):
50-        "Returns a boolean of whether the raw_password was correct."
51-        import md5
52-        return self.password_md5 == md5.new(raw_password).hexdigest()
53+        '''Returns a boolean of whether the raw_password was correct,
54+         while considering other encryption formats, and salt. A typical
55+         password hash looks like <algo>$<salt>$<hash>'''
56+        pass_string = self.password                   
57+
58+        (algo, salt, hash) = pass_string.split('$')
59+        if algo == 'md5':
60+            import md5
61+            return hash == md5.new(salt+raw_password).hexdigest()
62+        elif algo == 'sha1':
63+            import sha
64+            return hash == sha.new(salt+raw_password).hexdigest()
65 
66     def get_group_permissions(self):
67         "Returns a list of permission strings that this user has through his/her groups."