from django.contrib.auth.models import User
import hashlib

class PhpbbAuthenticationBackend:
    def authenticate(self, username=None, password=None):
        try:
            # phpbb 2.x encodes passwords as plain md5 hashes, no salt
            pass_md5 = hashlib.md5(password).hexdigest()
            user = User.objects.get(username=username, password=pass_md5)
            
            # get rid of the old-style password, get with the new style!
            user.set_password(password)
            user.save()

            return user
        except User.DoesNotExist:
            return None