Ticket #18171: can_authenticate.patch

File can_authenticate.patch, 2.3 KB (added by jeffhui, 3 years ago)

proposal for adding can_authenticate instead of capturing TypeErrors

  • django/contrib/auth/__init__.py

    diff -ur -x '*.pyc' django_original/django/contrib/auth/__init__.py django/django/contrib/auth/__init__.py
    old new  
    4141    If the given credentials are valid, return a User object.
    4242    """
    4343    for backend in get_backends():
    44         try:
     44        if hasattr(backend, 'can_authenticate'):
     45            if not backend.can_authenticate(credentials):
     46                continue
    4547            user = backend.authenticate(**credentials)
    46         except TypeError:
    47             # This backend doesn't accept these credentials as arguments. Try the next one.
    48             continue
     48        else:
     49            # fall back to the old method
     50            try:
     51                user = backend.authenticate(**credentials)
     52            except TypeError:
     53                # This backend doesn't accept these credentials as arguments. Try the next one.
     54                continue
    4955        if user is None:
    5056            continue
    5157        # Annotate the user object with the path of the backend.
  • django/contrib/auth/backends.py

    diff -ur -x '*.pyc' django_original/django/contrib/auth/backends.py django/django/contrib/auth/backends.py
    old new  
    77    """
    88    supports_inactive_user = True
    99
     10    def can_authenticate(self, credentials):
     11        # we don't check anything here for backwards compatibility
     12        # for any classes that subclass us.
     13        #
     14        #return 'username' in credentials and 'password' in credentials
     15        return True
     16
    1017    # TODO: Model, login attribute name and password attribute name should be
    1118    # configurable.
    1219    def authenticate(self, username=None, password=None):
     
    7986    # Create a User object if not already in the database?
    8087    create_unknown_user = True
    8188
     89    def can_authenticate(self, credentials):
     90        # we don't check anything here for backwards compatibility
     91        # for any classes that subclass us.
     92        #
     93        #return 'remote_user' in credentials
     94        return True
     95
    8296    def authenticate(self, remote_user):
    8397        """
    8498        The username passed as ``remote_user`` is considered trusted.  This
Back to Top