Esempio n. 1
0
    def authenticate(self, oauth_profile, username, request):
        source = oauth_profile['source']
        source_uid = oauth_profile['source_uid']
        device_uid = oauth_profile['device_uid']
        try:
            user = self.user_document.objects.get(username=username)
            if source not in user.accounts:
                user.accounts[source] = oauth_profile
            if device_uid not in user.device_uids:
                user.device_uids.append(device_uid)
            user.record_login_history()
        except self.user_document.DoesNotExist:
            user_document = self.user_document
            user = user_document(username=username, source=source, source_uid=source_uid)
            import datetime
            from people.models import User
            user.create_time = datetime.datetime.utcnow()
            user.accounts[source] = oauth_profile
            user.authenticate_by_oauth()
            device_users = User.objects(id=device_uid)
            device_user = None
            if device_users:
                device_user = device_users[0]
                user.device_uids = [device_uid]
                user.feature = device_user.feature
            user.record_login_history()
            if device_user:
                user.combine_user_log(device_user)
            else:
                user.init_warm_data()

        return user
Esempio n. 2
0
 def generate_verification_code(cls, receiver, type=TYPE_PHONE):
     user = User.objects(phone=receiver)
     if user:
         raise IdentityHasBinded()
     now = datetime.utcnow()
     verified_receivers = cls.objects(receiver=receiver, created_at__gt=now - timedelta(minutes=1))
     if verified_receivers:
         raise VerifyOverFrequency()
     now = datetime.utcnow()
     expires_in = timedelta(hours=3)
     expires_at = now + expires_in
     code  = random.randint(100000, 999999)
     vcode = cls.objects.create(receiver=receiver, code=code, created_at=now, expires_at=expires_at)
     return vcode.code