]> git.parisson.com Git - django-social-auth.git/commitdiff
Protect for possible race condition. Refs #131
authorMatías Aguirre <matiasaguirre@gmail.com>
Thu, 25 Aug 2011 23:27:50 +0000 (20:27 -0300)
committerMatías Aguirre <matiasaguirre@gmail.com>
Thu, 25 Aug 2011 23:27:50 +0000 (20:27 -0300)
social_auth/backends/__init__.py

index 2bda3972d9d60ebafee44b929af72ccb6bf9155d..8fa45a1e495ad3107e379e0d89d5406a933dc99c 100644 (file)
@@ -29,6 +29,7 @@ from django.contrib.auth import authenticate
 from django.contrib.auth.backends import ModelBackend
 from django.utils import simplejson
 from django.utils.importlib import import_module
+from django.db.utils import IntegrityError
 
 from social_auth.models import UserSocialAuth
 from social_auth.store import DjangoOpenIDStore
@@ -128,16 +129,18 @@ class SocialAuthBackend(ModelBackend):
                     user = User.objects.create_user(username=username,
                                                     email=email)
                     is_new = True
-            social_user = self.associate_auth(user, uid, response, details)
-        else:
-            # This account was registered to another user, so we raise an
-            # error in such case and the view should decide what to do on
-            # at this moment, merging account is not an option because that
-            # would imply update user references on other apps, that's too
-            # much intrusive
-            if user and user != social_user.user:
-                raise ValueError('Account already in use.', social_user)
-            user = social_user.user
+
+            try:
+                social_user = self.associate_auth(user, uid, response, details)
+            except IntegrityError:
+                # Protect for possible race condition, those bastard with FTL
+                # clicking capabilities
+                social_user = self.get_social_auth_user(uid)
+
+        # Raise ValueError if this account was registered by another user.
+        if user and user != social_user.user:
+            raise ValueError('Account already in use.', social_user)
+        user = social_user.user
 
         # Flag user "new" status
         setattr(user, 'is_new', is_new)