django authentication without a password

后端 未结 4 1482
既然无缘
既然无缘 2020-12-05 03:13

I\'m using the default authentication system with django, but I\'ve added on an OpenID library, where I can authenticate users via OpenID. What I\'d like to do is log them

相关标签:
4条回答
  • 2020-12-05 03:26

    It's straightforward to write a custom authentication backend for this. If you create yourapp/auth_backend.py with the following contents:

    from django.contrib.auth.backends import ModelBackend
    from django.contrib.auth.models import User
    
    
    class PasswordlessAuthBackend(ModelBackend):
        """Log in to Django without providing a password.
    
        """
        def authenticate(self, username=None):
            try:
                return User.objects.get(username=username)
            except User.DoesNotExist:
                return None
    
        def get_user(self, user_id):
            try:
                return User.objects.get(pk=user_id)
            except User.DoesNotExist:
                return None
    

    Then add to your settings.py:

    AUTHENTICATION_BACKENDS = (
        # ... your other backends
        'yourapp.auth_backend.PasswordlessAuthBackend',
    )
    

    In your view, you can now call authenticate without a password:

    user = authenticate(username=user.username)
    login(request, user)
    
    0 讨论(0)
  • 2020-12-05 03:32

    You can easily fix this by creating your own authentication backend and adding it to the AUTHENTICATION_BACKENDS setting.

    There are some OpenID backends available already, so with a bit of searching you could save yourself the trouble of writing one.

    0 讨论(0)
  • 2020-12-05 03:33

    In order to do authenticate without password, in your settings.py:

    AUTHENTICATION_BACKENDS = [
    # auth_backend.py implementing Class YourAuth inside yourapp folder
        'yourapp.auth_backend.YourAuth', 
    # Default authentication of Django
        'django.contrib.auth.backends.ModelBackend',
    ]
    

    In your auth_backend.py:

    NOTE: If you have custom model for your app then import from .models CustomUser

    from .models import User 
    from django.conf import settings
    
    # requires to define two functions authenticate and get_user
    
    class YourAuth:  
    
        def authenticate(self, request, username=None):
            try:
                user = User.objects.get(username=username)
                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
    

    In your Views for custom login request:

    # Your Logic to login user
    userName = authenticate(request, username=uid)
    login(request, userName)
    

    For further reference, use the django documentation here.

    0 讨论(0)
  • 2020-12-05 03:41

    This is a bit of a hack but if you don't want to rewrite a bunch of stuff remove the authenticate

    user.backend = 'django.contrib.auth.backends.ModelBackend'
    login(request, user)
    

    user would be your User object

    0 讨论(0)
提交回复
热议问题