from __future__ import unicode_literals

import nodeconductor.logging.middleware

import rest_framework.authentication


def user_capturing_auth(auth):
    class CapturingAuthentication(auth):
        def authenticate(self, request):
            result = super(CapturingAuthentication, self).authenticate(request)
            if result is not None:
                user, _ = result
                nodeconductor.logging.middleware.set_current_user(user)
            return result

    return CapturingAuthentication


SessionAuthentication = user_capturing_auth(rest_framework.authentication.SessionAuthentication)
TokenAuthentication = user_capturing_auth(rest_framework.authentication.TokenAuthentication)

@@ -38,85 +38,1207 @@
 ls%0A%0A
-import nodeconductor.logging.middleware%0A%0Aimport rest_framework.authentication
+from django.utils.translation import ugettext_lazy as _%0Aimport rest_framework.authentication%0Afrom rest_framework import exceptions%0A%0Aimport nodeconductor.logging.middleware%0A%0A%0ATOKEN_KEY = 'x-auth-token'%0A%0A%0Aclass TokenAuthentication(rest_framework.authentication.TokenAuthentication):%0A    %22%22%22%0A    Custom token-based authentication.%0A%0A    Use TOKEN_KEY from request query parameters if authentication token was not found in header.%0A    %22%22%22%0A%0A    def get_authorization_value(self, request):%0A        auth = rest_framework.authentication.get_authorization_header(request)%0A        if not auth:%0A            auth = request.query_params.get(TOKEN_KEY, '')%0A        return auth%0A%0A    def authenticate(self, request):%0A        auth = self.get_authorization_value(request).split()%0A%0A        if not auth or auth%5B0%5D.lower() != b'token':%0A            return None%0A%0A        if len(auth) == 1:%0A            msg = _('Invalid token. No credentials provided.')%0A            raise exceptions.AuthenticationFailed(msg)%0A        elif len(auth) %3E 2:%0A            msg = _('Invalid token. Token string should not contain spaces.')%0A            raise exceptions.AuthenticationFailed(msg)%0A%0A        return self.authenticate_credentials(auth%5B1%5D)
 %0A%0A%0Ad
@@ -1632,17 +1632,16 @@
 cation%0A%0A
-%0A
 SessionA
@@ -1775,38 +1775,8 @@
 uth(
-rest_framework.authentication.
 Toke

Use get parameter in token auth (nc-544)