def post(self, request): serializer = self.serializer_class(data=request.data) serializer.is_valid(raise_exception=True) app_id = serializer.validated_data['FEISHU_APP_ID'] app_secret = serializer.validated_data.get('FEISHU_APP_SECRET') if not app_secret: secret = Setting.objects.filter(name='FEISHU_APP_SECRET').first() if secret: app_secret = secret.cleaned_value app_secret = app_secret or '' try: feishu = FeiShu(app_id=app_id, app_secret=app_secret) feishu.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})
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) feishu = FeiShu(app_id=settings.FEISHU_APP_ID, app_secret=settings.FEISHU_APP_SECRET) user_id = feishu.get_user_id_by_code(code) if not user_id: # 正常流程不会出这个错误,hack 行为 msg = _('Failed to get user from FeiShu') response = self.get_failed_response(login_url, title=msg, msg=msg) return response user = get_object_or_none(User, feishu_id=user_id) if user is None: title = _('FeiShu is not bound') msg = _('Please login with a password and then bind the FeiShu') response = self.get_failed_response(login_url, title=title, msg=msg) return response try: self.check_oauth2_auth(user, settings.AUTH_BACKEND_FEISHU) 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()
def get(self, request: HttpRequest): 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 = request.user if user.feishu_id: response = self.get_already_bound_response(redirect_url) return response feishu = FeiShu(app_id=settings.FEISHU_APP_ID, app_secret=settings.FEISHU_APP_SECRET) user_id = feishu.get_user_id_by_code(code) if not user_id: msg = _('FeiShu query user failed') response = self.get_failed_response(redirect_url, msg, msg) return response try: user.feishu_id = user_id user.save() except IntegrityError as e: if e.args[0] == 1062: msg = _('The FeiShu is already bound to another user') response = self.get_failed_response(redirect_url, msg, msg) return response raise e msg = _('Binding FeiShu successfully') response = self.get_success_response(redirect_url, msg, msg) return response
class FeiShu(BackendBase): account_field = 'feishu_id' is_enable_field_in_settings = 'AUTH_FEISHU' def __init__(self): self.client = Client(app_id=settings.FEISHU_APP_ID, app_secret=settings.FEISHU_APP_SECRET) def send_msg(self, users, message, subject=None): accounts, __, __ = self.get_accounts(users) return self.client.send_text(accounts, message)
def __init__(self): self.client = Client(app_id=settings.FEISHU_APP_ID, app_secret=settings.FEISHU_APP_SECRET)