diff --git a/apps/authentication/views/feishu.py b/apps/authentication/views/feishu.py index 16e111bad..e9734b170 100644 --- a/apps/authentication/views/feishu.py +++ b/apps/authentication/views/feishu.py @@ -60,7 +60,7 @@ class FeiShuQRMixin(UserConfirmRequiredExceptionMixin, PermissionsMixin, View): 'state': state, 'redirect_uri': redirect_uri, } - url = URL.AUTHEN + '?' + urlencode(params) + url = URL().authen + '?' + urlencode(params) return url @staticmethod diff --git a/apps/common/sdk/im/feishu/__init__.py b/apps/common/sdk/im/feishu/__init__.py index 1cfee9cdd..cb01b66da 100644 --- a/apps/common/sdk/im/feishu/__init__.py +++ b/apps/common/sdk/im/feishu/__init__.py @@ -3,6 +3,7 @@ import json from django.utils.translation import ugettext_lazy as _ from rest_framework.exceptions import APIException +from django.conf import settings from common.utils.common import get_logger from common.sdk.im.utils import digest from common.sdk.im.mixin import RequestMixin, BaseRequest @@ -11,14 +12,30 @@ logger = get_logger(__name__) class URL: - AUTHEN = 'https://open.feishu.cn/open-apis/authen/v1/index' - - GET_TOKEN = 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/' - # https://open.feishu.cn/document/ukTMukTMukTM/uEDO4UjLxgDO14SM4gTN - GET_USER_INFO_BY_CODE = 'https://open.feishu.cn/open-apis/authen/v1/access_token' + @property + def host(self): + if settings.FEISHU_VERSION == 'feishu': + h = 'https://open.feishu.cn' + else: + h = 'https://open.larksuite.com' + return h - SEND_MESSAGE = 'https://open.feishu.cn/open-apis/im/v1/messages' + @property + def authen(self): + return f'{self.host}/open-apis/authen/v1/index' + + @property + def get_token(self): + return f'{self.host}/open-apis/auth/v3/tenant_access_token/internal/' + + @property + def get_user_info_by_code(self): + return f'{self.host}/open-apis/authen/v1/access_token' + + @property + def send_message(self): + return f'{self.host}/open-apis/im/v1/messages' class ErrorCode: @@ -51,7 +68,7 @@ class FeishuRequests(BaseRequest): def request_access_token(self): data = {'app_id': self._app_id, 'app_secret': self._app_secret} - response = self.raw_request('post', url=URL.GET_TOKEN, data=data) + response = self.raw_request('post', url=URL().get_token, data=data) self.check_errcode_is_0(response) access_token = response['tenant_access_token'] @@ -86,7 +103,7 @@ class FeiShu(RequestMixin): 'code': code } - data = self._requests.post(URL.GET_USER_INFO_BY_CODE, json=body, check_errcode_is_0=False) + data = self._requests.post(URL().get_user_info_by_code, json=body, check_errcode_is_0=False) self._requests.check_errcode_is_0(data) return data['data']['user_id'] @@ -107,7 +124,7 @@ class FeiShu(RequestMixin): try: logger.info(f'Feishu send text: user_ids={user_ids} msg={msg}') - self._requests.post(URL.SEND_MESSAGE, params=params, json=body) + self._requests.post(URL().send_message, params=params, json=body) except APIException as e: # 只处理可预知的错误 logger.exception(e) diff --git a/apps/jumpserver/conf.py b/apps/jumpserver/conf.py index b192cd993..86057dcd1 100644 --- a/apps/jumpserver/conf.py +++ b/apps/jumpserver/conf.py @@ -376,6 +376,7 @@ class Config(dict): 'AUTH_FEISHU': False, 'FEISHU_APP_ID': '', 'FEISHU_APP_SECRET': '', + 'FEISHU_VERSION': 'feishu', 'LOGIN_REDIRECT_TO_BACKEND': '', # 'OPENID / CAS / SAML2 'LOGIN_REDIRECT_MSG_ENABLED': True, diff --git a/apps/jumpserver/settings/auth.py b/apps/jumpserver/settings/auth.py index f1fec05cd..2eab9ebd8 100644 --- a/apps/jumpserver/settings/auth.py +++ b/apps/jumpserver/settings/auth.py @@ -137,6 +137,7 @@ DINGTALK_APPSECRET = CONFIG.DINGTALK_APPSECRET AUTH_FEISHU = CONFIG.AUTH_FEISHU FEISHU_APP_ID = CONFIG.FEISHU_APP_ID FEISHU_APP_SECRET = CONFIG.FEISHU_APP_SECRET +FEISHU_VERSION = CONFIG.FEISHU_VERSION # Saml2 auth AUTH_SAML2 = CONFIG.AUTH_SAML2 diff --git a/apps/settings/serializers/auth/feishu.py b/apps/settings/serializers/auth/feishu.py index 1443a244c..a06d41b23 100644 --- a/apps/settings/serializers/auth/feishu.py +++ b/apps/settings/serializers/auth/feishu.py @@ -9,6 +9,13 @@ __all__ = ['FeiShuSettingSerializer'] class FeiShuSettingSerializer(serializers.Serializer): PREFIX_TITLE = _('FeiShu') + VERSION_CHOICES = ( + ('feishu', _('FeiShu')), + ('lark', 'Lark') + ) + AUTH_FEISHU = serializers.BooleanField(default=False, label=_('Enable FeiShu Auth')) FEISHU_APP_ID = serializers.CharField(max_length=256, required=True, label='App ID') FEISHU_APP_SECRET = EncryptedField(max_length=256, required=False, label='App Secret') - AUTH_FEISHU = serializers.BooleanField(default=False, label=_('Enable FeiShu Auth')) + FEISHU_VERSION = serializers.ChoiceField( + choices=VERSION_CHOICES, default='feishu', label=_('Version') + )