def authenticate(self, request=None, identifier=None, openid=None, provider=None):
        if request is None:
            return None
        if not settings.ENABLE_OPENID_REGISTRATION:
            return None
        if not identifier or not openid or not provider:
            return None
        if not isinstance(openid, OpenID):
            return None
        try:
            account = OpenIdAccount.objects.get(identifier = identifier)
            return account.user
        except OpenIdAccount.DoesNotExist:
            #fetch if openid provider provides any simple registration fields
            nickname = None
            email = None
            if openid and openid.sreg:
                email = openid.sreg.get('email')
                nickname = openid.sreg.get('nickname')
            elif openid and openid.ax:
                email = openid.ax.get('email')
                nickname = openid.ax.get('nickname')
            generate_username = False
            if nickname is None:
                generate_username = True
                username =  ''.join([random.choice('abcdefghijklmnopqrstuvwxyz') for i in xrange(10)])
            else:
                username = nickname

            cnt = 1
            base_username = username
            while User.objects.filter(username = username).count():
                username = "******" % (base_username, cnt)
                cnt += 1
            user = User.objects.create_user(username, email or '')
            user.save()
    
            #create openid account
            account = OpenIdAccount()
            account.identifier = identifier
            account.user = user
            account.save()
            
            if generate_username:
                username = "******" % account.id
                while User.objects.filter(username = username).count():
                    username += random.choice('abcdefghijklmnopqrstuvwxyz')
                user.username = username
                user.save()
            
            #Create AuthMeta
            auth_meta = AuthMeta(user = user, provider = provider)
            auth_meta.save()
            
            signals.user_registered.send(sender=self.__class__,
                                         user=user,
                                         request=request)

            return user
 def authenticate(self, request=None, twitter_access_token=None):
     '''authenticates the token by requesting user information from twitter
     '''
     if request is None:
         return None
     if not settings.ENABLE_TWITTER_REGISTRATION:
         return None
     if not twitter_access_token:
         return None
     twitter = oauthtwitter.OAuthApi(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET, twitter_access_token)
     try:
         userinfo = twitter.GetUserInfo()
     except:
         # If we cannot get the user information, user cannot be authenticated
         raise
     
     screen_name = userinfo.screen_name
     twitter_id = userinfo.id
     try:
         account = TwitterAccount.objects.get(twitter_id = twitter_id)
         user = account.user
         return user
     except TwitterAccount.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
         user = User(username =  username)
         name_data = userinfo.name.split()
         try:
             first_name, last_name = name_data[0], ' '.join(name_data[1:])
         except IndexError:
             first_name, last_name =  screen_name, ''
         user.first_name, user.last_name = first_name, last_name
         #user.email = '*****@*****.**'%(userinfo.screen_name)
         user.save()
         account = TwitterAccount(user=user,
                           twitter_id=twitter_id,
                           screen_name=screen_name)
         # userprofile.access_token = access_token.key
         account.save()
         auth_meta = AuthMeta(user=user, provider='Twitter')
         auth_meta.portrait = userinfo.profile_image_url
         auth_meta.avatar = userinfo.profile_image_url
         auth_meta.location = userinfo.location
         auth_meta.save()
         signals.user_registered.send(sender=self.__class__,
                                      user=user,
                                      request=request)
         return user
    def authenticate(self,
                     request=None,
                     identifier=None,
                     openid=None,
                     provider=None):
        if request is None:
            return None
        if not settings.ENABLE_OPENID_REGISTRATION:
            return None
        if not identifier or not openid or not provider:
            return None
        if not isinstance(openid, OpenID):
            return None
        try:
            account = OpenIdAccount.objects.get(identifier=identifier)
            return account.user
        except OpenIdAccount.DoesNotExist:
            #fetch if openid provider provides any simple registration fields
            nickname = None
            email = None
            if openid and openid.sreg:
                email = openid.sreg.get('email')
                nickname = openid.sreg.get('nickname')
            elif openid and openid.ax:
                email = openid.ax.get('email')
                nickname = openid.ax.get('nickname')
            generate_username = False
            if nickname is None:
                generate_username = True
                username = ''.join([
                    random.choice('abcdefghijklmnopqrstuvwxyz')
                    for i in xrange(10)
                ])
            else:
                username = nickname

            cnt = 1
            base_username = username
            while User.objects.filter(username=username).count():
                username = "******" % (base_username, cnt)
                cnt += 1
            user = User.objects.create_user(username, email or '')
            user.save()

            #create openid account
            account = OpenIdAccount()
            account.identifier = identifier
            account.user = user
            account.save()

            if generate_username:
                username = "******" % account.id
                while User.objects.filter(username=username).count():
                    username += random.choice('abcdefghijklmnopqrstuvwxyz')
                user.username = username
                user.save()

            #Create AuthMeta
            auth_meta = AuthMeta(user=user, provider=provider)
            auth_meta.save()

            signals.user_registered.send(sender=self.__class__,
                                         user=user,
                                         request=request)

            return user
Esempio n. 4
0
    def authenticate(self, request = None):
        if not request:
            return None
        
        if not ENABLE_FACEBOOK_REGISTRATION:
            return None
        fb_user = getFacebookUserId(request)
        
        if not fb_user:
            return None
        
 
        try:
            account = FacebookAccount.objects.get(facebook_id = str(fb_user))
            return account.user
        except FacebookAccount.DoesNotExist:
            fb_data = getFacebookUserInfo(fb_user)
                
            if not fb_data:
                return None
 
            username = fb_data.get('name')
            generate_username = False
            
            if username is None:
                generate_username = True
                username =  ''.join([random.choice('abcdefghijklmnopqrstuvwxyz') for i in xrange(10)])

            cnt = 1
            base_username = username
            while User.objects.filter(username = username).count():
                username = "******" % (base_username, cnt)
                cnt += 1
                
            user = User.objects.create(username = username)
            user.first_name = fb_data['first_name']
            user.last_name = fb_data['last_name']
            user.save()
            account = FacebookAccount(facebook_id = str(fb_data['uid']), user = user)
            account.save()
            
            if generate_username:
                username = "******" % account.id
                while User.objects.filter(username = username).count():
                    username += random.choice('abcdefghijklmnopqrstuvwxyz')
                user.username = username
                user.save()
            
            auth_meta = AuthMeta(user=user, provider='Facebook')
            auth_meta.portrait = fb_data['pic_big']
            auth_meta.avatar = fb_data['pic_small']
            location = fb_data['current_location'] or fb_data['hometown_location']
            if location:
                auth_meta.location = '%(city)s, %(state)s, %(country)s'%location
            auth_meta.save()
            
            signals.user_registered.send(sender=self.__class__,
                                         user=user,
                                         request=request)
            return user
        
        return None