python social auth和oauth2对django rest框架的支持
django-rest-framework-social-oauth2的Python项目详细描述
此模块为django rest框架提供了python social auth和oauth2支持。
这个包的第一个目的是帮助为rest api设置社会认证。它还可以帮助您设置oauth2提供者。
这个包依赖于python-social-auth和django-oauth-toolkit。 如果你想比这里做的更进一步的话,你可能应该阅读他们的文档。 如果您很难理解oauth2,可以阅读一个简单的解释here。
安装
使用pip安装:
pip install django-rest-framework-social-oauth2
将这些应用程序添加到已安装的应用程序中
INSTALLED_APPS=(...'oauth2_provider','social_django','rest_framework_social_oauth2',)
将验证URL包含到您的URL.py中
urlpatterns=patterns(...(r'^auth/',include('rest_framework_social_oauth2.urls')),)
将这些上下文处理器添加到模板上下文处理器中
TEMPLATE_CONTEXT_PROCESSORS=(...'social_django.context_processors.backends','social_django.context_processors.login_redirect',)
由于django版本1.8,模板上下文处理器已被弃用,请改为在djangotemplates后端的选项中设置“上下文处理器”选项。
TEMPLATES=[{...'OPTIONS':{'context_processors':[...'social_django.context_processors.backends','social_django.context_processors.login_redirect',],},}]
然后,您可以在默认情况下或按视图启用django rest framework的身份验证类(将rest\u framework和authentication\u backends条目添加或更新到您的设置中。py)
REST_FRAMEWORK={...'DEFAULT_AUTHENTICATION_CLASSES':(...'oauth2_provider.contrib.rest_framework.OAuth2Authentication','rest_framework_social_oauth2.authentication.SocialAuthentication',),}
AUTHENTICATION_BACKENDS=(...'rest_framework_social_oauth2.backends.DjangoOAuth2','django.contrib.auth.backends.ModelBackend',)
- 此应用程序的设置为:
- drfso2_property_backend_name设置OAuth2社交后端(如Facebook)的名称,默认为“django”
- drfso2_url_namespace设置用于反转url的命名空间。
- 现在转到django admin并添加新应用程序。
- 不应更改客户端ID和客户端机密
- 用户应该是您的超级用户
- 重定向uri应留空
- 客户机类型应设置为机密
- 授权授予类型应设置为“基于资源所有者密码”
- 名称可以设置为任意值
安装完成,现在可以测试应用程序。
请记住,如果您想更进一步,您需要阅读python social auth和django oauth toolkit中的文档。 如果要启用社交后端(如Facebook),请查看python social authaboutsupported backends或django social authaboutbakends system的文档。
测试设置
既然安装完成了,我们就试试吧!为使用curl的用户请求令牌:
curl -X POST -d “client_id=<client_id>&client_secret=<client_secret>&grant_type=password&username=<user_name>&password=<password>” http://localhost:8000/auth/token
和是自动生成的密钥,您可以在创建的模型应用程序中找到它们。
现在假设您需要刷新令牌:
curl -X POST -d “grant_type=refresh_token&client_id=<client_id>&client_secret=<client_secret>&refresh_token=<your_refresh_token>” http://localhost:8000/auth/token
现在让我们试试别的!让我们用外部令牌交换链接到应用程序的令牌:
curl -X POST -d “grant_type=convert_token&client_id=<client_id>&client_secret=<client_secret>&backend=<backend>&token=<backend_token>” http://localhost:8000/auth/convert-token
<;backend>;此处需要替换为启用的后端的名称(例如Facebook,如果是这样的话)。请注意,专有的后端名称是一个有效的后端名称,但在这里没有使用。 <;backend_token>;用于从使用iOS应用程序的服务中获取的令牌。
最后,让我们尝试撤销令牌:
Revoke a single token :
curl -X POST -d “client_id=<client_id>&client_secret=<client_secret>&token=<your_token>” http://localhost:8000/auth/revoke-token
撤消用户的所有令牌:
curl -H “Authorization: Bearer <token>” -X POST -d “client_id=<client_id>” http://localhost:8000/auth/invalidate-sessions
如果你有任何问题,请随时问我。
Facebook示例
使用facebook作为djang的授权后端o-rest-framework api,您的settings.py文件应该如下所示:
INSTALLED_APPS=(...# OAuth'oauth2_provider','social_django','rest_framework_social_oauth2',)TEMPLATES=[{...'OPTIONS':{'context_processors':[...# OAuth'social_django.context_processors.backends','social_django.context_processors.login_redirect',],},}]REST_FRAMEWORK={...'DEFAULT_AUTHENTICATION_CLASSES':(...# OAuth'oauth2_provider.contrib.rest_framework.OAuth2Authentication','rest_framework_social_oauth2.authentication.SocialAuthentication',)}AUTHENTICATION_BACKENDS=(# Others auth providers (e.g. Google, OpenId, etc)...# Facebook OAuth2'social_core.backends.facebook.FacebookAppOAuth2','social_core.backends.facebook.FacebookOAuth2',# django-rest-framework-social-oauth2'rest_framework_social_oauth2.backends.DjangoOAuth2',# Django'django.contrib.auth.backends.ModelBackend',)# Facebook configurationSOCIAL_AUTH_FACEBOOK_KEY='<your app id goes here>'SOCIAL_AUTH_FACEBOOK_SECRET='<your app secret goes here>'# Define SOCIAL_AUTH_FACEBOOK_SCOPE to get extra permissions from facebook. Email is not sent by default, to get it, you must request the email permission:SOCIAL_AUTH_FACEBOOK_SCOPE=['email']SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS={'fields':'id, name, email'}
您可以通过运行以下命令来测试这些设置:
curl -X POST -d “grant_type=convert_token&client_id=<client_id>&client_secret=<client_secret>&backend=facebook&token=<facebook_token>” http://localhost:8000/auth/convert-token
此请求返回“access_token”,您应该在所有带有drf的http请求上使用它。这里发生的情况是,我们正在将访问令牌中的第三方访问令牌(<;user_access_token>;)转换为与您的api及其客户端一起使用(“访问令牌”)。您应该在系统/应用程序和API之间的每次和进一步通信中使用此令牌来验证每个请求,并避免每次都使用FB进行身份验证。
你可以在https://developers.facebook.com/apps/找到你的应用程序的ID和秘密。
出于测试目的,您可以使用https://developers.facebook.com/tools/accesstoken/中的访问令牌<;用户访问令牌
有关如何使用facebook配置python social auth的更多信息,请访问http://python-social-auth.readthedocs.io/en/latest/backends/facebook.html。
社会认证
正如您可能注意到的,我们启用了名为socialauthentication的默认身份验证后端。 此后端允许您在api上无缝注册和验证用户。
类只是从授权头获取后端名称和令牌,并尝试使用正确的外部提供程序对用户进行身份验证。
如果用户未在你的应用上注册,它将创建一个要使用的新用户。
示例请求: