Esempio n. 1
0
def social_user(backend, uid, details, user=None, *args, **kwargs):
    voter_manager = VoterManager()
    provider = backend.name
    social = backend.strategy.storage.user.get_social_auth(provider, uid)

    if backend.name == 'twitter':
        # Twitter: Check to see if we have a voter with a matching twitter_id
        local_user_matches = user and user.twitter_id == uid
    else:
        local_user_matches = user and user.email != details.get('email')
    switch_user = not local_user_matches
    if switch_user:
        # Logout the current Django user
        logout(backend.strategy.request)

        user = None

        voter_that_matches_auth = None
        voter_found_that_matches_auth = False
        if social:
            if social.user:
                if backend.name == 'twitter':
                    if social.user.twitter_id == uid:
                        voter_that_matches_auth = social.user
                        voter_found_that_matches_auth = True
                    else:
                        pass

        if not voter_found_that_matches_auth:
            # Find the voter account that actually matches this twitter_id
            results = voter_manager.retrieve_voter_by_twitter_id(uid)
            if results['voter_found']:
                voter_that_matches_auth = results['voter']
                voter_found_that_matches_auth = True

        if voter_found_that_matches_auth:
            user = voter_that_matches_auth
        else:
            # No other account matches this, so we want to save basic information in social.user
            if backend.name == 'twitter':
                if social and social.user:
                    twitter_user_dict = {
                        'id': uid,
                        'profile_image_url_https': kwargs['response']['profile_image_url_https'],
                        'screen_name': kwargs['response']['screen_name']
                    }
                    results = voter_manager.save_twitter_user_values_from_dict(social.user, twitter_user_dict)
                    if results['success']:
                        social.user = results['voter']
                        user = results['voter']

    return {'social': social,
            'user': user,
            'is_new': user is None,
            'switch_user': True,
            'new_association': False}
Esempio n. 2
0
def authenticate_associate_by_email(**kwargs):
    try:
        # Find the voter account that actually matches this twitter_id
        twitter_id = kwargs['uid']
        voter_manager = VoterManager()
        results = voter_manager.retrieve_voter_by_twitter_id(twitter_id)
        if results['voter_found']:
            kwargs['user'] = results['voter']
        else:
            kwargs['user'] = None
    except:
        pass
    return kwargs
Esempio n. 3
0
def authenticate_associate_by_email(**kwargs):
    try:
        # Find the voter account that actually matches this twitter_id
        twitter_id = kwargs['uid']
        voter_manager = VoterManager()
        results = voter_manager.retrieve_voter_by_twitter_id(twitter_id)
        if results['voter_found']:
            kwargs['user'] = results['voter']
        else:
            kwargs['user'] = None
    except:
        pass
    return kwargs
Esempio n. 4
0
def twitter_sign_in_request_voter_info_for_api(voter_device_id, return_url, switch_accounts_if_needed=True):
    """
    twitterSignInRequestVoterInfo
    When here, the incoming voter_device_id should already be authenticated
    :param voter_device_id:
    :param return_url: Where to return the browser when sign in process is complete
    :param switch_accounts_if_needed:
    :return:
    """

    twitter_handle = ''
    twitter_handle_found = False
    tweepy_user_object = None
    twitter_user_object_found = False
    voter_info_retrieved = False
    switch_accounts = False

    # Get voter_id from the voter_device_id
    results = is_voter_device_id_valid(voter_device_id)
    if not results['success']:
        results = {
            'success':              False,
            'status':               "VALID_VOTER_DEVICE_ID_MISSING",
            'voter_device_id':      voter_device_id,
            'twitter_handle':       twitter_handle,
            'twitter_handle_found': twitter_handle_found,
            'voter_info_retrieved': voter_info_retrieved,
            'switch_accounts':      switch_accounts,
            'return_url':           return_url,
        }
        return results

    voter_manager = VoterManager()
    results = voter_manager.retrieve_voter_from_voter_device_id(voter_device_id)
    if not positive_value_exists(results['voter_found']):
        results = {
            'status':               "VALID_VOTER_MISSING",
            'success':              False,
            'voter_device_id':      voter_device_id,
            'twitter_handle':       twitter_handle,
            'twitter_handle_found': twitter_handle_found,
            'voter_info_retrieved': voter_info_retrieved,
            'switch_accounts':      switch_accounts,
            'return_url':           return_url,
        }
        return results

    voter = results['voter']

    auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
    auth.set_access_token(voter.twitter_access_token, voter.twitter_access_secret)

    api = tweepy.API(auth)

    try:
        tweepy_user_object = api.me()
        twitter_json = tweepy_user_object._json

        success = True
        status = 'TWITTER_SIGN_IN_REQUEST_VOTER_INFO_SUCCESSFUL'
        twitter_handle = tweepy_user_object.screen_name
        twitter_handle_found = True
        twitter_user_object_found = True
    except tweepy.RateLimitError:
        success = False
        status = 'TWITTER_SIGN_IN_REQUEST_VOTER_INFO_RATE_LIMIT_ERROR'
    except tweepy.error.TweepError as error_instance:
        success = False
        status = 'TWITTER_SIGN_IN_REQUEST_VOTER_INFO_TWEEPY_ERROR: '
        error_tuple = error_instance.args
        for error_dict in error_tuple:
            for one_error in error_dict:
                status += '[' + one_error['message'] + '] '

    if twitter_user_object_found:
        # We need to deal with these cases

        # 1) Does account already exist?
        results = voter_manager.retrieve_voter_by_twitter_id(tweepy_user_object.id)
        if results['voter_found'] and switch_accounts_if_needed:
            voter_found_with_twitter_id = results['voter']

            switch_accounts = True

            # Relink this voter_device_id to the original account
            voter_device_manager = VoterDeviceLinkManager()
            voter_device_link_results = voter_device_manager.retrieve_voter_device_link(voter_device_id)
            voter_device_link = voter_device_link_results['voter_device_link']

            update_voter_device_link_results = voter_device_manager.update_voter_device_link(
                voter_device_link, voter_found_with_twitter_id)
            if update_voter_device_link_results['voter_device_link_updated']:
                # Transfer access token and secret
                voter_found_with_twitter_id.twitter_access_token = voter.twitter_access_token
                voter_found_with_twitter_id.twitter_access_secret = voter.twitter_access_secret
                voter_found_with_twitter_id.save()

                status += "TWITTER_SIGN_IN-ALREADY_LINKED_TO_OTHER_ACCOUNT-TRANSFERRED "
                success = True
                save_user_results = voter_manager.save_twitter_user_values(voter_found_with_twitter_id,
                                                                           tweepy_user_object)

                if save_user_results['success']:
                    voter_info_retrieved = True
                status += save_user_results['status']
            else:
                status = "TWITTER_SIGN_IN-ALREADY_LINKED_TO_OTHER_ACCOUNT-COULD_NOT_TRANSFER "
                success = False

        # 2) If account doesn't exist for this person, save
        else:
            save_user_results = voter_manager.save_twitter_user_values(voter, tweepy_user_object)

            if save_user_results['success']:
                voter_info_retrieved = True

    results = {
        'status':                       status,
        'success':                      success,
        'voter_device_id':              voter_device_id,
        'twitter_handle':               twitter_handle,
        'twitter_handle_found':         twitter_handle_found,
        'voter_info_retrieved':         voter_info_retrieved,
        'switch_accounts':              switch_accounts,
        'return_url':                   return_url,
    }
    return results
Esempio n. 5
0
def twitter_sign_in_request_voter_info_for_api(voter_device_id,
                                               return_url,
                                               switch_accounts_if_needed=True):
    """
    twitterSignInRequestVoterInfo
    When here, the incoming voter_device_id should already be authenticated
    :param voter_device_id:
    :param return_url: Where to return the browser when sign in process is complete
    :param switch_accounts_if_needed:
    :return:
    """

    twitter_handle = ''
    twitter_handle_found = False
    tweepy_user_object = None
    twitter_user_object_found = False
    voter_info_retrieved = False
    switch_accounts = False

    # Get voter_id from the voter_device_id
    results = is_voter_device_id_valid(voter_device_id)
    if not results['success']:
        results = {
            'success': False,
            'status': "VALID_VOTER_DEVICE_ID_MISSING",
            'voter_device_id': voter_device_id,
            'twitter_handle': twitter_handle,
            'twitter_handle_found': twitter_handle_found,
            'voter_info_retrieved': voter_info_retrieved,
            'switch_accounts': switch_accounts,
            'return_url': return_url,
        }
        return results

    voter_manager = VoterManager()
    results = voter_manager.retrieve_voter_from_voter_device_id(
        voter_device_id)
    if not positive_value_exists(results['voter_found']):
        results = {
            'status': "VALID_VOTER_MISSING",
            'success': False,
            'voter_device_id': voter_device_id,
            'twitter_handle': twitter_handle,
            'twitter_handle_found': twitter_handle_found,
            'voter_info_retrieved': voter_info_retrieved,
            'switch_accounts': switch_accounts,
            'return_url': return_url,
        }
        return results

    voter = results['voter']

    auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
    auth.set_access_token(voter.twitter_access_token,
                          voter.twitter_access_secret)

    api = tweepy.API(auth)

    try:
        tweepy_user_object = api.me()
        twitter_json = tweepy_user_object._json

        success = True
        status = 'TWITTER_SIGN_IN_REQUEST_VOTER_INFO_SUCCESSFUL'
        twitter_handle = tweepy_user_object.screen_name
        twitter_handle_found = True
        twitter_user_object_found = True
    except tweepy.RateLimitError:
        success = False
        status = 'TWITTER_SIGN_IN_REQUEST_VOTER_INFO_RATE_LIMIT_ERROR'
    except tweepy.error.TweepError as error_instance:
        success = False
        status = 'TWITTER_SIGN_IN_REQUEST_VOTER_INFO_TWEEPY_ERROR: '
        error_tuple = error_instance.args
        for error_dict in error_tuple:
            for one_error in error_dict:
                status += '[' + one_error['message'] + '] '

    if twitter_user_object_found:
        # We need to deal with these cases

        # 1) Does account already exist?
        results = voter_manager.retrieve_voter_by_twitter_id(
            tweepy_user_object.id)
        if results['voter_found'] and switch_accounts_if_needed:
            voter_found_with_twitter_id = results['voter']

            switch_accounts = True

            # Relink this voter_device_id to the original account
            voter_device_manager = VoterDeviceLinkManager()
            voter_device_link_results = voter_device_manager.retrieve_voter_device_link(
                voter_device_id)
            voter_device_link = voter_device_link_results['voter_device_link']

            update_voter_device_link_results = voter_device_manager.update_voter_device_link(
                voter_device_link, voter_found_with_twitter_id)
            if update_voter_device_link_results['voter_device_link_updated']:
                # Transfer access token and secret
                voter_found_with_twitter_id.twitter_access_token = voter.twitter_access_token
                voter_found_with_twitter_id.twitter_access_secret = voter.twitter_access_secret
                voter_found_with_twitter_id.save()

                status += "TWITTER_SIGN_IN-ALREADY_LINKED_TO_OTHER_ACCOUNT-TRANSFERRED "
                success = True
                save_user_results = voter_manager.save_twitter_user_values(
                    voter_found_with_twitter_id, tweepy_user_object)

                if save_user_results['success']:
                    voter_info_retrieved = True
                status += save_user_results['status']
            else:
                status = "TWITTER_SIGN_IN-ALREADY_LINKED_TO_OTHER_ACCOUNT-COULD_NOT_TRANSFER "
                success = False

        # 2) If account doesn't exist for this person, save
        else:
            save_user_results = voter_manager.save_twitter_user_values(
                voter, tweepy_user_object)

            if save_user_results['success']:
                voter_info_retrieved = True

    results = {
        'status': status,
        'success': success,
        'voter_device_id': voter_device_id,
        'twitter_handle': twitter_handle,
        'twitter_handle_found': twitter_handle_found,
        'voter_info_retrieved': voter_info_retrieved,
        'switch_accounts': switch_accounts,
        'return_url': return_url,
    }
    return results
Esempio n. 6
0
def social_user(backend, uid, details, user=None, *args, **kwargs):
    voter_manager = VoterManager()
    provider = backend.name
    social = backend.strategy.storage.user.get_social_auth(provider, uid)

    if backend.name == 'twitter':
        # Twitter: Check to see if we have a voter with a matching twitter_id
        local_user_matches = user and user.twitter_id == uid
    else:
        local_user_matches = user and user.email != details.get('email')
    switch_user = not local_user_matches
    if switch_user:
        # Logout the current Django user
        logout(backend.strategy.request)

        user = None

        voter_that_matches_auth = None
        voter_found_that_matches_auth = False
        if social:
            if social.user:
                if backend.name == 'twitter':
                    if social.user.twitter_id == uid:
                        voter_that_matches_auth = social.user
                        voter_found_that_matches_auth = True
                    else:
                        pass

        if not voter_found_that_matches_auth:
            # Find the voter account that actually matches this twitter_id
            results = voter_manager.retrieve_voter_by_twitter_id(uid)
            if results['voter_found']:
                voter_that_matches_auth = results['voter']
                voter_found_that_matches_auth = True

        if voter_found_that_matches_auth:
            user = voter_that_matches_auth
        else:
            # No other account matches this, so we want to save basic information in social.user
            if backend.name == 'twitter':
                if social and social.user:
                    twitter_user_dict = {
                        'id':
                        uid,
                        'profile_image_url_https':
                        kwargs['response']['profile_image_url_https'],
                        'screen_name':
                        kwargs['response']['screen_name']
                    }
                    results = voter_manager.save_twitter_user_values_from_dict(
                        social.user, twitter_user_dict)
                    if results['success']:
                        social.user = results['voter']
                        user = results['voter']

    return {
        'social': social,
        'user': user,
        'is_new': user is None,
        'switch_user': True,
        'new_association': False
    }