Files
modoboa/lib/authbackends.py
T
tonio c01d6de7d8 fix: return receipts are now displayed correctly.
new: parameters module refactored. The API is more simple now, there is no need to provide a module name anymore, this last is automatically guessed. Also, a new 'label' field can be provide when declaring a new parameter. It will be used instead of the name.

new: email parsing refactored. My first attempt was really ugly. I did not really studied the various RFC describing how messages are formatted or what IMAP fetch queries can do. Now, I hope this new code will face better unknown types. At least, It will be simpler to update.
2010-09-17 09:23:35 +02:00

45 lines
1.4 KiB
Python

from django.contrib.auth.models import User
from modoboa.admin.models import Mailbox
import hashlib
import crypt
import string
from random import Random
from modoboa.lib import parameters
class SimpleBackend:
def authenticate(self, username=None, password=None):
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return None
try:
mb = Mailbox.objects.get(full_address=username)
except Exception:
return None
if not _check_password(password, mb.password):
return None
return user
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
def _check_password(password, crypted):
scheme = parameters.get_admin("PASSWORD_SCHEME", app="admin")
if scheme == "crypt":
return crypt.crypt(password, crypted) == crypted
if scheme == "md5":
return hashlib.md5(password).hexdigest() == crypted
return password
def crypt_password(password):
scheme = parameters.get_admin("PASSWORD_SCHEME", app="admin")
if scheme == "crypt":
salt = ''.join(Random().sample(string.letters + string.digits, 2))
return crypt.crypt(password, salt)
if scheme == "md5":
return hashlib.md5(password).hexdigest()
return password