[PYTHON] The _authenticate_with_backend function was obsolete in django auth.autenticate

background

Updating Django version from 1.11.1 to 2.2, I started getting an error with authentication that I had customized myself.

authenticate of django 1.11.1

def authenticate(request=None, **credentials):
    """
    If the given credentials are valid, return a User object.
    """
    for backend, backend_path in _get_backends(return_tuples=True):
        try:
            user = _authenticate_with_backend(backend, backend_path, request, credentials)
        except PermissionDenied:
            # This backend says to stop in our tracks - this user should not be allowed in at all.
            break
        if user is None:
            continue
        # Annotate the user object with the path of the backend.
        user.backend = backend_path
        return user

    # The credentials supplied are invalid to all backends, fire signal
    user_login_failed.send(sender=__name__, credentials=_clean_credentials(credentials), request=request)



def _authenticate_with_backend(backend, backend_path, request, credentials):
    args = (request,)
    # Does the backend accept a request argument? <---What is this comment?
    try:
        inspect.getcallargs(backend.authenticate, request, **credentials)
    except TypeError:
        args = ()
        credentials.pop('request', None)
        # Does the backend accept a request keyword argument?
        try:
            inspect.getcallargs(backend.authenticate, request=request, **credentials)
        except TypeError:
            # Does the backend accept credentials without request?
            try:
                inspect.getcallargs(backend.authenticate, **credentials)
            except TypeError:
                # This backend doesn't accept these credentials as arguments. Try the next one.
                return None
            else:
                warnings.warn(
                    "Update %s.authenticate() to accept a positional "
                    "`request` argument." % backend_path,
                    RemovedInDjango21Warning
                )
        else:
            credentials['request'] = request
            warnings.warn(
                "In %s.authenticate(), move the `request` keyword argument "
                "to the first positional argument." % backend_path,
                RemovedInDjango21Warning
            )
    return backend.authenticate(*args, **credentials)

↓ Authentication made by myself

from django.contrib.auth.backends import ModelBackend
from .models import UserProfile


class SignedRequestBackend(ModelBackend):
    """Implemented a pass-through authentication backend for separate authentication"""

    def authenticate(self, user=None, user_profile=None, **kwds):
        if user is None:
            return
        if not user.is_authenticated or not user.is_active:
            return
        if municipality_user_profile is None:
            return
        if not isinstance(user_profile, UserProfile):
            return
        if user.id != user_profile.user_id:
            return
        return user

django 2.2 authenticate

def authenticate(request=None, **credentials):
    """
    If the given credentials are valid, return a User object.
    """
    for backend, backend_path in _get_backends(return_tuples=True):
        try:
            inspect.getcallargs(backend.authenticate, request, **credentials)
        except TypeError:
            # This backend doesn't accept these credentials as arguments. Try the next one.
            continue
        try:
            user = backend.authenticate(request, **credentials)
        except PermissionDenied:
            # This backend says to stop in our tracks - this user should not be allowed in at all.
            break
        if user is None:
            continue
        # Annotate the user object with the path of the backend.
        user.backend = backend_path
        return user

    # The credentials supplied are invalid to all backends, fire signal
    user_login_failed.send(sender=__name__, credentials=_clean_credentials(credentials), request=request)

[Conclusion] The _authenticate_with_backend function has been abolished.

I tried debugging inspect.getcallargs

[Error cause] ['self','user','user_profile'] is assigned to args (please enter ['self','request'] here)

--Ah, there was a comment ... # Does the backend accept a request argument? --Set request in the argument of authentication you made → The error has been resolved.

Recommended Posts

The _authenticate_with_backend function was obsolete in django auth.autenticate
sort warning in the pd.concat function
Implementation of login function in Django
Switch the language displayed in Django 1.9
The meaning of ".object" in Django
Get the query string (query string) in Django
Get the client's IP address in Django
Django ~ Let's display it in the browser ~
OR the List in Python (zip function)
Added a function to register desired shifts in the Django shift table
Try hitting the Spotify API in Django.
Get the caller of a function in Python
Fix the argument of the function used in map
Specify the view URL in your Django template
I implemented the inverse gamma function in python
The story of viewing media files in Django
[Django] css in the project cannot be read
Models in Django
[Django] Let's try to clarify the part of Django that was somehow through in the test
When I checked the query generated by Django, it was issued in large numbers
Duality in function
Forms in Django
Learning notes for the migrations feature in the Django framework (2)
Output the time from the time the program was started in python
[Django] Perform Truncate Table (delete all data in the table)
Set the form DateField to type = date in Django
Learning notes for the migrations feature in the Django framework (3)
[Django Rest Framework] Customize the filter function using Django-Filter
Learning notes for the migrations feature in the Django framework (1)
The story that Japanese output was confused with Django
What does the last () in a function mean in Python?
It was great to edit the Python file in the Raspberry Pi with Atom's remote function
The story I was addicted to when I specified nil as a function argument in Go