예제 #1
0
    def process_view(self, request, view, args, kwargs):
        """
        可通过登录认证的请求:
        1. 带有BK JWT HEADER
        2. JWT签名正确
        """
        # 框架前置中间件,已将识别的客户端信息填充进 request
        if not hasattr(request, 'is_bk_jwt') or not request.is_bk_jwt():
            return None

        logger.debug('当前请求是否经过JWT转发')
        login_exempt = getattr(view, 'login_exempt', False)

        # 每次请求都需要做校验
        if not (login_exempt or request.user.is_authenticated):
            user = auth.authenticate(request=request)
            if user:
                # 登录成功,确认登陆正常后退出
                auth.login(request, user)
                if request.user.is_authenticated:
                    return None

            handler = ResponseHandler(ConfFixture, settings)
            return handler.build_bk_jwt_401_response(request)
        return None
예제 #2
0
    def process_view(self, request, view, args, kwargs):
        """
        Login paas by two ways
        1. views decorated with 'login_exempt' keyword
        2. User has logged in calling auth.login
        """
        if hasattr(request, 'is_wechat') and request.is_wechat():
            return None

        if hasattr(request, 'is_bk_jwt') and request.is_bk_jwt():
            return None

        if getattr(view, 'login_exempt', False):
            return None

        form = AuthenticationForm(request.COOKIES)
        if form.is_valid():
            bk_token = form.cleaned_data['bk_token']
            user = auth.authenticate(request=request, bk_token=bk_token)
            if user:
                # Succeed to login, recall self to exit process
                if user.username != request.user.username:
                    auth.login(request, user)
                return None
        handler = ResponseHandler(ConfFixture, settings)
        return handler.build_401_response(request)
예제 #3
0
    def process_view(self, request, view, args, kwargs):
        """
        Login paas by two ways
        1. views decorated with 'login_exempt' keyword
        2. User has logged in calling auth.login
        """
        if hasattr(request, 'is_wechat') and request.is_wechat():
            return None

        if getattr(view, 'login_exempt', False):
            return None

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

        handler = ResponseHandler(ConfFixture, settings)
        return handler.build_401_response(request)
예제 #4
0
    def process_view(self, request, view, args, kwargs):
        """
        Login paas by two ways
        1. views decorated with 'login_exempt' keyword
        2. User has logged in calling auth.login
        """
        if hasattr(request, 'is_wechat') and request.is_wechat():
            return None

        if hasattr(request, 'is_bk_jwt') and request.is_bk_jwt():
            return None

        if hasattr(request, 'is_rio') and request.is_rio():
            return None

        if getattr(view, 'login_exempt', False):
            return None

        # 先做数据清洗再执行逻辑
        form = AuthenticationForm(request.COOKIES)
        if form.is_valid():
            bk_token = form.cleaned_data['bk_token']
            session_key = request.session.session_key
            if session_key:
                # 确认 cookie 中的 ticket 和 cache 中的是否一致
                cache_session = cache.get(session_key)
                is_match = (cache_session
                            and bk_token == cache_session.get('bk_token'))
                if is_match and request.user.is_authenticated:
                    return None

            user = auth.authenticate(request=request, bk_token=bk_token)
            if user is not None and user.username != request.user.username:
                auth.login(request, user)

            if user is not None and request.user.is_authenticated:
                # 登录成功,重新调用自身函数,即可退出
                cache.set(session_key, {'bk_token': bk_token},
                          settings.LOGIN_CACHE_EXPIRED)
                return self.process_view(request, view, args, kwargs)

        handler = ResponseHandler(ConfFixture, settings)
        return handler.build_401_response(request)
예제 #5
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)
예제 #6
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