custom authentication backend for legacy db

455 Views Asked by At

Since I'm working with a legacy database I should use another model instead of the default Django contrib.auth.models.User for authentication.

My model has an id field (id = models.DecimalField(...)) that is used for username and a field for password(password = models.CharField(...)) that the admin defines from the database interface.

I wrote my own authentication backend:

from auth.models import Owners
from django.contrib.auth.models import User

class AuthBackend(object):
    def authenticate(self, username, password):
        try:
           owner = Owners.objects.get(id=username, password=password)
           try:
               user = User.objects.get(id=owner.id)
               print user
           except User.DoesNotExist:
               user = User(id=owner.id)
               user.is_staff = False
               user.is_superuser = False
               user.set_unusable_password()
               user.save()
           return user

       except Owners.DoesNotExist:
           return None

   def get_user(self, id):
       try:
           return User.objects.get(id=id)
       except User.DoesNotExist:
           return None

I wanted to ask sth maybe trivial, but since I now create Users and I make use of django's authentication module that has it's own tables etc, should I also add them to the legacy db I use?(now I'm working on an instance of it and with synchdb I got the auth tables added in it but what about when I use the db itself?Am I supposed to do the same?) Is this the right way of handling my app's existing authentication system?

1

There are 1 best solutions below

0
On

An authdb entry is usually much longer than a password, and needs correspondingly more space for storage.

$<algorithm>$<salt>$<hash>

See Linux's crypt(3) man page for details.