Python and Django

In this example we will discuss the code required to configure a Django-based web-application to make use of Raven OAuth2 for sign in. We assume you've covered the first steps with Raven and have created some OAuth2 client credentials.

The python-social-auth package has direct support for OAuth2 and integration with a number of Python frameworks including Django. Most of the configuration we do in this guide could be ported to other Python web frameworks such as Flask or CherryPy.

Following the "golden rules"

When using Raven OAuth2, it is a "golden rule" that we verify at least the hd claim on sign in. Python-social-auth supports this by means of custom "pipelines" where small portions of code are run to process a sign in.

In the Django project module, we can add a small function to check the hd claim against a list of whitelisted domains:

# In project/

from social_core.exceptions import AuthForbidden

def enforce_hosted_domain(*, backend, response, **kwargs):
    A pipeline for use with django-social-auth's Google OAuth2 support which
    enforces that the signed in user is a member of one of the

    hosted_domain = response.get('hd')
    whitelist = backend.setting('WHITELISTED_HOSTED_DOMAINS', None)
    if whitelist is not None and hosted_domain not in whitelist:
        raise AuthForbidden(backend)

Configuring Django

Once we have our OAuth2 credentials, we can configure Django to make use of python-social-auth for sign in. See the corresponding section of the python-social-auth documentation for more information.

# In project/

    # ... other apps ...
    # ... other apps ...



SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'your-client-id'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'your-client-secret'
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = ['email', 'profile']

    # use our custom pipeline to check the "hd" claim

LOGIN_URL = '/accounts/login/google-oauth2/'
# In project/

urlpatterns = [
    # ...
    path('accounts/', include('django.contrib.auth.urls')),
    path('accounts/', include('social_django.urls', namespace='social')),
    # ...

Requiring sign in

Now we have configured Django, we can use the login_required decorator on views to enforce that a user has signed in to the site. A Django user object will be created for each new user on the site and we can make use of the Django groups and permissions settings to control what that user can do.

Last update: August 23, 2022