]> git.parisson.com Git - django-social-auth.git/commitdiff
Use OpenID identity_url field to identify UserSocialAuth instances. Closes gh-73
authorMatías Aguirre <matiasaguirre@gmail.com>
Mon, 23 May 2011 03:17:07 +0000 (00:17 -0300)
committerMatías Aguirre <matiasaguirre@gmail.com>
Mon, 23 May 2011 03:17:07 +0000 (00:17 -0300)
social_auth/backends/__init__.py
social_auth/views.py

index 4eb1f15953dffa66b35cbddc109b5bf584dc1994..d7b4a2de59bd09b6d99ae782426f44e69e5b7eff 100644 (file)
@@ -103,9 +103,7 @@ class SocialAuthBackend(ModelBackend):
         uid = self.get_user_id(details, response)
         is_new = False
         try:
-            social_user = UserSocialAuth.objects.select_related('user')\
-                                                .get(provider=self.name,
-                                                     uid=uid)
+            social_user = self.get_social_auth_user(uid)
         except UserSocialAuth.DoesNotExist:
             user = kwargs.get('user')
             if user is None:  # new user
@@ -149,6 +147,7 @@ class SocialAuthBackend(ModelBackend):
                 social_user.extra_data = extra_data
                 social_user.save()
 
+        user.social_user = social_user
         return user
 
     def username(self, details):
@@ -245,6 +244,15 @@ class SocialAuthBackend(ModelBackend):
         if changed:
             user.save()
 
+    def get_social_auth_user(self, uid):
+        """Return social auth user instance for given uid for current
+        backend.
+
+        Riase DoesNotExist exception if no entry.
+        """
+        return UserSocialAuth.objects.select_related('user')\
+                                     .get(provider=self.name, uid=uid)
+
     def get_user_id(self, details, response):
         """Must return a unique ID from values returned on details"""
         raise NotImplementedError('Implement in subclass')
@@ -300,6 +308,15 @@ class OpenIDBackend(SocialAuthBackend):
     """Generic OpenID authentication backend"""
     name = 'openid'
 
+    def get_social_auth_user(self, uid):
+        """Return social auth user instance for given uid. OpenId uses
+        identity_url to identify the user in a unique way and that value
+        identifies the provider too.
+
+        Riase DoesNotExist exception if no entry.
+        """
+        return UserSocialAuth.objects.select_related('user').get(uid=uid)
+
     def get_user_id(self, details, response):
         """Return user unique id provided by service"""
         return response.identity_url
index 23fcb63b9678fd195d35ce497477d975bde96f15..69678d895a34e8548a242e2daf3c188c4ba3f27f 100644 (file)
@@ -4,7 +4,7 @@ from django.http import HttpResponseRedirect, HttpResponse, \
                         HttpResponseServerError
 from django.core.urlresolvers import reverse
 from django.db import transaction
-from django.contrib.auth import login, REDIRECT_FIELD_NAME
+from django.contrib.auth import login, REDIRECT_FIELD_NAME, load_backend
 from django.contrib.auth.decorators import login_required
 
 from social_auth.backends import get_backend
@@ -49,9 +49,10 @@ def complete_process(request, backend):
             # Set session expiration date if present and not disabled by
             # setting. Use last social-auth instance for current provider,
             # users can associate several accounts with a same provider.
-            backend_name = backend.AUTH_BACKEND.name
-            social_user = user.social_auth.filter(provider=backend_name) \
-                                          .order_by('-id')[0]
+            #
+            # user.social_user is the used UserSocialAuth instance defined
+            # in authenticate process
+            social_user = user.social_user
             if social_user.expiration_delta():
                 request.session.set_expiry(social_user.expiration_delta())
         url = request.session.pop(REDIRECT_FIELD_NAME, '') or DEFAULT_REDIRECT