From 078e2f8d73bdf88d345ae90e505d6cf97dc42b68 Mon Sep 17 00:00:00 2001 From: Joseph Kocherhans Date: Wed, 10 May 2006 04:20:53 +0000 Subject: [PATCH] multi-auth: Moved check_password implementation to a function and updated the method to use it. This makes it available for future backends to use. git-svn-id: http://code.djangoproject.com/svn/django/branches/multi-auth@2882 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/auth/models.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py index cea7a694c9..00a6f0b097 100644 --- a/django/contrib/auth/models.py +++ b/django/contrib/auth/models.py @@ -6,6 +6,20 @@ import datetime SESSION_KEY = '_auth_user_id' +def check_password(raw_password, enc_password): + """ + Returns a boolean of whether the raw_password was correct. Handles + encryption formats behind the scenes. + """ + algo, salt, hsh = enc_password.split('$') + if algo == 'md5': + import md5 + return hsh == md5.new(salt+raw_password).hexdigest() + elif algo == 'sha1': + import sha + return hsh == sha.new(salt+raw_password).hexdigest() + raise ValueError, "Got unknown password algorithm type in password." + class SiteProfileNotAvailable(Exception): pass @@ -117,14 +131,7 @@ class User(models.Model): self.set_password(raw_password) self.save() return is_correct - algo, salt, hsh = self.password.split('$') - if algo == 'md5': - import md5 - return hsh == md5.new(salt+raw_password).hexdigest() - elif algo == 'sha1': - import sha - return hsh == sha.new(salt+raw_password).hexdigest() - raise ValueError, "Got unknown password algorithm type in password." + return check_password(raw_password, self.password) def get_group_permissions(self): "Returns a list of permission strings that this user has through his/her groups."