Exemplo n.º 1
0
    def validate(self, attrs):
        access_token = attrs['access_token']
        openid = OAuthQQ.check_save_user_token(access_token)
        if not openid:
            raise serializers.ValidationError("无效的access_token")

        attrs['openid'] = openid

        mobile = attrs['mobile']
        sms_code = attrs['sms_code']
        redis_conn = get_redis_connection('verify_codes')
        real_sms_code = redis_conn.get('sms_%s' % mobile)
        if real_sms_code.decode() != sms_code:
            raise serializers.ValidationError('短信验证码错误')

        password = attrs['password']
        try:
            user = User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            pass
        else:
            if not user.check_password(password):
                raise serializers.ValidationError('密码错误')
            attrs['user'] = user

        return attrs
Exemplo n.º 2
0
    def validate(self, data):
        # 检验access_token
        access_token = data['access_token']
        openid = OAuthQQ.check_save_user_token(access_token)
        if not openid:
            raise serializers.ValidationError('无效的access_token')

        data['openid'] = openid

        # 检验短信验证码
        mobile = data['mobile']
        sms_code = data['sms_code']
        redis_conn = get_redis_connection('verify_codes')
        real_sms_code = redis_conn.get('sms_%s' % mobile)
        if real_sms_code.decode() != sms_code:
            raise serializers.ValidationError('短信验证码错误')

        # 如果用户存在
        try:
            user = User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            pass
        else:
            password = data['password']
            if not user.check_password(password):
                raise serializers.ValidationError('密码错误')
            data['user'] = user
        return data
Exemplo n.º 3
0
    def validate(self, attrs):
        """access_token是否有效,短信验证码是否正确"""
        # access_token是否有效
        access_token = attrs['access_token']

        openid = OAuthQQ.check_save_user_token(access_token)

        if openid is None:
            raise serializers.ValidationError('无效的access_token')

        attrs['openid'] = openid

        # 短信验证码是否正确
        # 获取手机号
        mobile = attrs['mobile']

        # 从redis中获取真实的短信验证码内容
        redis_conn = get_redis_connection('verify_codes')
        real_sms_code = redis_conn.get('sms_%s' % mobile)  # bytes

        if not real_sms_code:
            raise serializers.ValidationError('短信验证码已过期')

        # 对比短信验证码
        sms_code = attrs['sms_code']  # str

        # bytes->str
        real_sms_code = real_sms_code.decode()
        if real_sms_code != sms_code:
            raise serializers.ValidationError('短信验证码错误')

        # 如果`mobile`已注册,校验密码是否正确
        try:
            user = User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            # 未注册,不校验
            user = None
        else:
            # 已注册,校验密码
            password = attrs['password']
            if not user.check_password(password):
                raise serializers.ValidationError('用户密码错误')

        # 给attrs中添加user数据,以便在保存绑定QQ登录用户的数据直接使用
        attrs['user'] = user

        return attrs
Exemplo n.º 4
0
    def validate(self, attrs):
        """短信验证码正确性,access_token是否有效"""
        # access_token是否有效
        access_token = attrs['access_token']

        openid = OAuthQQ.check_save_user_token(access_token)

        if openid is None:
            raise serializers.ValidationError('无效的access_token')

        attrs['openid'] = openid

        # 短信验证码是否正确
        mobile = attrs['mobile']

        # 从redis中获取真实的短信验证码文本
        redis_conn = get_redis_connection('verify_codes')
        # bytes
        real_sms_code = redis_conn.get('sms_%s' % mobile)  # None

        if real_sms_code is None:
            raise serializers.ValidationError('短信验证码已过期')

        # 对比短信验证码
        sms_code = attrs['sms_code']  # str

        if sms_code != real_sms_code.decode():
            raise serializers.ValidationError('短信验证码错误')

        # 如果`mobile`已注册,需要检验密码是否正确
        try:
            user = User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            # 用户未注册
            user = None
        else:
            # 已注册,校验对应用户的密码
            password = attrs['password']
            if not user.check_password(password):
                raise serializers.ValidationError('密码错误')

        # 向attrs字典中加入user,以便在进行绑定时直接使用
        attrs['user'] = user

        return attrs
Exemplo n.º 5
0
    def validate(self, attrs):
        # access_token是否有效
        access_token = attrs['access_token'] # 加密的openid

        openid = OAuthQQ.check_save_user_token(access_token)

        if openid is None:
            # 解密失败
            raise serializers.ValidationError('无效的access_token')

        attrs['openid'] = openid

        # 短信验证码是否正确
        # 从redis中获取真实的短信验证码内容
        redis_conn = get_redis_connection('verify_codes')

        mobile = attrs['mobile']
        real_sms_code = redis_conn.get('sms_%s' % mobile)  # bytes

        if real_sms_code is None:
            raise serializers.ValidationError('短信验证码已过期')

        # 获取客户端传递短信验证码内容
        sms_code = attrs['sms_code']  # str

        # 对比短信验证码内容
        if real_sms_code.decode() != sms_code:
            raise serializers.ValidationError('短信验证码错误')

        # 如果手机已注册,校验密码是否正确
        try:
            user = User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            # 用户不存在,mobile未注册
            user = None
        else:
            # 用户存在,mobile已注册,校验密码是否正确
            password = attrs['password']
            if not user.check_password(password):
                # 密码不正确
                raise serializers.ValidationError('密码错误')

        attrs['user'] = user

        return attrs
Exemplo n.º 6
0
    def validate(self, attrs):
        """access_token是有效,短信验证是否正确"""
        # access_token是有效
        access_token = attrs['access_token']

        openid = OAuthQQ.check_save_user_token(access_token)

        if openid is None:
            raise serializers.ValidationError('无效的access_token')

        attrs['openid'] = openid

        # 短信验证是否正确
        # 获取真实的短信验证码内容
        mobile = attrs['mobile']
        redis_conn = get_redis_connection('verify_codes')
        real_sms_code = redis_conn.get('sms_%s' % mobile)  # bytes

        if not real_sms_code:
            raise serializers.ValidationError('短信验证码已过期')

        # 对比
        sms_code = attrs['sms_code']  # str
        real_sms_code = real_sms_code.decode()  # str
        if sms_code != real_sms_code:
            raise serializers.ValidationError('短信验证码错误')

        # 如果`mobile`已注册,校验对应的密码是否正确
        mobile = attrs['mobile']

        try:
            user = User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            # 未注册,pass
            user = None
        else:
            # 已注册,校验对应的密码是否正确
            password = attrs['password']
            if not user.check_password(password):
                raise serializers.ValidationError('登录密码错误')

        # 给attrs字典中增加一条数据user,保存用户,以便在create中进行使用
        attrs['user'] = user

        return attrs
Exemplo n.º 7
0
    def validate(self, attrs):
        #access_token是否有效
        access_token = attrs['access_token']
        openid = OAuthQQ.check_save_user_token(access_token)
        if openid is None:
            #解密失败
            raise serializers.ValidationError('无效的access_token')
        attrs['openid'] = openid

        #短信验证码是否正确
        mobile = attrs['mobile']

        #从redis中获取真实的验证码内容
        redis_conn = get_redis_connection('verify_codes')

        real_sms_code = redis_conn.get('sms_%s' % mobile)

        if real_sms_code is None:
            raise serializers.ValidationError('短信验证码已失效')

        #对比验证码内容
        sms_code = attrs['sms_code']  #str

        if real_sms_code.decode() != sms_code:
            raise serializers.ValidationError('短信验证码错误')

        #如果`mobile`已注册,校验对应的密码是否正确
        try:
            user = User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            #未注册
            user = None
        else:
            #已注册
            password = attrs['password']
            if not user.check_password(password):
                raise serializers.ValidationError('密码错误')
        #校验是否已有绑定记录了拦截重复请求绑定接口
        count = OAuthQQUser.objects.filter(openid=openid).count()
        if count > 0:
            raise serializers.ValidationError('您已绑定了,请勿重复提交')
        attrs['user'] = user

        return attrs
Exemplo n.º 8
0
    def validate(self, attrs):
        # access_token是否有效
        access_token = attrs['access_token']

        openid = OAuthQQ.check_save_user_token(access_token)

        if openid is None:
            # 解密失败
            raise serializers.ValidationError('无效的access_token')

        attrs['openid'] = openid

        # 短信验证码是否正确
        mobile = attrs.get('mobile')

        # 从redis中获取真实的验证码内容
        redis_conn = get_redis_connection("verify_codes")
        real_sms_code = redis_conn.get('sms_%s' % mobile)  # bytes

        # 判断短信验证码是否过期
        if real_sms_code is None:
            raise serializers.ValidationError('短信验证码以过期')

        user_sms_code = attrs.get('sms_code')

        if real_sms_code.decode() != user_sms_code:
            raise serializers.ValidationError('验证码填写错误')

        # 如果'mobile'已注册,校验对应的密码是否正确
        try:
            user = User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            # 手机号未注册
            user = None
        else:
            # 已经注册,校验密码
            password = attrs['password']
            if not user.check_password(password):
                raise serializers.ValidationError('密码错误')

        attrs['user'] = user

        return attrs
Exemplo n.º 9
0
    def validate(self, attrs):
        # access_token是否有效
        access_token = attrs['access_token']
        openid = OAuthQQ.check_save_user_token(access_token)
        if openid is None:
            # 解密失败
            raise serializers.ValidationError('无效的access_token')
        # 向attrs中添加openid
        attrs['openid'] = openid

        # 短信验证码是否正确
        mobile = attrs['mobile']

        # 从redis中获取真是的短信验证码
        redis_conn = get_redis_connection('verify_codes')
        real_sms_code = redis_conn.get('sms_%s' % mobile)  # bytes
        if real_sms_code is None:
            raise serializers.ValidationError('短信验证码已经失效')
        # 对比短信验证码
        sms_code = attrs['sms_code']  # str
        if real_sms_code.decode() != sms_code:
            raise serializers.ValidationError('短信验证码填写错误')
        # 如果手机号已经注册,需要校验密码是否正确
        try:
            user = User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            # 手机号没注册
            user = None
        else:
            # 手机号已经注册
            password = attrs['password']
            if not user.check_password(password):
                raise serializers.ValidationError('密码错误')
        # 向attrs中添加User
        attrs['user'] = user
        return attrs