Looking for a detailed guide on setting up custom authentication databases in Django or pointers

I am trying to set up my own backend, which requests another database for which I created a model in the system. It uses its own rules (email instead of username and another salty / hashed password), so I cannot use built-in authentication. I configured my own backend for authentication:

class BlahBlahBackend:

    def check_password():
        # check password code here
        return true

    def authenticate(self, email=None, password=None):
        import myapp.models.loginmodel
        try:
            person =  myapp.models.loginmodel.People.objects.get(email=email)
            if check_password(password, person.password):
                try:
                    user = User.objects.get(email=email)
                except User.DoesNotExist:
                    username=person.first_name + person.last_name
                    name_count = User.objects.filter(username__startswith = username).count()
                    if name_count:
                        username = '%s%s'%(username, name_count + 1)
                        user = User.objects.create_user(username,email)
                    else:
                        user = User.objects.create_user(username,email)
        except People.DoesNotExist:
            return None

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

I added BlahBlahBackend as an authentication server:

AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',                          'Socialauth.auth_backends.OpenIdBackend',                          'Socialauth.auth_backends.TwitterBackend',                          'Socialauth.auth_backends.FacebookBackend',                          'Socialauth.auth_backends.BlahBlahBackend',                          )

, auth, socialauth.

, :

def blahblah_login_complete(request):
    email = request.POST.get('email')
    password = request.POST.get('password')
    user = authenticate(email,password)
    # if user is authenticated then login user
    if user:
        login(request, user)
    else:
        return HttpResponseRedirect(reverse('socialauth_login_page'))

, , , , , .

,

Session.objects.all().delete()

-.

:

  • , AUTHENTICATION_BACKENDS
  • /, ? , .
  • . , - , , openid Twitter?

Update:

! , . , , django doc : " , , ", , . , , . , urls.py, , .

+3
3

django.contrib.auth.authenticate() . " " "".

: blahblah_email blahblah_password, (blahblah_email =..., blahblah_password =...).

+1
  • - django , ..

  • , . django authenticate() , .

+1

I think django-cas would be a good link for you :)

And yes, the order of AUTHENTICATION_BACKENDS matters.

Django iterates over the list of backends and stops at the first backend, which has a method authenticatethat accepts the credential parameters that you passed to it.

+1
source

Source: https://habr.com/ru/post/1730341/


All Articles