Python 将令牌传递给客户端
我正在开发一个iOS应用程序,并使用Django作为后端。我在Django中使用了两个应用程序Python 将令牌传递给客户端,python,django,authentication,oauth,python-social-auth,Python,Django,Authentication,Oauth,Python Social Auth,我正在开发一个iOS应用程序,并使用Django作为后端。我在Django中使用了两个应用程序 Django OAuth Toolkit支持OAuth身份验证 Python社交身份验证支持社交身份验证 社会认证过程应该是: 获取localhost/login/{application} 应用程序站点上的身份验证 重定向到localhost/complete/{application} 获取{application}的访问令牌 使用我的服务器的访问令牌创建一个新用户,并将其与{applicat
支持OAuth身份验证Django OAuth Toolkit
支持社交身份验证Python社交身份验证
一种解决方案是使用访问令牌作为localhost/accounts/profile?token=MyServerToken进行重定向,但在重定向到配置文件url时如何添加参数?您不应在查询字符串中传递访问令牌,如
/?token=my_token
。这不是一种安全的方式,绝对不推荐使用。
您可以使用的其他方法有:
方法1:在响应头中设置服务器访问令牌
您可以在响应头中设置访问令牌,并使用HTTPS协议发送它
令牌将发送一次并由客户端使用。由于响应头不会在后续请求中传递,因此令牌将只传递给客户端一次。然后,客户端将通过在请求头中设置令牌来使用它发出进一步的请求
class MySocialApplicationRedirectView(View):
def get(self, request, *args, **kwargs):
# Here, write your code to fetch the {application}'s access token,
# creating a new user with your server's access token, and then
# associating it with {application}'s access token
# assign the response to a variable and set the access token as a header in the response
response = HttpResponseRedirect('/accounts/profile/')
response['X-Auth-Token'] = 'my_server_access_token'
# can also use the below name as 'X-' prefixed headers are deprecated
# response['Auth-Token'] = 'my_server_access_token'
return response
class MySocialApplicationRedirectView(View):
def get(self, request, *args, **kwargs):
# Here, write your code to fetch the {application}'s access token,
# creating a new user with your server's access token, and then
# associating it with {application}'s access token
# assign the response to a variable and set the access token as a cookie in the response object
response = HttpResponseRedirect('/accounts/profile/')
response.set_cookie(key, value='my_server_access_token', ..other parameters )
return response
然后,客户端可以从头中检索令牌,并使用该令牌发出进一步的请求。在进一步的请求中,他必须在请求头中发送访问令牌
方法2:将服务器访问\u令牌设置为cookie
另一个选项是在您的响应中设置服务器访问\u令牌
cookie,如前所述
将在响应中设置server\u access\u令牌
cookie,然后客户端可以读取cookie并在请求头中的进一步请求中发送此cookie
class MySocialApplicationRedirectView(View):
def get(self, request, *args, **kwargs):
# Here, write your code to fetch the {application}'s access token,
# creating a new user with your server's access token, and then
# associating it with {application}'s access token
# assign the response to a variable and set the access token as a header in the response
response = HttpResponseRedirect('/accounts/profile/')
response['X-Auth-Token'] = 'my_server_access_token'
# can also use the below name as 'X-' prefixed headers are deprecated
# response['Auth-Token'] = 'my_server_access_token'
return response
class MySocialApplicationRedirectView(View):
def get(self, request, *args, **kwargs):
# Here, write your code to fetch the {application}'s access token,
# creating a new user with your server's access token, and then
# associating it with {application}'s access token
# assign the response to a variable and set the access token as a cookie in the response object
response = HttpResponseRedirect('/accounts/profile/')
response.set_cookie(key, value='my_server_access_token', ..other parameters )
return response
注意:为了安全起见,所有请求(包括获取和使用令牌的请求)都必须使用HTTPS端点。它没有回答您的特定问题,但我已使用解决方案解决了类似问题。它非常简单,虽然不需要处理多个应用程序,但因为它为任何给定的应用程序提供了API,所以应该不会有问题。您可能已经在Django会话中为您的用户提供了所需的内容。也就是说,如果您使用的是会话中间件(没有它,这种类型的身份验证几乎是不可能的),您的身份提供者特定的令牌通常会填充在所讨论的特定提供者的
SocialUser
模型上的extra_data
dict中
例如,假设您引用了Django用户模型(我们称之为user
):
不幸的是,具体细节会因您使用的后端而异。请记住,这些工具旨在让用户对您的应用进行身份验证,而不是让您对各种身份提供商公开的特定于产品的API执行任意操作
至于将这些令牌传递给客户机,我需要更多地了解您的用例。身份提供者很可能在其身份验证流期间在客户端上设置了一些会话cookie。例如,如果您使用Facebook登录,他们会设置一些cookie,这些cookie由Facebook客户端javascript API自动检索。因此,服务器和客户端之间不需要显式共享令牌
否则,如果必须自己执行,请将其存储在安全会话cookie上,如下所示:
response.set_cookie(social_auth_tokens,
value=your_data_here,
max_age=None, #cookie will expire at end of user session
expires=None,
path='/',
domain=None, #only readable by this domain
secure=True, #only transmitted over https
httponly=False) #readable by scripts running on the page
客户端打算如何处理服务器的访问令牌?我将使用服务器的访问令牌请求数据,如果数据在{application}上,我的服务器将使用{application}的访问令牌请求数据,然后返回到客户端。感谢您的回答,你的意思是我需要直接更改Python Social Auth的代码吗?这复制了其他更符合标准的功能。会话cookie正是为此目的创建的,客户端代码可能已经支持它们,而无需任何特殊处理,因为会话本身由后端管理。@ybbaigo您在处理问题的第4、5、6点时必须这样做。在重定向到
localhost/accounts/profile
时,您可以设置标题。MySocialApplicationRedirectView
是社交应用程序将重定向到服务器正在侦听的url的视图。@BenBurns同意并更新了ans。目的是在响应标题中发送令牌,客户端将在其中使用令牌。响应头不会在后续请求中传递。因此,令牌将被发送一次并被消费。我避免在cookie中设置令牌,因为这样令牌将在请求头和请求cookie中发送。不用担心-删除了我的否决票。另外,您可能需要注意,非标准标题通常以X
作为前缀,并且通常在标题键中使用连字符,而不是下划线,因此为了保持典型惯例,如果您使用标题,您可能需要设置X-Auth-Token
。事实上,如果你在谷歌上搜索一下,你会发现很多框架都使用这个头键进行基于头的身份验证。