django authentication without a password -
i'm using default authentication system django, i've added on openid library, can authenticate users via openid. i'd log them in, seems using default django auth system, need password authenticate user. there way around without using password?
i'd this...
user = ... # queried user based on openid response user = authenticate(user) # function requires username , password login(user) i sooner leave off authenticate function, attaches backend field, required login.
it's straightforward write custom authentication backend this. if create yourapp/auth_backend.py following contents:
from django.contrib.auth.backends import modelbackend django.contrib.auth.models import user class passwordlessauthbackend(modelbackend): """log in django without providing 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 settings.py:
authentication_backends = ( # ... other backends 'yourapp.auth_backend.passwordlessauthbackend', ) in view, can call authenticate without password:
user = authenticate(username=user.username) login(request, user)
Comments
Post a Comment