Categories
Python

Python Social Auth: Custom pipeline

Eventually, from “Django Social Auth”, came “Python Social Auth” which is compatible with a number of other frameworks as well. This package is now the recommended one for integrating with social networks. If you have worked with Django Social Auth in the past, the setup and configuration is pretty straightforward. The major difference is in the package names (obviously!).

So, in one of my apps, I am using Facebook logins. And despite the inherent security risk (I am not going to elaborate here), I have been asked to implement the “associate by email address” feature. The idea is simple – if there is an user account already with the same email address of the social account being linked, connect them together.

Like Django Social Auth before, Python Social Auth has the same Pipeline architecture. The SOCIAL_AUTH_PIPELINE is a tuple of functions which are passed various data (when I say various, don’t get me wrong, it pretty much passes everything in the app). The data get passed to the first function, the return value of the first function get passed to the second – this way, it’s a pipeline of functions where one function’s return value is fed into another.

With that in mind, I just need to create a function that sits somewhere in the pipeline and checks for the email address returned from the social network. If any existing user accounts found, associate that as the user. So, I created a function in “core/utils.py” so that the full function identifier would be – “core.utils.associate_by_email”.

The contents are:

We are accepting all the arguments to the function using **kwargs. We’re then grabbing the email address from the “details” dictionary and setting the user if found. Being lazy, I surrounded the block with a try..except block for any exceptions which might arise.

Now, in settings, I added the pipeline like:

So, we set the user before we create an user. Thus if there is an existing user, we just update that user, otherwise, we create a new one.

I really love how simple and easy it is to extend the Python Social Auth package, no wonder it’s become the de-facto choice for social networks integration. 🙂

4 replies on “Python Social Auth: Custom pipeline”

Thank you!

One doubt, if I want to add some extra information to user profile, based on the login-authentication link user clicked.

I am thinking to provide extra parameter to the authentication url, like:
<a href=”{% url ‘social:begin’ ‘facebook’ %}?next={{ request.path }}&profile_type=0″>Login with faceboook</a>
<a href=”{% url ‘social:begin’ ‘facebook’ %}?next={{ request.path }}&profile_type=1″>Login with faceboook</a>

Is it possible to access the profile_type value form pipeline function? Or how can I achieve something like that?

Comments are closed.