Django

Social Auth in Django

Today we are going to talk about a library to authenticate with social networks. This library is “python-social-auth”  - https://python-social-auth.readthedocs.io/en/latest/. With this library we can implement the authentication and authorization system with a several group of social networks, link to the sign-in to an existing account or do the register process using a social network.

To explain how use this library we are going to divide in different sections:

INSTALLATION

To install the library we use the python package control: pip install python-social-auth.

pip install python-social-auth


By doing this, we install the library and we proceed to configure it.

CONFIGURATION

To configure the library in our django application, we must add a set of settings:

INSTALLED_APPS

Add the library to INSTALLED_APPS. If we are using the ORM of django, we need to add  ‘social.apps.django_app.default’. If we are using the MongoEngine ORM we need to add ‘social.apps.django_app.me’ and add SOCIAL_AUTH_STORAGE = 'social.apps.django_app.me.models.DjangoStorage'.

An example of this configuration is the following:

INSTALLED_APPS = (
    ...,
    'social.apps.django_app.default',
    # 'social.apps.django_app.me.models.DjangoStorage', Only if we use MongoEngine ORM 
    ...,
)

# Only if we use MongoEngine ORM
# SOCIAL_AUTH_STORAGE = 'social.apps.django_app.me.models.DjangoStorage'

ARE WE USING Soauth?

If we use Soauth, we must override the SOUTH_MIGRATION_MODULES setting. To override this setting we can do:

SOUTH_MIGRATION_MODULES = {
    'default': 'social.apps.django_app.default.south_migrations'
}

RUN THE MIGRATIONS

In the next step we must run the migrations. If we are using a version of Django <= 1.7 we must use python manage.py syncdb. Otherwise, we must use python manage.py migrate.

DECLARE THE BACKENDS FOR AUTH

How we define the applications that we allow to sign in? To add the applications we must configure the AUTHENTICATION_BACKENDS setting by adding the backends to use. The library has a huge list with the supported applications - http://python-social-auth.readthedocs.io/en/latest/backends/index.html#supported-backends. For example, if we want add the auth system with Facebook, we must add the following setting:

AUTHENTICATION_BACKENDS = (
    ...,
    'social.backends.facebook.FacebookOAuth2',
    ...,
    'django.contrib.auth.backends.ModelBackend',
    ...,
)
# In addition, we must configure the client key and the secret key. 
# Following the Facebook example, we need add these two parameters:
SOCIAL_AUTH_FACEBOOK_KEY = 'app-id'
SOCIAL_AUTH_FACEBOOK_SECRET = 'app-secret'

In the appendix we will talk about how to get the client key and the secret for Facebook, Google and Twitter.

CONFIGURE THE URLS

Until now, we have not talked about the url to do log-in. Well, python-social-auth has a URL for the log-in. To add this,  we have to write the next line in the urlpatterns file urls.py:

urlpatterns = [
    ...,
    url('', include('social.apps.django_app.urls', namespace='social')),
    ...,
]

We added the extra parameter namespace to the url which is optional, but if you add it, you must configure the following setting:

SOCIAL_AUTH_URL_NAMESPACE = 'social'

CONFIGURE THE TEMPLATE CONTEXT PROCESSORS

Finally, we must configure the context_processors for the templates:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...,
    'social.apps.django_app.context_processors.backends',
    'social.apps.django_app.context_processors.login_redirect',
    ...,
)

At this point, we should have configured the library and we can start to work and add the external sign-in for our django application. Finally, we must add the authentication backend for each application that we allow in our application.

APPENDIX

AUTH WITH GOOGLE 

To configure sign-in with Google we must register the Google+ API in the Google Developers Console and create a client of OAuth 2.0. When we have created the client, we must add the credentials to the settings.py:

AUTHENTICATION_BACKENDS = (
    ...,
    'social.backends.google.GoogleOAuth2',
    ...,
)
# Add the google credentials
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'client-id'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'secret-key'

Finally,  we configure the redirect URL to successful sign-in. If you are using the same url configuration that this tutorial, the redirect url must be like: http://<domain>:<port>/complete/google-oauth2/.

AUTH WITH FACEBOOK

To configure the log-in with Facebook we must create an application in Facebook Developers. When we have created the Facebook application, we must go to settings and obtain the App ID and App Secret. When we get the credentials, we must add the credentials to the settings.py:

AUTHENTICATION_BACKENDS = (
    ...,
    'social.backends.facebook.FacebookOAuth2',
    ...,
)
# Add the Facebook app credentials.
SOCIAL_AUTH_FACEBOOK_KEY = 'app-id'
SOCIAL_AUTH_FACEBOOK_SECRET = 'app-secret'


    
Finally, we configure the redirect URL to successful sign-in. If you are using the same url configuration that this tutorial, the redirect url must be like: http://<domain>:<port>.

AUTH WITH TWITTER

To configure the sign-in with Twitter we must create an application in Twitter Developers. When we have created the Twitter application. Now, we need get the Consumer Key and the Consumer Secret.  To get it, we must go to ‘Keys and Access Tokens’  and get the Consumer Key and Consumer Secret. With this information, we must add this credentials in the settings.py:

AUTHENTICATION_BACKENDS = (
    ...,
    'social.backends.twitter.TwitterOAuth',
    ...,
)
# Add twitter credentials.
SOCIAL_AUTH_TWITTER_KEY = 'consumer-key'
SOCIAL_AUTH_TWITTER_SECRET = 'consumer-secret'

Finally, we need add a Callback URL. To add this url, we must go to settings and introduce in this field an url like this: http://<domain>:<port>/complete/twitter/

About the author

How to add Social Media buttons to Django admin
02.20.2017

How to add Social Media buttons to Django admin

The Django admin panel is one of the strong points and one of the features we like most, and one of the ways it has as a ...

How to use managers in Django
02.16.2017

How to use managers in Django

The communication with the database and the abstraction about the database’s engine we are using is one of the best features ...

Let’s have a coffee and talk about your project

START A PROJECT

Let’s have a coffee and talk about your project

START A PROJECT

We use cookies to ensure you get the best experience on our website. More info.

Accept