          """
          Returns a `User` if a correct username and password have been supplied
          using HTTP Basic authentication.  Otherwise returns `None`.
          """
          if 'HTTP_AUTHORIZATION' in request.META:
              auth = request.META['HTTP_AUTHORIZATION'].split()
              if len(auth) == 2 and auth[0].lower() == "basic":
                  try:
                      auth_parts = base64.b64decode(auth[1]).partition(':')
                  except TypeError:
                      return None
  
                  try:
                      userid = smart_unicode(auth_parts[0])
                      password = smart_unicode(auth_parts[2])
                  except DjangoUnicodeDecodeError:
                      return None
  
                  return self.authenticate_credentials(userid, password)
  
      def authenticate_credentials(self, userid, password):
          """
          Authenticate the userid and password against username and password.
          """
 
</s> remove         if len(auth) == 2 and auth[0].lower() == "token":
            key = auth[1]
            try:
                token = self.model.objects.get(key=key)
            except self.model.DoesNotExist:
                return None
 </s> add         if not auth or auth[0].lower() != "token":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid token header')

        return self.authenticate_credentials(auth[1])

    def authenticate_credentials(self, key):
        try:
            token = self.model.objects.get(key=key)
        except self.model.DoesNotExist:
            raise exceptions.AuthenticationFailed('Invalid token')

        if token.user.is_active:
            return (token.user, token)
        raise exceptions.AuthenticationFailed('User inactive or deleted')

    def authenticate_header(self):
        return 'Token' </s> remove         Attempt to authenticate the request using each authentication instance in turn.
        Returns a two-tuple of (user, authtoken).
 </s> add         Attempt to authenticate the request using each authentication instance
        in turn.
        Returns a three-tuple of (authenticator, user, authtoken). </s> remove                 return user_auth_tuple
 </s> add                 user, auth = user_auth_tuple
                return (authenticator, user, auth)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
          """
          Returns a `User` if a correct username and password have been supplied
          using HTTP Basic authentication.  Otherwise returns `None`.
          """
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
        auth = request.META.get('HTTP_AUTHORIZATION', '').split()

        if not auth or auth[0].lower() != "basic":
            return None

        if len(auth) != 2:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            auth_parts = base64.b64decode(auth[1]).partition(':')
        except TypeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        try:
            userid = smart_unicode(auth_parts[0])
            password = smart_unicode(auth_parts[2])
        except DjangoUnicodeDecodeError:
            raise exceptions.AuthenticationFailed('Invalid basic header')

        return self.authenticate_credentials(userid, password)
  
      def authenticate_credentials(self, userid, password):
          """
          Authenticate the userid and password against username and password.
          """
keep keep keep keep replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace keep keep keep keep keep
 Implementing 401 vs 403 responses 
rest_framework/authentication.py