Ejemplo n.º 1
0
    def authenticate(self, access_token):
        '''authenticates the token by requesting user information from twitter
        '''
        twitter = oauthtwitter.OAuthApi(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, access_token)

        try:
            userinfo = twitter.GetUserInfo()
        except:
            # If we cannot get the user information, user cannot be authenticated
            raise

        screen_name = userinfo.screen_name
        img_url = userinfo.profile_image_url
        
        try:
            user_profile = TwitterUserProfile.objects.get(screen_name = screen_name)
            if user_profile.user.is_active:
                return user_profile.user
            else:
                return        
        except TwitterUserProfile.DoesNotExist:
            #Create new user
            same_name_count = User.objects.filter(username__startswith = screen_name).count()
            if same_name_count:
                username = '******' % (screen_name, same_name_count + 1)
            else:
                username = screen_name
            username = '******'+username
            
            name_count = AuthUser.objects.filter(username__startswith = username).count()
                
            if name_count:
                username = '******'%(username, name_count + 1)
                            
            user = User(username =  username)
            temp_password = User.objects.make_random_password(length=12)
            user.set_password(temp_password)
            name_data = userinfo.name.split()
            try:
                first_name, last_name = name_data[0], ' '.join(name_data[1:])
            except:
                first_name, last_name =  screen_name, ''
            user.first_name, user.last_name = first_name, last_name
            if img_url:
                img = ContentFile(urlopen(img_url).read())
                name = img_url.split('/')[-1]
                user.picture.save(name, img, False)            
            #user.email = '%s@twitteruser.%s.com'%(userinfo.screen_name, settings.SITE_NAME)
            user.save()
            userprofile = TwitterUserProfile(user = user, screen_name = screen_name)
            userprofile.access_token = access_token.key
            userprofile.url = userinfo.url
            userprofile.location = userinfo.location
            userprofile.description = userinfo.description
            userprofile.profile_image_url = userinfo.profile_image_url
            userprofile.save()
            AuthMeta(user=user, provider='Twitter').save()
            return user
Ejemplo n.º 2
0
    def authenticate(self, access_token):
        twitter = oauthtwitter.OAuthApi(TWITTER_CONSUMER_KEY,
                                        TWITTER_CONSUMER_SECRET, access_token)
        try:
            userinfo = twitter.GetUserInfo()
        except:
            # If we cannot get the user information, user cannot be authenticated
            raise

        user = self._get_existing_user(userinfo)
        if not user:
            user = self._create_user(access_token, userinfo)

        return user
Ejemplo n.º 3
0
    def pre_authenticate(access_token):
        twitter = oauthtwitter.OAuthApi(TWITTER_CONSUMER_KEY,
                                        TWITTER_CONSUMER_SECRET, access_token)
        try:
            userinfo = twitter.GetUserInfo()
        except:
            # If we cannot get the user information, user cannot be authenticated
            raise

        user = TwitterAuthBackend._get_existing_user(userinfo)
        if user:
            return (True, '')

        return (False,
                TwitterAuthBackend._generate_email(userinfo.screen_name))
Ejemplo n.º 4
0
    def authenticate(self, access_token):
        """ authenticates the token by requesting user information from twitter """
        try:
            api = oauthtwitter.OAuthApi(TWITTER_CONSUMER_KEY,
                                        TWITTER_CONSUMER_SECRET, access_token)
            userinfo = api.GetUserInfo()
        except:
            # If we cannot get the user information, user cannot be authenticated
            raise

        try:
            user_profile = TwitterUserProfile.objects.get(
                screen_name=userinfo.screen_name)
            if user_profile.user.is_active:
                return user_profile.user
            else:
                return
        except TwitterUserProfile.DoesNotExist:
            #Create new user
            username = userinfo.screen_name
            user = new_user(username=username, provider='Twitter')
            name_data = userinfo.name.split()
            try:
                first_name, last_name = name_data[0], ' '.join(name_data[1:])
            except:
                first_name, last_name = userinfo.screen_name, ''
            user.first_name, user.last_name = first_name, last_name
            user.save()
            img_url = userinfo.profile_image_url
            userprofile = TwitterUserProfile(user=user,
                                             screen_name=userinfo.screen_name)
            userprofile.access_token = access_token.key
            userprofile.url = userinfo.url
            userprofile.location = userinfo.location
            userprofile.description = userinfo.description
            userprofile.profile_image_url = userinfo.profile_image_url
            #user.email = '%s@Twitteruser.%s.com'%(userinfo.screen_name, settings.SITE_NAME)
            if img_url:
                img = ContentFile(urlopen(img_url).read())
                name = img_url.split('/')[-1]
                user.picture.save(name, img, False)

            userprofile.save()
            AuthMeta(user=user, provider='Twitter').save()
            return user