OAuth 2.0 Provider Implementation Tutorial

Activate the virtual environment ./venv.

$ . ./venv/bin/activate

Add the new provider to functional tests

Change directory to ./tests/functional_tests/.

$(venv) cd ./tests/functional_tests/

Create the expected values module for the new provider by copying the tests/functional_tests/expected_values/new_provider.py.template file to tests/functional_tests/expected_values/eventbrite.py.

$(venv) cp expected_values/new_provider.py.template expected_values/eventbrite.py

Create the functional tests config from the template tests/functional_tests/config-template.py.

$(venv) cp config-template.py config.py

Add a new provider slug to the INCLUDE_PROVIDERS list in the config in correct alphabetical order and comment out all the other providers.

# tests/functional_tests/config.py

# Only providers included here will be tested.
# This is a convenience to easily exclude providers from tests by commenting
# them out.
    # 'behance',
    # 'bitly',
    # 'deviantart',
    # 'facebook',
    # 'foursquare',
    # 'google',
    # 'github',
    # 'linkedin',
    # 'paypal',
    # 'reddit',
    # 'vk',
    # 'windowslive',
    # 'yammer',
    # 'yandex',

Run the test. Make sure that you have the virtual environment active.

$(venv) py.test -vv

The test should fail with the No record for the provider "provider_slug" was not found in the config! exception. Replace the 'provider_slug' value on line 7 with the slug of the new provider.

# tests/functional_tests/expected_values/eventbrite.py

# (...)

conf = fixtures.get_configuration('eventbrite')

Run the test again.

$(venv) py.test -vv

It should fail again, this time with this exception: No record for the provider "eventbrite" was not found in the config!. We need to add a record for the new provider to the actual config dictionary.

  1. Register by the provider and fill out ALL possible user profile fields.
  2. Register an OAuth 2.0 application by the provider.
  3. Find the OAuth 2.0 flow and user info endpoints in the provider’s API documentation.
  4. Make a domain alias for localhost.
  5. Launch the ./examples/flask/functional_test application.
  6. Create a new class for the provider.
  7. Override the OAuth 2.0 enpoints.
  8. Add the provider to functional tests.
  1. Add an entry to the functional tests config.
  2. Comment out all other providers in the INCLUDE_PROVIDERS list.
  1. Refresh the functional tests app and click on the link of the new provider.
    1. On the consent page open the browser inspector and copy the login, password and consent button(s) xpaths to the expected values file.
    2. If there was a problem debug, identify the problem and handle it in the _x_credentials_parser() method.
    3. If everything went good there should be a syntax highlighted json response.
    4. If the json response has any properties which were not automatically parsed to the user object you need to override the _x_user_parser() method.
  1. Stop the functional tests app.
  2. Run py.test -vv tests/functional_tests
  3. A Firefox window should open and the tests should fill out the consent form and hit the consent button.
  1. Add the provider’s OAuth 2.0, API and app dashboard links if any to the docstring.
Fork me on GitHub