将令牌传递给客户端
我正在开发iOS应用程序并将Django用于后端。有两个应用程序,我用在Django将令牌传递给客户端
-
Django OAuth Toolkit
支持OAuth认证 -
Python Social Auth
支持社会认证
社会认证过程应该是:
- GET本地主机/ login/{application}
- 应用程序站点上的身份验证
- 重定向到localhost /完整/ {应用}
- 获取{应用}的访问令牌
- 创建我的服务器的访问令牌的新用户,并将其与{应用}相关联的访问令牌
- 重定向到本地主机/帐户/配置文件
然后,我可以使用我的服务器的访问令牌与{application}进行通信。
但客户端会看到浏览器以localhost/login/{application}开头并以localhost/accounts/profile结尾,但仍不知道我的服务器的访问令牌是什么,所以我的问题是如何通过访问令牌给客户端?
一种解决方案是将访问令牌重定向为localhost/accounts/profile?token = MyServerToken,但是如何在重定向到配置文件url时添加参数?
您不应该在查询字符串中传递访问令牌,如/?token=my_token
。它不是一个安全的方式,绝对不推荐。
您可以使用其他一些方法是:
方法1:在响应头
您可以设置访问令牌的响应报头,并使用HTTPS协议发送设置server_access_token
。
令牌将被发送一次并被客户端使用。由于响应标头未在后续请求中传递,令牌只会传递给客户端一次。然后,客户端将通过在请求标头中设置令牌来使用它来发出进一步的请求。
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
客户端然后可以从标题中检索令牌并使用此令牌进行进一步的请求。在进一步的请求中,他必须在请求标题中发送访问令牌。
方法2:设置server_access_token
作为cookie
另一种选择是设置server_access_token
饼干作为@Ben提到你的回应。
response.set_cookie()
将在响应中设置server_access_token
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 cookie in the response object
response = HttpResponseRedirect('/accounts/profile/')
response.set_cookie(key, value='my_server_access_token', ..other parameters)
return response
注:出于安全,所有的要求(包括获取和使用代币)必须使用HTTPS端点。
感谢您的回答,您的意思是我需要直接更改Python社区认证的代码吗? –
这复制了其他更多符合标准的功能。会话cookie是为了这个确切目的而创建的,并且它们可能已经由客户端代码支持而不需要任何特殊处理,因为会话本身由后端管理。 –
@ybbaigo当你正在做4,5,6分的问题时,你必须这样做。在重定向到'localhost/accounts/profile'时,您可以设置标题.'MySocialApplicationRedirectView'是社交应用程序将重定向到您的服务器正在侦听的URL的视图。 –
对于有问题的用户,您可能已经拥有了在Django会话中需要的内容。也就是说,假如你使用的是会话中间件(这种类型的身份验证几乎是不可能的),那么你的身份提供者特定的令牌通常会被填充到SocialUser
模型的extra_data
字典中。
例如,假设你有Django的用户模型的引用(可以称之为user
):
access_token = user.social_auth.get(provider='google-oauth2').extra_data['access_token']
不幸的是,具体取决于您正在使用的后端变化。请记住,这些工具旨在让用户针对您的应用进行身份验证,而不是让您针对各种身份提供商公开的特定于产品的API执行任意操作。
至于将这些令牌传递给客户端,我需要更多地了解您的用例。有可能是身份提供者在身份验证流程中在客户端上设置了一些会话cookie。例如,如果您使用Facebook登录,他们会设置一些由Facebook客户端JavaScript API自动检索的Cookie。因此,服务器和客户端之间不需要显式共享令牌。
否则,如果你必须自己做,把它们存储在一个安全的会话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}的访问令牌请求数据并返回给客户端。 –