Source

django-publicauth / openauth / __init__.py

from django.contrib.auth.models import User

from openauth.models import SocialID
from openauth import signals
from openauth import settings
from openauth import lang


class SocialBackend(object):
    """
    Add this Authentication Backend to 
    AUTHENTICATION_BACKENDS tuple in your settings.py
    """

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

    def authenticate(self, identity=None, provider=None):
        """
        Authenticate user by social identity.
        """
        if identity:
            try:
                user = SocialID.objects.get(identity=identity, provider=provider).user
                return user
            except SocialID.DoesNotExist:
                return None
        else:
            return None