示例#1
0
    def process_view(self, request, view, args, kwargs):
        """
        可通过登录认证的方式,仅有两种
        1. 带有 login_exemp 标识的 view 函数
        2. 用户已成功 auth.login
        """
        # 框架前置中间件,已将识别的客户端信息填充进 request
        if not request.is_wechat():
            return None

        logger.debug('当前请求客户端为微信端')
        login_exempt = getattr(view, 'login_exempt', False)
        if login_exempt or request.user.is_authenticated:
            return None

        user = WeixinLoginRequiredMiddleware.authenticate(request)
        if user:
            return None

        WeixinLoginRequiredMiddleware.set_state(request)
        handler = ResponseHandler(ConfFixture, settings)
        return handler.build_weixin_401_response(request)
示例#2
0
    def process_view(self, request, view, args, kwargs):
        """
        可通过登录认证的方式,仅有两种
        1. 带有 login_exemp 标识的 view 函数
        2. 用户已成功 auth.login
        """
        # 框架前置中间件,已将识别的客户端信息填充进 request
        if not request.is_wechat():
            return None

        logger.debug('当前请求客户端为微信端')
        login_exempt = getattr(view, 'login_exempt', False)
        if not (login_exempt or request.user.is_authenticated):

            form = WeixinAuthenticationForm(request.GET)

            if form.is_valid():
                code = form.cleaned_data['code']
                state = form.cleaned_data['state']
                logger.debug(u"微信请求链接,检测到微信验证码,code:{},state:{}".format(
                    code, state))

                if self.valid_state(request, state):
                    user = auth.authenticate(request=request,
                                             code=code,
                                             is_wechat=True)
                    if user and user.username != request.user.username:
                        auth.login(request, user)
                    if request.user.is_authenticated:
                        # 登录成功,确认登陆正常后退出
                        return None
            else:
                logger.debug(u"微信请求链接,未检测到微信验证码,url:{},params:{}".format(
                    request.path_info, request.GET))

            self.set_state(request)
            handler = ResponseHandler(ConfFixture, settings)
            return handler.build_weixin_401_response(request)
        return None