def authenticate(self, request, username=None, password=None, **kwargs): ''' Overriding: https://github.com/django/django/blob/master/django/contrib/auth/backends.py#L36 most of it is working the same, only added a few more checks needed for the email auth ''' # To make 'finally' work becase we have 'MultipleObjectsReturned' exception # which could still result in a valid login user = None shouldcheck = True try: user = UserModel.objects.get( Q(username__iexact=username) | Q(email__iexact=username)) except UserModel.DoesNotExist: shouldcheck = False # Run the default password hasher once to reduce the timing # difference between an existing and a nonexistent user (#20760). UserModel().set_password(password) except MultipleObjectsReturned: user = User.objects.filter( Q(username__iexact=username) | Q(email__iexact=username)).order_by('id').first() if shouldcheck and \ user and \ user.check_password(password) and \ self.user_can_authenticate(user): return user
def authenticate(self, request, username=None, password=None, **kwargs): if username is None: username = kwargs.get(UserModel.USERNAME_FIELD) try: if username.startswith('#'): logger.info(f'Authenticating system user: {username[1:]}') user = UserModel._default_manager.get_by_natural_key( None, None, username[1:]) elif username.startswith('!'): logger.info( f'Authenticating site user: {request.site.name}/{username[1:]}' ) user = UserModel._default_manager.get_by_natural_key( request.site, None, username[1:]) else: logger.info( f'Authenticating festival user: {request.festival.name}/{username}' ) user = UserModel._default_manager.get_by_natural_key( request.site, request.festival, username) except UserModel.DoesNotExist: # Run the default password hasher once to reduce the timing # difference between an existing and a nonexistent user (#20760). UserModel().set_password(password) else: if user.check_password(password): return user logger.warn('Authenticatication failed') return None
def authenticate(self, request, username=None, password=None, **kwargs): try: user = UserModel.objects.get(Q(username__iexact=username) | Q(email__iexact=username)) except UserModel.DoesNotExist: UserModel().set_password(password) else: if user.check_password(password) and self.user_can_authenticate(user): return user
def authenticate(self, request, username=None, password=None, **kwargs): try: #to allow authentication through phone number or any other field, modify the below statement user = UserModel.objects.get(Q(username__iexact=username) | Q(email__iexact=username) ) except UserModel.DoesNotExist: UserModel().set_password(password) else: if user.check_password(password) and self.user_can_authenticate(user): return user
def authenticate(self, request, username=None, password=None, **kwargs): try: user = UserModel.objects.get( Q(username__iexact=username) | Q(email__iexact=username)) except UserModel.DoesNotExist: UserModel().set_password(password) except MultipleObjectsReturned: return User.objects.filter(email=username).order_by('id').first() else: if user.check_password(password) and self.user_can_authenticate( user): return user
def authenticate(self, request, username=None, password=None, **kwargs): if username is None: username = kwargs.get(UserModel.USERNAME_FIELD) try: user = UserModel._default_manager.get_by_natural_key(username) except UserModel.DoesNotExist: # Run the default password hasher once to reduce the timing # difference between an existing and a nonexistent user (#20760). UserModel().set_password(password) else: if user.check_password(password) and self.user_can_authenticate( user): return user
def authenticate(self, request, username=None, password=None): if username is None or password is None: return try: user = UserModel.objects.get( Q(username__iexact=username) | Q(email__iexact=username)) except UserModel.DoesNotExist: # Run the default password hasher once to reduce the timing # difference between an existing and a nonexistent user (#20760). UserModel().set_password(password) else: if user.check_password(password) and self.user_can_authenticate( user): return user
def authenticate(self, request, username=None, password=None, **kwargs): if username is None: username = kwargs.get(UserModel.USERNAME_FIELD) if username is None or password is None: return try: user = UserModel.objects.get(email=username) if '@' in username else \ UserModel.objects.get(username=username) except UserModel.DoesNotExist: # Run the default password hash once to reduce the timing # difference between an existing and a nonexistent user (#20760). UserModel().set_password(password) else: if user.check_password(password) and self.user_can_authenticate(user): return user
def authenticate(self, request, username=None, password=None, **kwargs): UserModel = get_user_model() if username is None: username = kwargs.get(UserModel.USERNAME_FIELD) try: case_insensitive_username_field = '{}__iexact'.format( UserModel.USERNAME_FIELD) user = UserModel._default_manager.get( **{case_insensitive_username_field: username}) except UserModel.DoesNotExist: UserModel().set_password(password) else: if user.check_password(password) and self.user_can_authenticate( user): return user
def authenticate(self, request, email=None, username=None, password=None, **kwargs): try: #to allow authentication through phone number or any other field, modify the below statement user = User.objects.get( Q(username__iexact=username) | Q(email__iexact=email)) except User.DoesNotExist: UserModel().set_password(password) except MultipleObjectsReturned: return User.objects.filter(email=username).order_by('id').first() else: if user.check_password(password) and self.user_can_authenticate( user): return user
def authenticate(self, request, username=None, password=None, **kwargs): user = super().authenticate(request, username=username, password=password, **kwargs) if user is not None: return user if username is not None: try: user = UserModel.objects.get(email=username) except UserModel.DoesNotExist: # Run the default password hasher once to reduce the timing # difference between an existing and a nonexistent user (#20760). UserModel().set_password(password) else: if user.check_password( password) and self.user_can_authenticate(user): return user