Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: validate feishu app (#12416)

p0nyae5256d15f47
diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.go
index c54bd96de2..fb158ab928 100644
--- a/backend/api/v1/setting_service.go
+++ b/backend/api/v1/setting_service.go
@@ -25,6 +25,7 @@ import (
api "github.com/bytebase/bytebase/backend/legacyapi"
"github.com/bytebase/bytebase/backend/plugin/mail"
"github.com/bytebase/bytebase/backend/plugin/schema"
+ "github.com/bytebase/bytebase/backend/plugin/webhook/feishu"
"github.com/bytebase/bytebase/backend/plugin/webhook/slack"
"github.com/bytebase/bytebase/backend/store"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
@@ -134,6 +135,11 @@ func (s *SettingService) GetSetting(ctx context.Context, request *v1pb.GetSettin
// SetSetting set the setting by name.
func (s *SettingService) UpdateSetting(ctx context.Context, request *v1pb.UpdateSettingRequest) (*v1pb.Setting, error) {
+ user, ok := ctx.Value(common.UserContextKey).(*store.UserMessage)
+ if !ok {
+ return nil, status.Errorf(codes.Internal, "user not found")
+ }
+
settingName, err := common.GetSettingName(request.Setting.Name)
if err != nil {
return nil, err
@@ -342,11 +348,15 @@ func (s *SettingService) UpdateSetting(ctx context.Context, request *v1pb.Update
for _, path := range request.UpdateMask.Paths {
switch path {
case "value.app_im_setting_value.slack":
- setting.Slack = payload.Slack
if err := slack.ValidateToken(ctx, payload.Slack.GetToken()); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "token doesn't pass validation, error: %v", err)
}
+ setting.Slack = payload.Slack
+
case "value.app_im_setting_value.feishu":
+ if err := feishu.Validate(ctx, payload.GetFeishu().GetAppId(), payload.GetFeishu().GetAppSecret(), user.Email); err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, "token does not pass validation, error: %v", err)
+ }
setting.Feishu = payload.Feishu
case "value.app_im_setting_value.wecom":
setting.Wecom = payload.Wecom
diff --git a/backend/plugin/webhook/feishu/app.go b/backend/plugin/webhook/feishu/app.go
index e57b80263d..537d349356 100644
--- a/backend/plugin/webhook/feishu/app.go
+++ b/backend/plugin/webhook/feishu/app.go
@@ -70,6 +70,18 @@ type generalResponse struct {
Msg string `json:"msg"`
}
+func Validate(ctx context.Context, id, secret, email string) error {
+ p := newProvider(id, secret)
+ if err := p.refreshToken(ctx); err != nil {
+ return errors.Wrapf(err, "failed to refresh token")
+ }
+ _, err := p.getIDByEmail(ctx, []string{email})
+ if err != nil {
+ return errors.Wrapf(err, "failed to get id for user %s", email)
+ }
+ return nil
+}
+
func (p *provider) refreshToken(ctx context.Context) error {
const getTenantAccessTokenReq = `{"app_id": "%s","app_secret": "%s"}`
const url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"