# Path: src/contrib/django_autologin/utils.py
# def login(request, user):
#     user.backend = settings.AUTHENTICATION_BACKENDS[0]
#     auth.login(request, user)
# 
# def strip_token(url):
#     bits = urlparse.urlparse(url)
#     original_query = urlparse.parse_qsl(bits.query)
# 
#     query = {}
#     for k, v in original_query:
#         if k != app_settings.KEY:
#             query[k] = v
# 
#     query = urllib.urlencode(query)
# 
#     return urlparse.urlunparse(
#         (bits[0], bits[1], bits[2], bits[3], query, bits[5]),
#     )
# 
# def get_user_salt(user):
#     return "".join(str(getattr(user, x)) for x in app_settings.SALT_FIELDS)


from django.shortcuts import redirect
from django.utils.cache import add_never_cache_headers
from django.core.signing import TimestampSigner, BadSignature
from django.contrib.auth.models import User
from . import app_settings
from .utils import login, strip_token, get_user_salt


class AutomaticLoginMiddleware(object):
    def process_request(self, request):
        token = request.GET.get(app_settings.KEY)
        if not token:
            return

        r = redirect(strip_token(request.get_full_path()))

        try:
            pk = int(token.split(':', 1)[0])

            # Only change user if necessary. We strip the token in any case.
            # The AnonymousUser class has no 'pk' attribute (#18093)
            if getattr(request.user, 'pk', request.user.id) == pk:
                return r

            user = User.objects.get(pk=pk)
        except (ValueError, User.DoesNotExist):
            return r

        try:

# Path: src/contrib/django_autologin/utils.py
# def login(request, user):
#     user.backend = settings.AUTHENTICATION_BACKENDS[0]
#     auth.login(request, user)
# 
# def strip_token(url):
#     bits = urlparse.urlparse(url)
#     original_query = urlparse.parse_qsl(bits.query)
# 
#     query = {}
#     for k, v in original_query:
#         if k != app_settings.KEY:
#             query[k] = v
# 
#     query = urllib.urlencode(query)
# 
#     return urlparse.urlunparse(
#         (bits[0], bits[1], bits[2], bits[3], query, bits[5]),
#     )
# 
# def get_user_salt(user):
#     return "".join(str(getattr(user, x)) for x in app_settings.SALT_FIELDS)


# Path: src/contrib/django_autologin/middleware.py
from django.shortcuts import redirect
from django.utils.cache import add_never_cache_headers
from django.core.signing import TimestampSigner, BadSignature
from django.contrib.auth.models import User
from . import app_settings
from .utils import login, strip_token, get_user_salt



class AutomaticLoginMiddleware(object):
    def process_request(self, request):
        token = request.GET.get(app_settings.KEY)
        if not token:
            return

        r = redirect(strip_token(request.get_full_path()))

        try:
            pk = int(token.split(':', 1)[0])

            # Only change user if necessary. We strip the token in any case.
            # The AnonymousUser class has no 'pk' attribute (#18093)
            if getattr(request.user, 'pk', request.user.id) == pk:
                return r

            user = User.objects.get(pk=pk)
        except (ValueError, User.DoesNotExist):
            return r

        try:

            TimestampSigner(salt=get_user_salt(user)).unsign(