Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Multiple Authentication Backend for one project

Tags:

I have an application written in Django and I have to extend it and include some other solution as an "app" in this application.

For example, my app to be integrated is named "my_new_app" Now there is a backend authentication written for the main application and I cannot use it.

I have a MySQL DB to query from and the main app uses Cassandra and Redis mostly.

Is there any way I can use a separate authentication backend for the new app "my_new_app" and run both in the same domain?

like image 981
Rai Muhammad Ehtisham Avatar asked Jun 12 '13 11:06

Rai Muhammad Ehtisham


People also ask

How many methods does an authentication backend class implement?

An authentication backend is a class that implements two required methods: get_user(user_id) and authenticate(request, **credentials) , as well as a set of optional permission related authorization methods.

Which method of authentication backend takes credentials in Django?

Using the authenticate function to process each credential as a keyword argument, you can verify users' credentials. It checks the credentials against the authentication backend and returns User objects if they are valid. If they are not valid for a backend or they have no permissions, Django will return “none.”

What is Auth_user_model in Django?

Django allows you to override the default user model by providing a value for the AUTH_USER_MODEL setting that references a custom model. Method 2 – AUTH_USER_MODEL : AUTH_USER_MODEL is the recommended approach when referring to a user model in a models.py file.

How do I authenticate username and password 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 ...


2 Answers

You can have multiple authentication backends. Just set the AUTHENTICATION_BACKENDS in settings.py of your Django project to list the backend implementations you want to use. For example I often use a combination of OpenID authentication and the standard Django authentication, like this in my settings.py:

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'django_openid_auth.auth.OpenIDBackend',
    )

In this example Django will first try to authenticate using django.contrib.auth.backends.ModelBackend, which is the default backend of Django. If that fails, then it moves on to the next backend, django_openid_auth.auth.OpenIDBackend.

Note that your custom backends must be at a path visible by Django. In this example I have to add django_openid_auth to INSTALLED_APPS, otherwise Django won't be able to import it and use it as a backend.

Also read the relevant documentation, it's very nicely written, easy to understand: https://docs.djangoproject.com/en/dev/topics/auth/customizing/

like image 146
janos Avatar answered Sep 22 '22 00:09

janos


I've been through this problem before. This is the code I used.

This is the authentication backend at the api/backend.py

from django.contrib.auth.models import User

class EmailOrUsernameModelBackend(object):
    def authenticate(self, username=None, password=None):
        if '@' in username:
            kwargs = {'email': username}
        else:
             kwargs = {'username': username}
        try:
            user = User.objects.get(**kwargs)
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None

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

And this is my settings.py

AUTHENTICATION_BACKENDS = (
    'api.backend.EmailOrUsernameModelBackend',
    'django.contrib.auth.backends.ModelBackend',
)

This code will enable you to use email to authenticate the default Django user even in Django admin.

like image 38
Edwin Lunando Avatar answered Sep 19 '22 00:09

Edwin Lunando