コード例 #1
0
ファイル: auth_backends.py プロジェクト: oaleeapp/EguoWorld
    def authenticate(self, openid_key, request, provider, email=None):
        try:
            assoc = UserAssociation.objects.get(openid_key=openid_key)
            user = User.objects.get(pk=assoc.user.pk)
            if user.is_active:
                return user
            else:
                return
        except (UserAssociation.DoesNotExist, User.DoesNotExist):
            #fetch if openid provider provides any simple registration fields
            nickname = None
            if request.openid and request.openid.sreg:
                if email is None:
                    email = request.openid.sreg.get('email')
                nickname = request.openid.sreg.get('nickname')
            elif request.openid and request.openid.ax:
                if email is None:
                    email = request.openid.ax.get('email')
                nickname = request.openid.ax.get('nickname')
            if nickname is None:
                nickname = ''.join([
                    random.choice('abcdefghijklmnopqrstuvwxyz')
                    for i in xrange(10)
                ])
            if email is None:
                valid_username = False
                email = '%s@%s.%s.com' % (nickname, provider,
                                          settings.SITE_NAME)
            else:
                valid_username = True
            existing_users = User.objects.filter(username=nickname).count()
            if existing_users > 0:
                index = 0
                username = '******' % (nickname, index)
                while existing_users > 0:
                    username = '******' % (nickname, index)
                    existing_users = User.objects.filter(
                        username=username).count()
                    index += 1
                user = User.objects.create_user(username, email)
            else:
                user = User.objects.create_user(nickname, email)
            user.save()

            #create openid association
            assoc = UserAssociation()
            assoc.openid_key = openid_key
            assoc.user = user
            if email:
                assoc.email = email
            if nickname:
                assoc.nickname = nickname
            if valid_username:
                assoc.is_username_valid = True
            assoc.save()

            #Create AuthMeta
            auth_meta = AuthMeta(user=user, provider=provider)
            auth_meta.save()
            return user
コード例 #2
0
ファイル: backends.py プロジェクト: sokokame/langolab
    def authenticate(self, openid_key, request, provider):
        try:
            assoc = UserAssociation.objects.get(openid_key=openid_key)
            if assoc.user.is_active:
                return assoc.user
            else:
                return
        except UserAssociation.DoesNotExist:
            #fetch if openid provider provides any simple registration fields
            nickname = None
            email = None
            if request.openid and request.openid.sreg:
                email = request.openid.sreg.get('email')
                nickname = request.openid.sreg.get('nickname')
            elif request.openid and request.openid.ax:
                if provider in ('Google', 'Yahoo'):
                    email = request.openid.ax.get('http://axschema.org/contact/email')
                    email = email.pop()
                else:
                    email = request.openid.ax.get('email')                      
                    nickname = request.openid.ax.get('nickname')

            if nickname is None :
                if email:
                    nickname = email.split('@')[0]
                else:
                    nickname =  ''.join([random.choice('abcdefghijklmnopqrstuvwxyz') for i in xrange(10)])
            if email is None :
                valid_username = False
                email =  None #'*****@*****.**'%(nickname)
            else:
                valid_username = True
            name_count = User.objects.filter(username__startswith = nickname).count()

            if name_count:
                username = '******'%(nickname, name_count + 1)
                user = User.objects.create_user(username,email or '')
            else:
                user = User.objects.create_user(nickname,email or '')
            user.save()

            #create openid association
            assoc = UserAssociation()
            assoc.openid_key = openid_key
            assoc.user = user#AuthUser.objects.get(pk=user.pk)
            if email:
                assoc.email = email
            if nickname:
                assoc.nickname = nickname
            if valid_username:
                assoc.is_username_valid = True
            assoc.save()

            #Create AuthMeta
            auth_meta = AuthMeta(user = user, provider = provider)
            auth_meta.save()
            return user
コード例 #3
0
    def authenticate(self, openid_key, request, provider, user=None):
        try:
            assoc = UserAssociation.objects.get(openid_key = openid_key)
            return assoc.user
        except UserAssociation.DoesNotExist:
            #fetch if openid provider provides any simple registration fields
            nickname = None
            email = None
            if request.openid and request.openid.sreg:
                email = request.openid.sreg.get('email')
                nickname = request.openid.sreg.get('nickname')
            elif request.openid and request.openid.ax:
                email = request.openid.ax.get('http://axschema.org/contact/email')[0]
                try:
                      nickname = request.openid.ax.get('nickname')#should be replaced by correct schema
                except:
                      pass
            if nickname is None :
                nickname =  ''.join([random.choice('abcdefghijklmnopqrstuvwxyz') for i in xrange(10)])
            
            name_count = User.objects.filter(username__startswith = nickname).count()
            if name_count:
                username = '******'.format(nickname, name_count + 1)
            else:
                username = '******'.format(nickname)
                
            if email is None :
                valid_username = False
                email =  "{0}@socialauth".format(username)
            else:
                valid_username = True

            if not user:
                user = User.objects.create_user(username, email or '')
                user.save()
    
            #create openid association
            assoc = UserAssociation()
            assoc.openid_key = openid_key
            assoc.user = user
            if email:
                assoc.email = email
            if nickname:
                assoc.nickname = nickname
            if valid_username:
                assoc.is_username_valid = True
            assoc.save()
            
            #Create AuthMeta
            auth_meta = AuthMeta(user=user, provider=provider, provider_model='OpenidProfile', provider_id=assoc.pk)
            auth_meta.save()
            return user
コード例 #4
0
    def authenticate(self, openid_key, request, provider):
        try:
            assoc = UserAssociation.objects.get(openid_key=openid_key)
            return assoc.user
        except UserAssociation.DoesNotExist:
            #fetch if openid provider provides any simple registration fields
            nickname = None
            email = None
            print "dfsdfsdfsdfsfsdfsdf"
            if request.openid and request.openid.sreg:
                email = request.openid.sreg.get('email')
                nickname = request.openid.sreg.get('nickname')
            elif request.openid and request.openid.ax:
                email = request.openid.ax.__dict__.get('data').values()[0][0]
                nickname = email.split('@')[
                    0]  #request.openid.ax.get('nickname')
            if nickname is None:
                nickname = ''.join([
                    random.choice('abcdefghijklmnopqrstuvwxyz')
                    for i in xrange(10)
                ])
            if email is None:
                valid_username = False
                email = '%s@%s.%s.com' % (nickname, provider,
                                          settings.SITE_NAME)
            else:
                valid_username = True
            name_count = User.objects.filter(
                username__startswith=nickname).count()
            if name_count:
                username = '******' % (nickname, name_count + 1)
                user = User.objects.create_user(username, email)
            else:
                user = User.objects.create_user(nickname, email)
            user.save()

            #create openid association
            assoc = UserAssociation()
            assoc.openid_key = openid_key
            assoc.user = user
            if email:
                assoc.email = email
            if nickname:
                assoc.nickname = nickname
            if valid_username:
                assoc.is_username_valid = True
            assoc.save()

            #Create AuthMeta
            auth_meta = AuthMeta(user=user, provider=provider)
            auth_meta.save()
            return user
コード例 #5
0
    def authenticate(self, openid_key, request, provider):
        try:
            assoc = UserAssociation.objects.get(openid_key=openid_key)
            return assoc.user
        except UserAssociation.DoesNotExist:
            # fetch if openid provider provides any simple registration fields
            nickname = None
            email = None
            if request.openid and request.openid.sreg:
                email = request.openid.sreg.get("email")
                nickname = request.openid.sreg.get("nickname")
            elif request.openid and request.openid.ax:
                email = request.openid.ax.get("http://axschema.org/contact/email")[0]
                try:
                    nickname = request.openid.ax.get("nickname")  # should be replaced by correct schema
                except:
                    pass
            if nickname is None:
                nickname = "".join([random.choice("abcdefghijklmnopqrstuvwxyz") for i in xrange(10)])
            if email is None:
                valid_username = False
                email = None  #'*****@*****.**'%(nickname)
            else:
                valid_username = True
            name_count = User.objects.filter(username__startswith=nickname).count()
            if name_count:
                username = "******" % (nickname, name_count + 1)
            try:
                user = User.objects.get(email=email)
            except:
                user = User.objects.create_user(nickname, email or "")
                user.save()

            # create openid association
            assoc = UserAssociation()
            assoc.openid_key = openid_key
            assoc.user = user
            if email:
                assoc.email = email
            if nickname:
                assoc.nickname = nickname
            if valid_username:
                assoc.is_username_valid = True
            assoc.save()

            # Create AuthMeta
            auth_meta = AuthMeta(user=user, provider=provider)
            auth_meta.save()
            return user
コード例 #6
0
ファイル: backends.py プロジェクト: itsbenweeks/Amara
 def _lookup_gmail_assocation(self, openid_key, request, provider):
     email = request.openid.ax.get('http://axschema.org/contact/email')[-1]
     try:
         rv = UserAssociation.objects.filter(email=email)[0]
     except IndexError:
         raise UserAssociation.DoesNotExist()
     if rv.openid_key != openid_key:
         logger.error("Gmail OpenID key different for user",
                      extra={
                          'email': email,
                          'original OpenID key': rv.openid_key,
                          'new OpenID key': openid_key,
                      })
     return rv
コード例 #7
0
ファイル: auth_backends.py プロジェクト: exezaid/TipsForLinux
 def authenticate(self, openid_key, request, provider):
     try:
         assoc = UserAssociation.objects.get(openid_key = openid_key)
         return assoc.user
     except UserAssociation.DoesNotExist:
         #fetch if openid provider provides any simple registration fields
         nickname = None
         email = None
         print "dfsdfsdfsdfsfsdfsdf"
         if request.openid and request.openid.sreg:
             email = request.openid.sreg.get('email')
             nickname = request.openid.sreg.get('nickname')
         elif request.openid and request.openid.ax:
             email = request.openid.ax.__dict__.get('data').values()[0][0]
             nickname = email.split('@')[0] #request.openid.ax.get('nickname')
         if nickname is None :
             nickname =  ''.join([random.choice('abcdefghijklmnopqrstuvwxyz') for i in xrange(10)])
         if email is None :
             valid_username = False
             email =  '%s@%s.%s.com'%(nickname, provider, settings.SITE_NAME)
         else:
             valid_username = True
         name_count = User.objects.filter(username__startswith = nickname).count()
         if name_count:
             username = '******'%(nickname, name_count + 1)
             user = User.objects.create_user(username,email)
         else:
             user = User.objects.create_user(nickname,email)
         user.save()
 
         #create openid association
         assoc = UserAssociation()
         assoc.openid_key = openid_key
         assoc.user = user
         if email:
             assoc.email = email
         if nickname:
             assoc.nickname = nickname
         if valid_username:
             assoc.is_username_valid = True
         assoc.save()
         
         #Create AuthMeta
         auth_meta = AuthMeta(user = user, provider = provider)
         auth_meta.save()
         return user
コード例 #8
0
 def authenticate(self, openid_key, request, provider):
     try:
         assoc = UserAssociation.objects.get(openid_key = openid_key)
         return assoc.user
     except UserAssociation.DoesNotExist:
         #fetch if openid provider provides any simple registration fields
         nickname = None
         email = None
         # print 'request.openid: %s' % request.openid
         # print 'request.openid.sreg: %s' % request.openid.sreg
         if request.openid and request.openid.sreg:
             email = request.openid.sreg.get('email')
             nickname = request.openid.sreg.get('nickname')
         if nickname is None :
             nickname =  ''.join([random.choice('abcdefghijklmnopqrstuvwxyz') for i in xrange(10)])
         if email is None :
             email =  '%s@%s.%s.com'%(nickname, provider, settings.SITE_NAME)
         name_count = User.objects.filter(username__startswith = nickname).count()
         # print 'nickname: %s, email: %s, name_count: %s' % (nickname, email, name_count)
         if name_count:
             username = '******'%(nickname, name_count + 1)
             user = User.objects.create_user(username,email)
         else:
             user = User.objects.create_user(nickname,email)
         user.save()
 
         #create openid association
         assoc = UserAssociation()
         assoc.openid_key = openid_key
         assoc.user = user
         if email:
             assoc.email = email
         if nickname:
             assoc.nickname = nickname
         assoc.save()
         
         #Create AuthMeta
         auth_meta = AuthMeta(user = user, provider = provider)
         auth_meta.save()
         
         return user
コード例 #9
0
    def authenticate(self, openid_key, request, provider, user=None):
        try:
            assoc = UserAssociation.objects.get(openid_key=openid_key)
            return assoc.user
        except UserAssociation.DoesNotExist:
            # fetch if openid provider provides any simple registration fields
            nickname = None
            email = None
            first_name = None
            last_name = None
            if request.openid and request.openid.sreg:
                email = request.openid.sreg.get("email")
                nickname = request.openid.sreg.get("nickname")
                first_name, last_name = request.openid.sreg.get("fullname", "").split(" ", 1)
            elif request.openid and request.openid.ax:
                email = request.openid.ax.get("http://axschema.org/contact/email")[0]
                try:
                    nickname = request.openid.ax.get("nickname")  # should be replaced by correct schema
                except:
                    pass
                try:
                    first_name = request.openid.ax.get("http://axschema.org/namePerson/first").pop()
                except:
                    pass
                try:
                    last_name = request.openid.ax.get("http://axschema.org/namePerson/last").pop()
                except:
                    pass
            if nickname is None:
                nickname = "".join([random.choice("abcdefghijklmnopqrstuvwxyz") for i in xrange(10)])

            name_count = User.objects.filter(username__startswith=nickname).count()
            if name_count:
                username = "******" % (nickname, name_count + 1)
            else:
                username = "******" % (nickname)

            if email is None:
                valid_username = False
                email = "%s@socialauth" % (username)
            else:
                valid_username = True

            if not user:
                user = User.objects.create_user(username, email or "")
                if first_name is not None:
                    user.first_name = first_name
                if last_name is not None:
                    user.last_name = last_name
                user.save()

            # create openid association
            assoc = UserAssociation()
            assoc.openid_key = openid_key
            assoc.user = user
            if email:
                assoc.email = email
            if nickname:
                assoc.nickname = nickname
            if valid_username:
                assoc.is_username_valid = True
            assoc.save()

            # Create AuthMeta
            # auth_meta =
            #             AuthMeta(user=user,
            #                        provider=provider,
            #                        provider_model='OpenidProfile',
            #                        provider_id=assoc.pk)
            auth_meta = AuthMeta(user=user, provider=provider)
            auth_meta.save()
            return user
コード例 #10
0
    def authenticate(self, openid_key, request, provider, user=None):
        try:
            assoc = UserAssociation.objects.get(openid_key=openid_key)
            return assoc.user
        except UserAssociation.DoesNotExist:
            #fetch if openid provider provides any simple registration fields
            nickname = None
            email = None
            firstname = None
            lastname = None
            
            if request.openid and request.openid.sreg:
                email = request.openid.sreg.get('email')
                nickname = request.openid.sreg.get('nickname')
                firstname, lastname = (request.openid.sreg
                                       .get('fullname', ' ')
                                       .split(' ', 1))
            elif request.openid and request.openid.ax:
                email = \
                    (request.openid.ax
                     .getSingle('http://axschema.org/contact/email'))
                if 'google' in provider:
                    ax_schema = OPENID_AX_PROVIDER_MAP['Google']
                    firstname = (request.openid.ax
                                 .getSingle(ax_schema['firstname']))
                    lastname = (request.openid.ax
                                .getSingle(ax_schema['lastname']))
                    nickname = email.split('@')[0]
                else:
                    ax_schema = OPENID_AX_PROVIDER_MAP['Default']
                    try:
                         #should be replaced by correct schema
                        nickname = (request.openid.ax
                                    .getSingle(ax_schema['nickname']))
                        (firstname, 
                            lastname) = (request.openid.ax
                                         .getSingle(ax_schema['fullname'])
                                         .split(' ', 1))
                    except:
                        pass

            if nickname is None :
                nickname =  ''.join(
                                    [random
                                     .choice('abcdefghijklmnopqrstuvwxyz')
                                     for i in xrange(10)])
            
            name_count = (User.objects
                          .filter(username__startswith=nickname)
                          .count())
            if name_count:
                username = '******' % (nickname, name_count + 1)
            else:
                username = '******' % (nickname)
                
            if email is None :
                valid_username = False
                email =  "%s@socialauth" % (username)
            else:
                valid_username = True
            
            if not user:
                user = User.objects.create_user(username, email)
                
            user.first_name = firstname
            user.last_name = lastname
            user.save()
    
            #create openid association
            assoc = UserAssociation()
            assoc.openid_key = openid_key
            assoc.user = user
            if email:
                assoc.email = email
            if nickname:
                assoc.nickname = nickname
            if valid_username:
                assoc.is_username_valid = True
            assoc.save()
            
            #Create AuthMeta
            # auth_meta = 
            #             AuthMeta(user=user, 
            #                        provider=provider, 
            #                        provider_model='OpenidProfile', 
            #                        provider_id=assoc.pk)
            auth_meta = AuthMeta(user=user, provider=provider)
            auth_meta.save()
            return user
コード例 #11
0
    def authenticate(self, openid_key, request, provider, user=None):
        try:
            assoc = UserAssociation.objects.get(openid_key=openid_key)
            return assoc.user
        except UserAssociation.DoesNotExist:
            #fetch if openid provider provides any simple registration fields
            nickname = None
            email = None
            firstname = None
            lastname = None

            if request.openid and request.openid.sreg:
                email = request.openid.sreg.get('email')
                nickname = request.openid.sreg.get('nickname')
                firstname, lastname = (request.openid.sreg.get(
                    'fullname', ' ').split(' ', 1))
            elif request.openid and request.openid.ax:
                email = \
                    (request.openid.ax
                     .getSingle('http://axschema.org/contact/email'))
                if 'google' in provider:
                    ax_schema = OPENID_AX_PROVIDER_MAP['Google']
                    firstname = (request.openid.ax.getSingle(
                        ax_schema['firstname']))
                    lastname = (request.openid.ax.getSingle(
                        ax_schema['lastname']))
                    nickname = email.split('@')[0]
                else:
                    ax_schema = OPENID_AX_PROVIDER_MAP['Default']
                    try:
                        #should be replaced by correct schema
                        nickname = (request.openid.ax.getSingle(
                            ax_schema['nickname']))
                        (firstname, lastname) = (request.openid.ax.getSingle(
                            ax_schema['fullname']).split(' ', 1))
                    except:
                        pass

            if nickname is None:
                nickname = ''.join([
                    random.choice('abcdefghijklmnopqrstuvwxyz')
                    for i in xrange(10)
                ])

            name_count = (User.objects.filter(
                username__startswith=nickname).count())
            if name_count:
                username = '******' % (nickname, name_count + 1)
            else:
                username = '******' % (nickname)

            if email is None:
                valid_username = False
                email = "%s@socialauth" % (username)
            else:
                valid_username = True

            if not user:
                user = User.objects.create_user(username, email)

            user.first_name = firstname
            user.last_name = lastname
            user.save()

            #create openid association
            assoc = UserAssociation()
            assoc.openid_key = openid_key
            assoc.user = user
            if email:
                assoc.email = email
            if nickname:
                assoc.nickname = nickname
            if valid_username:
                assoc.is_username_valid = True
            assoc.save()

            #Create AuthMeta
            # auth_meta =
            #             AuthMeta(user=user,
            #                        provider=provider,
            #                        provider_model='OpenidProfile',
            #                        provider_id=assoc.pk)
            auth_meta = AuthMeta(user=user, provider=provider)
            auth_meta.save()
            return user
コード例 #12
0
    def authenticate(self, openid_key, request, provider):
        try:
            assoc = UserAssociation.objects.get(openid_key=openid_key)
            if assoc.user.is_active:
                return assoc.user
            else:
                return
        except UserAssociation.DoesNotExist:
            #fetch if openid provider provides any simple registration fields
            nickname = None
            email = None
            if request.openid and request.openid.sreg:
                email = request.openid.sreg.get('email')
                nickname = request.openid.sreg.get('nickname')
            elif request.openid and request.openid.ax:
                if provider in ('Google', 'Yahoo'):
                    email = request.openid.ax.get(
                        'http://axschema.org/contact/email')
                    email = email.pop()
                else:
                    try:
                        email = request.openid.ax.get('email')
                    except KeyError:
                        pass

                    try:
                        nickname = request.openid.ax.get('nickname')
                    except KeyError:
                        pass

            if nickname is None:
                if email:
                    nickname = email.split('@')[0]
                else:
                    nickname = ''.join([
                        random.choice('abcdefghijklmnopqrstuvwxyz')
                        for i in xrange(10)
                    ])
            if email is None:
                valid_username = False
                email = None  #'*****@*****.**'%(nickname)
            else:
                valid_username = True
            name_count = AuthUser.objects.filter(
                username__startswith=nickname).count()

            if name_count:
                username = '******' % (nickname, name_count + 1)
                user = User.objects.create_user(username, email or '')
            else:
                user = User.objects.create_user(nickname, email or '')
            user.save()

            #create openid association
            assoc = UserAssociation()
            assoc.openid_key = openid_key
            assoc.user = user  #AuthUser.objects.get(pk=user.pk)
            if email:
                assoc.email = email
            if nickname:
                assoc.nickname = nickname
            if valid_username:
                assoc.is_username_valid = True
            assoc.save()

            #Create AuthMeta
            auth_meta = AuthMeta(user=user, provider=provider)
            auth_meta.save()
            return user
コード例 #13
0
    def authenticate(self, openid_key, request, provider, user=None):
        logger.info("request.GET: %s", request.GET)
        logger.info("request.POST: %s", request.POST)
        logger.info("------------------------------------------ Authenticating against OpenID")
        logger.info("openid_key: %s", openid_key)
        logger.info("provider: %s", provider)
        try:
            assoc = OpenidProfile.objects.get(openid_key=openid_key)
            logger.info("Found a OpenidProfile")
            logger.info("assoc.openid_key: %s", assoc.openid_key)
            logger.info("assoc.user.username: %s", assoc.user.username)
            logger.info("assoc.nickname: %s", assoc.nickname)
            logger.info("assoc.is_username_valid: %s", assoc.is_username_valid)
            logger.info("assoc.email: %s", assoc.email)
            if assoc.email.endswith("@socialauth") and request.openid.ax is not None:
                assoc.email = request.openid.ax.getSingle("http://axschema.org/contact/email", None)
                logger.info("Updating existing OpenidProfile with correct openid email")
                assoc.save()
            return assoc.user
        except OpenidProfile.DoesNotExist:
            # fetch if openid provider provides any simple registration fields
            logger.info("Creating a OpenidProfile")
            nickname = None
            email = None
            if request.openid and request.openid.sreg:
                email = request.openid.sreg.get("email")
                nickname = request.openid.sreg.get("nickname")
            elif request.openid and request.openid.ax:
                email = request.openid.ax.getSingle("http://axschema.org/contact/email", None)

            if nickname is None:
                nickname = "".join([random.choice("abcdefghijklmnopqrstuvwxyz") for i in xrange(10)])

            name_count = User.objects.filter(username__startswith=nickname).count()
            if name_count:
                username = "******".format(nickname, name_count + 1)
            else:
                username = "******".format(nickname)

            if email is None:
                valid_username = False
                email = "{0}@socialauth".format(username)
            else:
                valid_username = True
            if not user:
                user = User.objects.create_user(username, email or "")
                user.set_unusable_password()
                user.save()

            # create openid association
            assoc = OpenidProfile()
            assoc.openid_key = openid_key
            assoc.user = user
            if provider == "Google":
                assoc.needs_google_crossdomain_merge = True
            if email:
                assoc.email = email
            if nickname:
                assoc.nickname = nickname
            if valid_username:
                assoc.is_username_valid = True
            assoc.save()

            logger.info("assoc.openid_key: %s", assoc.openid_key)
            logger.info("assoc.user.username: %s", assoc.user.username)
            logger.info("assoc.nickname: %s", assoc.nickname)
            logger.info("assoc.is_username_valid: %s", assoc.is_username_valid)
            logger.info("assoc.email: %s", assoc.email)

            # Create AuthMeta
            auth_meta = AuthMeta(user=user, provider=provider, provider_model="OpenidProfile", provider_id=assoc.pk)
            auth_meta.save()
            return user