From e0b12d7bb780dae81b43677d38477b683bcdc9fd Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 31 Jul 2025 10:35:02 +0800 Subject: [PATCH] Add Captcha handlers to OpenAPI routing - Integrated Captcha functionality by adding a new route for captcha handlers within the OpenAPI structure. - Updated the Attach method to include the Captcha endpoint, ensuring OAuth protection for the new route. - Enhanced overall routing organization by grouping captcha-related handlers under a dedicated path. --- openapi/captcha/captcha.go | 57 ++++++++++++++++++++++++++++++++++++++ openapi/openapi.go | 4 +++ 2 files changed, 61 insertions(+) create mode 100644 openapi/captcha/captcha.go diff --git a/openapi/captcha/captcha.go b/openapi/captcha/captcha.go new file mode 100644 index 00000000..7db9cadd --- /dev/null +++ b/openapi/captcha/captcha.go @@ -0,0 +1,57 @@ +package captcha + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/yaoapp/yao/helper" + "github.com/yaoapp/yao/openapi/oauth/types" + "github.com/yaoapp/yao/openapi/response" +) + +// Attach attaches the hello world handlers to the router +func Attach(group *gin.RouterGroup, oauth types.OAuth) { + + // Health check + group.GET("/image", image) + + // OAuth Protected Resource + group.GET("/audio", audio) +} + +// image captcha +func image(c *gin.Context) { + var option helper.CaptchaOption = helper.NewCaptchaOption() + + err := c.ShouldBindQuery(&option) + if err != nil { + response.RespondWithError(c, http.StatusBadRequest, &response.ErrorResponse{ + Code: response.ErrInvalidRequest.Code, + ErrorDescription: err.Error(), + }) + return + } + + // Set the type to image + option.Type = "image" + id, content := helper.CaptchaMake(option) + response.RespondWithSuccess(c, http.StatusOK, gin.H{"id": id, "data": content}) +} + +// audio captcha +func audio(c *gin.Context) { + var option helper.CaptchaOption = helper.NewCaptchaOption() + + err := c.ShouldBindQuery(&option) + if err != nil { + response.RespondWithError(c, http.StatusBadRequest, &response.ErrorResponse{ + Code: response.ErrInvalidRequest.Code, + ErrorDescription: err.Error(), + }) + } + + // Set the type to audio + option.Type = "audio" + id, content := helper.CaptchaMake(option) + response.RespondWithSuccess(c, http.StatusOK, gin.H{"id": id, "data": content}) +} diff --git a/openapi/openapi.go b/openapi/openapi.go index f23d19ff..9a82a329 100644 --- a/openapi/openapi.go +++ b/openapi/openapi.go @@ -6,6 +6,7 @@ import ( "github.com/gin-gonic/gin" "github.com/yaoapp/gou/application" "github.com/yaoapp/yao/config" + "github.com/yaoapp/yao/openapi/captcha" "github.com/yaoapp/yao/openapi/chat" "github.com/yaoapp/yao/openapi/dsl" "github.com/yaoapp/yao/openapi/file" @@ -100,5 +101,8 @@ func (openapi *OpenAPI) Attach(router *gin.Engine) { // Signin handlers signin.Attach(group, openapi.OAuth) + // Captcha handlers + captcha.Attach(group.Group("/captcha"), openapi.OAuth) + // Custom handlers (Defined by developer) }