Wiki

Clone wiki

django-publicauth / Home

Description

Django application that allows authenticate users through OpenID/OAuth/Facebook/Vkontakte.

Successfully used for a year in django community website.

Requirements

Installation

  • Download and run python setup.py install or put publicauth directory on PYTHONPATH
  • Add 'publicauth' to your INSTALLED_APPS tuple
  • Add 'annoying.middlewares.RedirectMiddleware' to your MIDDLEWARE_CLASSES
  • Add url(r'^auth/', include('publicauth.urls')) to your base urls.py
  • Set FOO_PROFILE_MAPPING

Facebook/OpenID/OAuth/Vkontakte

  • Add 'publicauth.PublicBackend' to your AUTHENTICATION_BACKENDS
  • run python manage.py syncdb

Facebook

  • Install PyFacebook
  • Add 'facebook.djangofb.FacebookMiddleware' to your MIDDLEWARE_CLASSES
  • Go to http://www.facebook.com/developers/ and create application
  • Set FACEBOOK_API_KEY and FACEBOOK_SECRET_KEY in your settings.py file.
  • Add next code to your login page template (can be "templates/publicauth/login.html" if you want to use built in login view.
<a href="#" onclick="return fb_login();" id="facebook">Login with facebook</a>

<script type="text/javascript" src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"></script>
<script type="text/javascript">
    FB.init("{{ request.facebook.api_key }}", "{% url publicauth-facebook-xdreceiver %}");

    function fb_login() {
        FB.Connect.requireSession(function(test) {
            window.location = "{% url publicauth-complete 'facebook' %}?next={{ request.GET.next }}";
        });
        return false;
    }
</script>

OpenID

  • Install python-openid

OAuth (Twitter)

Vkontakte

  • Create application in vkontakte.ru webiste and set in your settings.py file next variables (replace values with yours)
VKONTAKTE_APP_ID = XXXX
VKONTAKTE_API_KEY = "XXXX"
VKONTAKTE_SECRET_KEY = "XXXXXXXXX"
  • Note that VKONTAKTE_APP_ID is integer, not a string, also this is the case in template below
  • Then add folowing html to your login page
<a onclick="return vk_login();" href="#">Вконтакте.ru</a><


<script src="http://vkontakte.ru/js/api/openapi.js" type="text/javascript" charset="windows-1251"></script>
<script type="text/javascript">
VK.init({
    apiId: xxxxx,
    nameTransportPath: '{% url publicauth-vkontakte-xdreceiver %}'
});

function vk_login() {
    VK.Auth.login(testMe);
    return false;
}

function testMe(response) {
    if (response.status == "connected") {
        window.location = "{% url publicauth-complete 'vkontakte' %}"
    }
};
</script>

Defaults

By default every authentication method is allowed. To configure specific backends, please set PUBLICAUTH_BACKEND_MAPPING in your settings.py file. To see example, go to publicauth.settings.py file

Note

You need to setup messages framework as described in django documentation

Updated