def authenticate(self, request=None):
     '''
     Lookup a User model based on their LinkedIn member ID. If 
     the profile exists, then they have already successfully
     merged their LinkedIn account and their Django app, so the
     User is considered authenticated and is returned.
     '''
     liapi           = LinkedInAPI(linkedin_settings.LINKEDIN_API_KEY, linkedin_settings.LINKEDIN_SECRET_KEY)
     cred_cookie     = get_credential_cookie(request)
     if not valid_cookie(cred_cookie):
         return None
     
     '''
     This is the actual "login" step, as it verifies the already 
     logged-in LinkedIn user, whose credential cookie was created after
     successfully logging in with the JSAPI.
     '''
     access_token    = liapi.exchange_bearer_token(cred_cookie)
     if not self._verify_access_token(access_token):
         return None       
     
     user            = self._lookup_user_by_token(access_token, cred_cookie['member_id'])
     if not user:
         user        = create_merged_user(access_token)
         
     if settings.DEBUG:
         print 'AUTHENTICATING USER ---> %s' % user
         
     return user
Exemple #2
0
def create_merged_user(access_token=None):
    '''
    Create a new User object and profile for the LinkedIn user profile.
    
    NOTE: The LinkedIn API does not provide access to the user email, so
    you must build in a separate means to collect email addresses from
    your users, if you require email.  Django User objects do not require
    email addresses to function.
    '''
    if not access_token:
        raise ValueError, 'You must supply a valid LinkedIn access_token with which to create the Django User'

    if settings.DEBUG:
        print 'ACCESS_TOKEN --->'
        print access_token

    new_user = None

    if settings.DEBUG:
        print 'RETRIEVING LINKEDIN PROFILE --->'
        print 'API Key: %s' % linkedin_settings.LINKEDIN_API_KEY
        print 'Secret Key: %s' % linkedin_settings.LINKEDIN_SECRET_KEY

    liapi = LinkedInAPI(linkedin_settings.LINKEDIN_API_KEY,
                        linkedin_settings.LINKEDIN_SECRET_KEY)
    liprofile = liapi.get_user_profile(
        access_token, linkedin_auth.LINKEDIN_DJANGO_MAPPED_PROFILE_FIELDS)

    if settings.DEBUG:
        print 'LINKEDIN PROFILE --->'
        print liprofile

    if liprofile and len(liprofile) > 0:
        liprofile = liprofile[0]

        if settings.DEBUG:
            print 'Creating user: %s %s' % (liprofile.first_name,
                                            liprofile.last_name)

        new_user = User.objects.create_user(
            liprofile.id, '', User.objects.make_random_password())
        new_user.first_name = getattr(liprofile, 'first_name', None)
        new_user.last_name = getattr(liprofile, 'last_name', None)
        new_user.backend = 'linkedin_auth.auth_backends.LinkedInAuthenticationBackend'

        new_user.set_unusable_password()
        new_user.save()

        merge_profile_fields(new_user, liprofile, access_token)

    return new_user
Exemple #3
0
def create_merged_user(access_token=None):
    '''
    Create a new User object and profile for the LinkedIn user profile.
    
    NOTE: The LinkedIn API does not provide access to the user email, so
    you must build in a separate means to collect email addresses from
    your users, if you require email.  Django User objects do not require
    email addresses to function.
    '''
    if not access_token:
        raise ValueError, 'You must supply a valid LinkedIn access_token with which to create the Django User'
    
    if settings.DEBUG:
        print 'ACCESS_TOKEN --->'
        print access_token
    
    new_user                = None
    
    if settings.DEBUG:
        print 'RETRIEVING LINKEDIN PROFILE --->'
        print 'API Key: %s' % linkedin_settings.LINKEDIN_API_KEY
        print 'Secret Key: %s' % linkedin_settings.LINKEDIN_SECRET_KEY
    
    liapi                   = LinkedInAPI(linkedin_settings.LINKEDIN_API_KEY, linkedin_settings.LINKEDIN_SECRET_KEY)
    liprofile               = liapi.get_user_profile(access_token, linkedin_auth.LINKEDIN_DJANGO_MAPPED_PROFILE_FIELDS)

    if settings.DEBUG:
        print 'LINKEDIN PROFILE --->'
        print liprofile
        
    if liprofile and len(liprofile) > 0:
        liprofile           = liprofile[0]
        
        if settings.DEBUG:
            print 'Creating user: %s %s' % (liprofile.first_name, liprofile.last_name)
            
        new_user            = User.objects.create_user(liprofile.id, '', User.objects.make_random_password())
        new_user.first_name = getattr(liprofile, 'first_name', None)
        new_user.last_name  = getattr(liprofile, 'last_name', None)
        new_user.backend    = 'linkedin_auth.auth_backends.LinkedInAuthenticationBackend'
        
        new_user.set_unusable_password()
        new_user.save()
        
        merge_profile_fields(new_user, liprofile, access_token)
        
    return new_user