Skip to main content

OAuth 2.0 provider for python

Project description

python-oauth2 is a framework that aims at making it easy to provide authentication via OAuth 2.0 within an application stack.



python-oauth2 is currently not ready for use in production environments. While the basic implementations work already pretty well, some types of authorization Grants defined in the RFC are still missing.


python-oauth2 is available on PyPI.

pip install python-oauth2


Example Authorization server:

from wsgiref.simple_server import make_server
import oauth2
import oauth2.grant
import oauth2.error
import oauth2.tokengenerator
import oauth2.web

# Create a SiteAdapter to interact with the user.
# This can be used to display confirmation dialogs and the like.
class ExampleSiteAdapter(oauth2.web.SiteAdapter):
    def authenticate(self, request, environ, scopes):
        if request.post_param("confirm") == "1":
            return {}

        raise oauth2.error.UserNotAuthenticated

    def render_auth_page(self, request, response, environ, scopes):
        response.body = '''
        <form method="POST" name="confirmation_form">
            <input name="confirm" type="hidden" value="1" />
            <input type="submit" value="confirm" />
        return response

# Create an in-memory storage to store your client apps.
client_store =
# Add a client
client_store.add_client(client_id="abc", client_secret="xyz",

# Create an in-memory storage to store issued tokens.
# LocalTokenStore can store access and auth tokens
token_store =

# Create the controller.
auth_controller = oauth2.AuthorizationController(

# Add Grants you want to support

# Add refresh token capability and set expiration time of access tokens
# to 30 days

# Wrap the controller with the Wsgi adapter
app = oauth2.web.Wsgi(server=auth_controller)

if __name__ == "__main__":
    httpd = make_server('', 8080, app)

Storage adapters

python-oauth2 handles the request/response flow needed to create a OAuth 2.0 token. It does not define how a token is stored so you can choose the persistence strategy that works best for you. It is possible to write a token to mysql or mongodb for persistence, save it in memcache or redis for fast access or mix both approaches. This flexibility is achieved by the use of storage adapters that define an interface which is called by a Grant handler during processing.

The module defines base classes for each type of storage. Also take a look at the examples in the examples directory of the project.

Site adapter

Like 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 by oauth2.web.SiteAdapter.


New in version 0.4.0

  • Added support for refresh tokens

New in version 0.3.2

  • Fixed a bug where MemcacheTokenStore saved objects instead of dictionaries.

New in version 0.3.1

  • Fixed a bug causing a supplied redirect uri being ignored if it is not the first entry in the list of a client object.

New in version 0.3.0

  • Headers of a response are returned as a dictionary
  • Status code of a response is an integer
  • Streamlining the integration of storage classes and site adapters by requiring them to raise specified errors

New in version 0.2.0

  • Support for scopes
  • Local token and client stores
  • Memcache token store
  • Support for Python 2.6, 3.2 and 3.3

New in version 0.1.0

  • Working implementation of Authorization Code Grant
  • Working implementation of Implicit Grant
  • Working implementation of Resource Owner Password Credentials Grant

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for python-oauth2, version 0.4.0
Filename, size File type Python version Upload date Hashes
Filename, size python-oauth2-0.4.0.tar.gz (24.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page