A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
feat(captcha): add captcha config API
LinkinStars0894cd94a954
diff --git a/cmd/wire_gen.go b/cmd/wire_gen.goindex 7ed904d0..23718e52 100644--- a/cmd/wire_gen.go+++ b/cmd/wire_gen.go@@ -1,28 +1,8 @@-//go:build !wireinject-// +build !wireinject--/*- * Licensed to the Apache Software Foundation (ASF) under one- * or more contributor license agreements. See the NOTICE file- * distributed with this work for additional information- * regarding copyright ownership. The ASF licenses this file- * to you under the Apache License, Version 2.0 (the- * "License"); you may not use this file except in compliance- * with the License. You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing,- * software distributed under the License is distributed on an- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY- * KIND, either express or implied. See the License for the- * specific language governing permissions and limitations- * under the License.- */-// Code generated by Wire. DO NOT EDIT.//go:generate go run github.com/google/wire/cmd/wire+//go:build !wireinject+// +build !wireinjectpackage answercmd@@ -262,7 +242,8 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,connectorController := controller.NewConnectorController(siteInfoCommonService, emailService, userExternalLoginService)userCenterLoginService := user_external_login2.NewUserCenterLoginService(userRepo, userCommon, userExternalLoginRepo, userActiveActivityRepo, siteInfoCommonService)userCenterController := controller.NewUserCenterController(userCenterLoginService, siteInfoCommonService)- pluginAPIRouter := router.NewPluginAPIRouter(connectorController, userCenterController)+ captchaController := controller.NewCaptchaController()+ pluginAPIRouter := router.NewPluginAPIRouter(connectorController, userCenterController, captchaController)ginEngine := server.NewHTTPServer(debug, staticRouter, answerAPIRouter, swaggerRouter, uiRouter, authUserMiddleware, avatarMiddleware, shortIDMiddleware, templateRouter, pluginAPIRouter)scheduledTaskManager := cron.NewScheduledTaskManager(siteInfoCommonService, questionService)application := newApplication(serverConf, ginEngine, scheduledTaskManager)diff --git a/internal/controller/controller.go b/internal/controller/controller.goindex 42c2c1d3..c690d4c7 100644--- a/internal/controller/controller.go+++ b/internal/controller/controller.go@@ -48,4 +48,5 @@ var ProviderSetController = wire.NewSet(NewPermissionController,NewUserPluginController,NewReviewController,+ NewCaptchaController,)diff --git a/internal/controller/plugin_captcha_controller.go b/internal/controller/plugin_captcha_controller.gonew file mode 100644index 00000000..7b47e071--- /dev/null+++ b/internal/controller/plugin_captcha_controller.go@@ -0,0 +1,53 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one+ * or more contributor license agreements. See the NOTICE file+ * distributed with this work for additional information+ * regarding copyright ownership. The ASF licenses this file+ * to you under the Apache License, Version 2.0 (the+ * "License"); you may not use this file except in compliance+ * with the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing,+ * software distributed under the License is distributed on an+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+ * KIND, either express or implied. See the License for the+ * specific language governing permissions and limitations+ * under the License.+ */++package controller++import (+ "encoding/json"++ "github.com/apache/incubator-answer/internal/base/handler"+ "github.com/apache/incubator-answer/plugin"+ "github.com/gin-gonic/gin"+)++// CaptchaController comment controller+type CaptchaController struct {+}++// NewCaptchaController new controller+func NewCaptchaController() *CaptchaController {+ return &CaptchaController{}+}++type GetCaptchaConfigResp struct {+ SlugName string `json:"slug_name"`+ Config map[string]any `json:"config"`+}++// GetCaptchaConfig get captcha config+func (uc *CaptchaController) GetCaptchaConfig(ctx *gin.Context) {+ resp := &GetCaptchaConfigResp{}+ _ = plugin.CallCaptcha(func(fn plugin.Captcha) error {+ resp.SlugName = fn.Info().SlugName+ _ = json.Unmarshal([]byte(fn.GetConfig()), &resp.Config)+ return nil+ })+ handler.HandleResponse(ctx, nil, resp)+}diff --git a/internal/router/plugin_api_router.go b/internal/router/plugin_api_router.goindex 8554b4c7..e0e42b2d 100644--- a/internal/router/plugin_api_router.go+++ b/internal/router/plugin_api_router.go@@ -27,15 +27,18 @@ import (type PluginAPIRouter struct {connectorController *controller.ConnectorControlleruserCenterController *controller.UserCenterController+ captchaController *controller.CaptchaController}func NewPluginAPIRouter(connectorController *controller.ConnectorController,userCenterController *controller.UserCenterController,+ captchaController *controller.CaptchaController,) *PluginAPIRouter {return &PluginAPIRouter{connectorController: connectorController,userCenterController: userCenterController,+ captchaController: captchaController,}}@@ -54,6 +57,9 @@ func (pr *PluginAPIRouter) RegisterUnAuthConnectorRouter(r *gin.RouterGroup) {r.GET(controller.UserCenterSignUpRedirectRouter, pr.userCenterController.UserCenterSignUpRedirect)r.GET("/user-center/login/callback", pr.userCenterController.UserCenterLoginCallback)r.GET("/user-center/sign-up/callback", pr.userCenterController.UserCenterSignUpCallback)++ // captcha plugin+ r.GET("/captcha/config", pr.captchaController.GetCaptchaConfig)}func (pr *PluginAPIRouter) RegisterAuthUserConnectorRouter(r *gin.RouterGroup) {diff --git a/internal/service/action/captcha_service.go b/internal/service/action/captcha_service.goindex 888946ff..326ea637 100644--- a/internal/service/action/captcha_service.go+++ b/internal/service/action/captcha_service.go@@ -81,9 +81,10 @@ func (cs *CaptchaService) ActionRecord(ctx context.Context, req *schema.ActionRe}verificationResult := cs.ValidationStrategy(ctx, unit, req.Action)if !verificationResult {+ resp.Verify = trueresp.CaptchaID, resp.CaptchaImg, err = cs.GenerateCaptcha(ctx)- if len(resp.CaptchaID) > 0 {- resp.Verify = true+ if err != nil {+ log.Errorf("GenerateCaptcha error: %v", err)}}return@@ -132,6 +133,7 @@ func (cs *CaptchaService) ActionRecordDel(ctx context.Context, actionType string// GenerateCaptcha generate captchafunc (cs *CaptchaService) GenerateCaptcha(ctx context.Context) (key, captchaBase64 string, err error) {realCaptcha := ""+ key = token.GenerateToken()_ = plugin.CallCaptcha(func(fn plugin.Captcha) error {if captcha, code := fn.Create(); len(code) > 0 {captchaBase64 = captcha@@ -140,15 +142,11 @@ func (cs *CaptchaService) GenerateCaptcha(ctx context.Context) (key, captchaBasereturn nil})if len(realCaptcha) == 0 {- return "", "", nil+ return key, captchaBase64, nil}- id := token.GenerateToken()- err = cs.captchaRepo.SetCaptcha(ctx, id, realCaptcha)- if err != nil {- return "", "", err- }- return id, captchaBase64, nil+ err = cs.captchaRepo.SetCaptcha(ctx, key, realCaptcha)+ return key, captchaBase64, err}// VerifyCaptcha generate captchadiff --git a/plugin/captcha.go b/plugin/captcha.goindex f73fa784..81ee0e13 100644--- a/plugin/captcha.go+++ b/plugin/captcha.go@@ -21,6 +21,9 @@ package plugintype Captcha interface {Base+ // GetConfig required. Get the captcha plugin configuration.+ // The configuration is used to generate the captcha for frontend. Such as the token for third-party service.+ GetConfig() (configJsonStr string)// Create optional. If this plugin need to create captcha via backend, implement this method.// On other hand, if this plugin create captcha via third-party service, ignore this method.// Return captcha: The captcha image base64 string, code: The real captcha code.