我使用django、django rest框架和ember.js;我的整个应用程序通过ajax进行通信。
身份验证是通过oauth2完成的,每个请求中都会在头中发送令牌。
所有的东西都很漂亮,但是文件下载。
有一次,用户可以下载一个pdf,而我不知道如何在那里应用身份验证--因为在文件下载上,我不能发送和标题,这只是一个链接。
我想将SessionAuthentication添加到特定的rest调用中,但是会话总是将传入的用户标记为任意名称。
如何迫使django在oauth2令牌流的基础上创建会话?
我试过login(request, user),但不知何故它不起作用。
发布于 2015-02-27 17:11:59
最后,我得到了签了名的票,例如,我发回了一个令牌,它能够在规定的时间框架内绕过auth。因此,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.')https://stackoverflow.com/questions/26549542
复制相似问题