import json

from django.conf import settings
from django.contrib.auth import authenticate, login

from .models import LOT

class LOTMiddleware(object):
    def process_request(self, request):
        lot_uuid = request.GET.get(settings.LOT_MIDDLEWARE_PARAM_NAME, None)
        if lot_uuid:
            try:
                lot = LOT.objects.get(uuid=lot_uuid)
            except LOT.DoesNotExist:
                return None

            if not lot.verify():
                lot.delete()
                return None

            user = authenticate(lot_uuid=lot_uuid)
            login(request, user)

            try:
                session_data = json.loads(lot.session_data)
                request.session.update(session_data)
            except Exception:
                # If not correctly serialized not set the session_data
                pass

            if lot.is_one_time():
                lot.delete()

@@ -892,28 +892,685 @@
                lot.delete()%0A
+%0A%0Aclass LOTAuthenticationMiddleware(object):%0A    '''Authenticate using a Header'''%0A    def process_request(self, request):%0A        try:%0A            token = request.META%5B'X-Auth-Token'%5D%0A            lot = LOT.objects.select_related('user').get(uuid=token)%0A        except (KeyError, LOT.DoesNotExist):%0A            return%0A        %0A        if not lot.verify():%0A            lot.delete()%0A            return%0A        %0A        request.user = lot.user%0A        %0A        try:%0A            session_data = json.loads(lot.session_data)%0A            request.session.update(session_data)%0A        except:%0A            pass%0A%0A        if lot.is_one_time():%0A            lot.delete()%0A

Update middleware.py