Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding authenticate method - Django admin

I'm trying to figure out how to enhance the authenticate method with additional functionality.

e.g.

  • Expiring passwords
  • special password formats
  • length requirements
  • etc...

It is pretty straight forward for the site's frontend, but what about the admin panel?


I reckon that I should override the User's Manager object, as authenticate probably resides there. This is quite a tough one to figure out I think.

Thanks in advance! :)

like image 408
RadiantHex Avatar asked Nov 16 '10 18:11

RadiantHex


People also ask

How do I manually authenticate in Django?

from django.contrib.auth import authenticate, login def my_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) # Redirect to a success page. ... else: # Return an 'invalid ...

What is Auth_user_model?

AUTH_USER_MODEL is the recommended approach when referring to a user model in a models.py file. For this you need to create custom User Model by either subclassing AbstractUser or AbstractBaseUser.


1 Answers

You can create custom authentication backend by following the instructions in http://docs.djangoproject.com/en/dev/topics/auth/#authentication-backends. Essentially, you create a backend class that has an authenticate method:

class MyBackend:
    def authenticate(self, username=None, password=None):
        # Check the username/password and return a User.

Then add the class to AUTHENTICATION_BACKENDS in settings.py.

Though this is for authentication, you could do all the password validation things you mentioned simply by redirecting a user to a change password page if the password is correct but expired, for instance. Consider using the messaging framework to give a user a hint about what is going on when directing him to a generic change password page.

like image 54
eddie_c Avatar answered Sep 30 '22 05:09

eddie_c