Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python oauth2 django应用程序中的用户会话_Python_Django_Session - Fatal编程技术网

Python oauth2 django应用程序中的用户会话

Python oauth2 django应用程序中的用户会话,python,django,session,Python,Django,Session,我使用django、django rest框架和ember.js;我的整个应用程序通过ajax进行通信 身份验证通过oauth2完成,并在每个请求的头中发送令牌 一切都很好,但文件下载 在某一点上,用户可以下载pdf,而我不知道如何在那里应用身份验证——因为在文件下载上,我无法发送和发送标题,它只是一个链接 我曾想过将SessionAuthentication添加到特定的RESTAPI调用中,但会话总是将传入用户标记为anyonymous 如何强制django在oauth2令牌流之上创建会话 我

我使用django、django rest框架和ember.js;我的整个应用程序通过ajax进行通信

身份验证通过oauth2完成,并在每个请求的头中发送令牌

一切都很好,但文件下载

在某一点上,用户可以下载pdf,而我不知道如何在那里应用身份验证——因为在文件下载上,我无法发送和发送标题,它只是一个链接

我曾想过将SessionAuthentication添加到特定的RESTAPI调用中,但会话总是将传入用户标记为anyonymous

如何强制django在oauth2令牌流之上创建会话


我尝试了loginrequest,user,但不知何故没有生效。

我最终得到了已签名的票证,例如,我发回了一个令牌,该令牌能够在定义的时间范围内绕过身份验证。因此,ajax应用程序可以首先请求令牌,然后再次发出带有令牌的标准get请求

以下是我融入视图的基本想法:

class DownloadableMixin():
    """
    Manages a ticket response, where a ticket is a signed response that gives a user limited access to a resource
    for a time frame of 5 secs.

    Therefore, file downloads can request a ticket for a resource and gets a ticket in the response that he can
    use for non-ajax file-downloads.
    """

    MAX_AGE = 5

    def check_ticket(self, request):
        signer = TimestampSigner()
        try:
            unsigned_ticket = signer.unsign(request.QUERY_PARAMS['ticket'], max_age=self.__class__.MAX_AGE)
        except SignatureExpired:
            return False
        except BadSignature:
            return False

        if self.get_requested_file_name() == unsigned_ticket:
            return True
        return False

    def get_ticket(self):
        signer = TimestampSigner()
        return signer.sign(self.get_requested_file_name())

    def has_ticket(self, request):
        return 'ticket' in request.QUERY_PARAMS

    def requires_ticket(self, request):
        return 'download' in request.QUERY_PARAMS

    def get_requested_file_name(self):
        raise NotImplementedError('Extending classes must define the requested file name.')