Package: python-oauth2
This project is not maintained anymore.
If you are looking for a OAuth 2.0 library to integrate into your Python application, I recommendoauthlib.python-oauth2python-oauth2 is a framework that aims at making it easy to provide authentication
viaOAuth 2.0within an application stack.DocumentationStatuspython-oauth2 has reached its beta phase. All main parts of theOAuth 2.0 RFCsuch as the various types of Grants, Refresh Token and Scopes have been implemented. However, bugs might occur or implementation details might be wrong.Installationpython-oauth2 is available onPyPI.pip install python-oauth2UsageExample Authorization serverfromwsgiref.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()This example only shows how to instantiate the server.
It is not a working example as a client app is missing. Take a look at theexamplesdirectory.Supported storage backendspython-oauth2 does not force you to use a specific database.
It currently supports these storage backends out-of-the-box:MongoDBMySQLRedisMemcachedHowever, you are not not bound to these implementations.
By adhering to the interface defined by the base classes inoauth2.store,
you can easily add an implementation of your backend.
It also is possible to mix different backends and e.g. read data of a client
from MongoDB while saving all tokens in memcached for fast access.Take a look at the examples in theexamplesdirectory of the project.Site adapterLike for storage, python-oauth2 does not define how you identify a user or
show a confirmation dialogue.
Instead your application should use the API defined byoauth2.web.SiteAdapter.