def _googleAuth(user): google_auth = UserSocialAuth() google_auth.user = user google_auth.provider = 'google' google_auth.uid = user.email google_auth.extra_data = '{}' return google_auth
def authenticate(self, *args, **kwargs): """Authenticate user using social credentials Authentication is made if this is the correct backend, backend verification is made by kwargs inspection for current backend name presence. """ # Validate backend and arguments. Require that the Social Auth # response be passed in as a keyword argument, to make sure we # don't match the username/password calling conventions of # authenticate. if not (self.name and kwargs.get(self.name) and 'response' in kwargs): return None response = kwargs.get('response') details = self.get_user_details(response) uid = self.get_user_id(details, response) is_new = False user = kwargs.get('user') try: social_user = self.get_social_auth_user(uid) except UserSocialAuth.DoesNotExist: if user is None and HOLD_SOCIAL_USER and not CREATE_USERS: # create fake user, and create social user in session user = User() user.is_fake = True social_user = UserSocialAuth(user=user, uid=uid, provider=self.name) user.social_user = social_user if LOAD_EXTRA_DATA: extra_data = self.extra_data(user, uid, response, details) if extra_data and social_user.extra_data != extra_data: social_user.extra_data = extra_data # todo: get request #self.request.session[SESSION_USER_NAME] = UserSocialAuth.objects.get(id=social_user.id) #request.session['tmpUserProfile'] = user.get_profile() # sending pre_update signal self.update_user_details(user, response, details, is_new) return user if user is None: # new user if not CREATE_USERS or not kwargs.get('create_user', True): # Send signal for cases where tracking failed registering # is useful. socialauth_not_registered.send(sender=self.__class__, uid=uid, response=response, details=details) return None email = details.get('email') if email and ASSOCIATE_BY_MAIL: # try to associate accounts registered with the same email # address, only if it's a single object. ValueError is # raised if multiple objects are returned try: user = User.objects.get(email=email) except MultipleObjectsReturned: raise ValueError('Not unique email address supplied') except User.DoesNotExist: user = None if not user: username = self.username(details) user = User.objects.create_user(username=username, email=email) is_new = True 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) # Update extra_data storage, unless disabled by setting if LOAD_EXTRA_DATA: extra_data = self.extra_data(user, uid, response, details) if extra_data and social_user.extra_data != extra_data: social_user.extra_data = extra_data social_user.save() user.social_user = social_user # Update user account data. self.update_user_details(user, response, details, is_new) return user