用于python的oauth 2.0提供程序
python-oauth2的Python项目详细描述
这个项目不再维持了。 如果您正在寻找一个oauth 2.0库来集成到python应用程序中,我建议您使用oauthlib。
Python-OAuth2
python-oauth2是一个框架,旨在使提供身份验证变得容易 通过应用程序堆栈中的OAuth 2.0。
使用量
授权服务器示例
fromwsgiref.simple_serverimportmake_serverimportoauth2importoauth2.grantimportoauth2.errorimportoauth2.store.memoryimportoauth2.tokengeneratorimportoauth2.web.wsgi# Create a SiteAdapter to interact with the user.# This can be used to display confirmation dialogs and the like.classExampleSiteAdapter(oauth2.web.AuthorizationCodeGrantSiteAdapter,oauth2.web.ImplicitGrantSiteAdapter):TEMPLATE=''' <html> <body> <p> <a href="{url}&confirm=confirm">confirm</a> </p> <p> <a href="{url}&deny=deny">deny</a> </p> </body> </html>'''defauthenticate(self,request,environ,scopes,client):# Check if the user has granted accessifrequest.post_param("confirm")=="confirm":return{}raiseoauth2.error.UserNotAuthenticateddefrender_auth_page(self,request,response,environ,scopes,client):url=request.path+"?"+request.query_stringresponse.body=self.TEMPLATE.format(url=url)returnresponsedefuser_has_denied_access(self,request):# Check if the user has denied accessifrequest.post_param("deny")=="deny":returnTruereturnFalse# Create an in-memory storage to store your client apps.client_store=oauth2.store.memory.ClientStore()# Add a clientclient_store.add_client(client_id="abc",client_secret="xyz",redirect_uris=["http://localhost/callback"])site_adapter=ExampleSiteAdapter()# Create an in-memory storage to store issued tokens.# LocalTokenStore can store access and auth tokenstoken_store=oauth2.store.memory.TokenStore()# Create the controller.provider=oauth2.Provider(access_token_store=token_store,auth_code_store=token_store,client_store=client_store,token_generator=oauth2.tokengenerator.Uuid4())# Add Grants you want to supportprovider.add_grant(oauth2.grant.AuthorizationCodeGrant(site_adapter=site_adapter))provider.add_grant(oauth2.grant.ImplicitGrant(site_adapter=site_adapter))# Add refresh token capability and set expiration time of access tokens# to 30 daysprovider.add_grant(oauth2.grant.RefreshToken(expires_in=2592000))# Wrap the controller with the Wsgi adapterapp=oauth2.web.wsgi.Application(provider=provider)if__name__=="__main__":httpd=make_server('',8080,app)httpd.serve_forever()
此示例仅显示如何实例化服务器。 由于缺少客户端应用程序,因此它不是一个有效的示例。看看 examples目录。
支持的存储后端
python-oauth2不会强制您使用特定的数据库。 它目前支持这些现成的存储后端:
- MongoDB
- mysql
- redis
- memcached
但是,您并没有绑定到这些实现。 通过附加由oauth2.store中的基类定义的接口, 您可以轻松地添加后端的实现。 也可以混合不同的后端,例如读取客户机的数据 在memcached中保存所有令牌以进行快速访问时从MongoDB。
查看项目目录examples中的示例
站点适配器
与存储类似,python-oauth2没有定义如何标识用户或 显示确认对话。 相反,您的应用程序应该使用由 oauth2.web.SiteAdapter。