Ejemplo n.º 1
0
    def post(self, request):
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)

        wecom_corpid = serializer.validated_data['WECOM_CORPID']
        wecom_agentid = serializer.validated_data['WECOM_AGENTID']
        wecom_corpsecret = serializer.validated_data.get('WECOM_SECRET')

        if not wecom_corpsecret:
            secret = Setting.objects.filter(name='WECOM_SECRET').first()
            if secret:
                wecom_corpsecret = secret.cleaned_value

        wecom_corpsecret = wecom_corpsecret or ''

        try:
            wecom = WeCom(corpid=wecom_corpid,
                          corpsecret=wecom_corpsecret,
                          agentid=wecom_agentid)
            wecom.send_text(['test'], 'test')
            return Response(status=status.HTTP_200_OK,
                            data={'msg': _('Test success')})
        except APIException as e:
            try:
                error = e.detail['errmsg']
            except:
                error = e.detail
            return Response(status=status.HTTP_400_BAD_REQUEST,
                            data={'error': error})
Ejemplo n.º 2
0
    def get(self, request: HttpRequest, user_id):
        code = request.GET.get('code')
        redirect_url = request.GET.get('redirect_url')

        if not self.verify_state():
            return self.get_verify_state_failed_response(redirect_url)

        user = get_object_or_none(User, id=user_id)
        if user is None:
            logger.error(
                f'WeComQR bind callback error, user_id invalid: user_id={user_id}'
            )
            msg = _('Invalid user_id')
            response = self.get_failed_response(redirect_url, msg, msg)
            return response

        if user.wecom_id:
            response = self.get_already_bound_response(redirect_url)
            return response

        wecom = WeCom(corpid=settings.WECOM_CORPID,
                      corpsecret=settings.WECOM_SECRET,
                      agentid=settings.WECOM_AGENTID)
        wecom_userid, __ = wecom.get_user_id_by_code(code)
        if not wecom_userid:
            msg = _('WeCom query user failed')
            response = self.get_failed_response(redirect_url, msg, msg)
            return response

        try:
            user.wecom_id = wecom_userid
            user.save()
        except IntegrityError as e:
            if e.args[0] == 1062:
                msg = _('The WeCom is already bound to another user')
                response = self.get_failed_response(redirect_url, msg, msg)
                return response
            raise e

        ip = get_request_ip(request)
        OAuthBindMessage(user, ip, _('WeCom'), wecom_userid).publish_async()
        msg = _('Binding WeCom successfully')
        response = self.get_success_response(redirect_url, msg, msg)
        return response
Ejemplo n.º 3
0
class WeCom(BackendBase):
    account_field = 'wecom_id'
    is_enable_field_in_settings = 'AUTH_WECOM'

    def __init__(self):
        self.wecom = Client(
            corpid=settings.WECOM_CORPID,
            corpsecret=settings.WECOM_SECRET,
            agentid=settings.WECOM_AGENTID
        )

    def send_msg(self, users, message, subject=None):
        accounts, __, __ = self.get_accounts(users)
        return self.wecom.send_text(accounts, message, markdown=True)
Ejemplo n.º 4
0
    def get(self, request: HttpRequest):
        code = request.GET.get('code')
        redirect_url = request.GET.get('redirect_url')
        login_url = reverse('authentication:login')

        if not self.verify_state():
            return self.get_verify_state_failed_response(redirect_url)

        wecom = WeCom(corpid=settings.WECOM_CORPID,
                      corpsecret=settings.WECOM_SECRET,
                      agentid=settings.WECOM_AGENTID)
        wecom_userid, __ = wecom.get_user_id_by_code(code)
        if not wecom_userid:
            # 正常流程不会出这个错误,hack 行为
            msg = _('Failed to get user from WeCom')
            response = self.get_failed_response(login_url, title=msg, msg=msg)
            return response

        user = get_object_or_none(User, wecom_id=wecom_userid)
        if user is None:
            title = _('WeCom is not bound')
            msg = _('Please login with a password and then bind the WeCom')
            response = self.get_failed_response(login_url,
                                                title=title,
                                                msg=msg)
            return response

        try:
            self.check_oauth2_auth(user, settings.AUTH_BACKEND_WECOM)
        except errors.AuthFailedError as e:
            self.set_login_failed_mark()
            msg = e.msg
            response = self.get_failed_response(login_url, title=msg, msg=msg)
            return response

        return self.redirect_to_guard_view()
Ejemplo n.º 5
0
 def __init__(self):
     self.wecom = Client(
         corpid=settings.WECOM_CORPID,
         corpsecret=settings.WECOM_SECRET,
         agentid=settings.WECOM_AGENTID
     )