コード例 #1
0
    def inner(message, *args, **kwargs):
        # Make sure there's NOT a http_session already
        try:
            # We want to parse the WebSocket (or similar HTTP-lite) message
            # to get cookies and GET, but we need to add in a few things that
            # might not have been there.
            if "method" not in message.content:
                message.content['method'] = "FAKE"
            request = AsgiRequest(message)

        except Exception as e:
            raise ValueError(
                "Cannot parse HTTP message - are you sure this is a HTTP consumer? %s"
                % e)
        # Make sure there's a session key
        user = None
        auth = None
        auth_token = request.GET.get("token", None)
        print('NEW TOKEN : {}'.format(auth_token))
        if auth_token:
            # comptatibility with rest framework
            request._request = {}
            request.META["HTTP_AUTHORIZATION"] = "Bearer {}".format(auth_token)
            authenticators = [
                auth() for auth in api_settings.DEFAULT_AUTHENTICATION_CLASSES
            ]
            print('Try Auth with {}'.format(
                request.META['HTTP_AUTHORIZATION']))
            for authenticator in authenticators:
                try:
                    user_auth_tuple = authenticator.authenticate(request)
                except AuthenticationFailed:
                    pass

                if user_auth_tuple is not None:
                    message._authenticator = authenticator
                    user, auth = user_auth_tuple
                    break
        message.user, message.auth = user, auth
        # Make sure there's a session key
        # Run the consumer
        result = func(message, *args, **kwargs)
        return result
コード例 #2
0
    def inner(message, *args, **kwargs):
        try:
            # We want to parse the WebSocket (or similar HTTP-lite) message
            # to get cookies and GET, but we need to add in a few things that
            # might not have been there.
            if 'method' not in message.content:
                message.content['method'] = 'FAKE'
            request = AsgiRequest(message)

        except Exception as e:
            raise ValueError('Cannot parse HTTP message - are you sure this is a HTTP consumer? {}'.format(e))

        user = None
        auth = None
        auth_token = request.GET.get('token', None)

        if auth_token:
            # comptatibility with rest framework
            request._request = {}
            request.META['HTTP_AUTHORIZATION'] = 'token {}'.format(auth_token)
            for authenticator in authenticators:
                try:
                    user_auth_tuple = authenticator.authenticate(request)
                except AuthenticationFailed:
                    pass

                if user_auth_tuple:
                    message._authenticator = authenticator
                    user, auth = user_auth_tuple
                    break

        if user is not None:
            message.user = user
            message.channel_session['user_id'] = message.user.id
        elif message.channel_session is not None and 'user_id' in message.channel_session:
            message.user = get_user_model().objects.get(id=int(message.channel_session['user_id']))

        # Run the consumer
        result = func(message, *args, **kwargs)
        return result