With the ever increasing social media and enourmous web presence of the human population as a whole as developers we have a lot of resources and information about our users available for us to us in our applications. A most import resource, and one that should never be taken lightly, are our users authentication credentials. If we take a minute to think from a user standpoint, when you go to a new website that wants to maintain information pertainent to you, the easiest thing that site can do is allow you to 'login' with your credentials that you have already created elsewhere, such as Facebook, Github, etc. From a developer standpoint this is a great, and extremely encouraged, approach to implementing 'Users' of our websites. Let's offload the liability and grunt work of trying to roll our own security system, and simply interface with an existing API provided from a trusted 3rd party source instead. We are going to do just that in this tutorial using the amazing Django package 'django-social-auth'. The source can be found on Github here, and a great set of thorough documentations about this package can be found here.
Prereq) This tutorial assumes you have a base site setup, with a database configured and the Admin suite up and running, check previous tutorials if you have questions.
1) We need to download the package and install it:
git clone git://github.com/omab/django-social-auth.git
cd django-social-auth
sudo python setup.py install
2) Add 'social_auth' to your 'INSTALLED_APPS' dictionary in testsite/settings.py:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'social_auth',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
3) Commit the models for 'social_auth' to your local database:
python manage.py syncdb
4) We need to add in our desired authentication backend in our settings file, by default the settings file doesn't contain this tuple but it is located in the global settings of Django, thus add the following to your settings.py file in order to simply append our desired authentication backend to the existing tuple:
from django.conf import global_settings
AUTHENTICATION_BACKENDS = global_settings.AUTHENTICATION_BACKENDS + (
'social_auth.backends.contrib.github.GithubBackend',
)
5) Next, we need to add 'social_auth's urls to our site's url configuration in testsite/urls.py:
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'testsite.views.home', name='home'),
# url(r'^testsite/', include('testsite.foo.urls')),
url(r'', include('social_auth.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
6) The next step is to obtain an API ID and and API secret key from Github. You can do so by going to the 'Application' section in your account settings and registering your application by its domain. Once you grab those two things we need to insert them into our settings.py file as such ( I have X'd mine out here):
# github settings
GITHUB_APP_ID = 'XXXXXXXXXXXX'
GITHUB_API_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
7) Next, add two more settings to the settings.py which are URL settings that the Github API will use when directing users back to your website ( I just set the URL's back to the root of testsite ):
LOGIN_URL = '/'
LOGIN_REDIRECT_URL = '/'
8) Now lets create a 'login.html' which is where we want our users to utilize this login functionality. Create a directory called 'templates' inside testsite/. Then create a baseline html file called 'login.html' inside templates/, here is mine:
<html>
<body>
<a href="{% url "socialauth_begin" "github" %}">Login with Github</a>
</body>
</html>
)
9) Now we need to create a controller function that will serve up our login.html, so in views.py:
from django.shortcuts import render_to_response
def login_user( request ):
return render_to_response( 'login.html' )
10) And last, but very important, lets wire up a url to the controller we just created. In testsite/urls.py:
from controllers import *
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'testsite.views.home', name='home'),
# url(r'^testsite/', include('testsite.foo.urls')),
url( r'^$', controllers.login_user ),
url(r'', include('social_auth.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
11) That's it, go to your login page and click the button :)