Esempio n. 1
0
    def check_sms(cls, data):
        '''
        check sms code with mobile and code
        '''
        mobile = data.get('mobile', None)
        if not mobile:
            raise ValidationError({'mobile': ['This field is required.']})

        code = data.get('code', None)
        if not code:
            raise ValidationError({'code': ['This field is required.']})

        res = redis_conn.get(cls.gen_sms_code_key(mobile))
        if res:
            send_code = res
            if send_code and code == send_code:
                sms_token = "".join(
                    random.choice(string.ascii_letters + string.digits)
                    for _ in range(24))
                redis_conn.set(cls.gen_sms_token_key(sms_token),
                               mobile,
                               ex=settings.SMS_LIFESPAN.seconds * 10)
                redis_conn.delete(cls.gen_sms_code_key(mobile))
                return sms_token, int(
                    time.time()) + settings.SMS_LIFESPAN.seconds * 10
        raise ValidationError({'code': ['invalid']})
Esempio n. 2
0
 def check_email_token(cls, email_token):
     '''
     校验email_token
     '''
     key = cls.gen_email_token_key(email_token)
     res = redis_conn.get(key)
     if res:
         return {'email': res}
     raise ValidationError({'email_token': ['invalid']})
Esempio n. 3
0
 def check_sms_token(cls, sms_token):
     '''
     通过短信验证后,拿到的身份凭证。用于临时性的二次验证。
     有效期长于之前的验证码,一般短于账号密码登录后的token。
     '''
     key = cls.gen_sms_token_key(sms_token)
     res = redis_conn.get(key)
     if res:
         return {'mobile': res}  # mobile
     raise ValidationError({'sms_token': ['invalid']})
Esempio n. 4
0
 def check_email_token(cls, email_token):
     '''
     校验email_token
     '''
     key = cls.gen_email_token_key(email_token)
     res = redis_conn.get(key)
     if res:
         email = res
         user = User.valid_objects.filter(private_email=email).first()
         if user:
             return {'email': email, 'name': user.name, 'username': user.username}
     raise ValidationError({'email_token': ['invalid']})