update captcha

This commit is contained in:
aichy 2023-07-28 16:44:01 +08:00
parent e421c6eb35
commit c470a8d381
5 changed files with 46 additions and 2 deletions

View File

@ -458,6 +458,10 @@ func (uc *UserController) ActionRecord(ctx *gin.Context) {
if handler.BindAndCheck(ctx, req) {
return
}
userinfo := middleware.GetUserInfoFromContext(ctx)
if userinfo != nil {
req.UserID = userinfo.UserID
}
req.IP = ctx.ClientIP()
resp := &schema.ActionRecordResp{}
isAdmin := middleware.GetUserIsAdminModerator(ctx)

View File

@ -111,7 +111,6 @@ func (a *AnswerAPIRouter) RegisterMustUnAuthAnswerAPIRouter(r *gin.RouterGroup)
routerGroup.GET("/user/register/captcha", a.userController.UserRegisterCaptcha)
routerGroup.POST("/user/email/verification", a.userController.UserVerifyEmail)
routerGroup.PUT("/user/email", a.userController.UserChangeEmailVerify)
routerGroup.GET("/user/action/record", a.userController.ActionRecord)
routerGroup.POST("/user/password/reset", a.userController.RetrievePassWord)
routerGroup.POST("/user/password/replacement", a.userController.UseRePassWord)
routerGroup.PUT("/user/email/notification", a.userController.UserUnsubscribeEmailNotification)
@ -124,6 +123,7 @@ func (a *AnswerAPIRouter) RegisterUnAuthAnswerAPIRouter(r *gin.RouterGroup) {
r.POST("/user/email/verification/send", middleware.BanAPIForUserCenter, a.userController.UserVerifyEmailSend)
r.GET("/personal/user/info", a.userController.GetOtherUserInfoByUsername)
r.GET("/user/ranking", a.userController.UserRanking)
r.GET("/user/action/record", a.userController.ActionRecord)
//answer
r.GET("/answer/info", a.answerController.Get)

View File

@ -328,6 +328,7 @@ type ActionRecordReq struct {
// action
Action string `validate:"required,oneof=email password edit_userinfo question answer comment edit invitation_answer search report delete vote" form:"action"`
IP string `json:"-"`
UserID string `json:"-"`
}
type ActionRecordResp struct {

View File

@ -38,7 +38,32 @@ func NewCaptchaService(captchaRepo CaptchaRepo) *CaptchaService {
// ActionRecord action record
func (cs *CaptchaService) ActionRecord(ctx context.Context, req *schema.ActionRecordReq) (resp *schema.ActionRecordResp, err error) {
resp = &schema.ActionRecordResp{}
verificationResult := cs.ValidationStrategy(ctx, req.IP, req.Action)
unit := req.IP
switch req.Action {
case entity.CaptchaActionEditUserinfo:
unit = req.UserID
case entity.CaptchaActionQuestion:
unit = req.UserID
case entity.CaptchaActionAnswer:
unit = req.UserID
case entity.CaptchaActionComment:
unit = req.UserID
case entity.CaptchaActionEdit:
unit = req.UserID
case entity.CaptchaActionInvitationAnswer:
unit = req.UserID
case entity.CaptchaActionSearch:
if req.UserID != "" {
unit = req.UserID
}
case entity.CaptchaActionReport:
unit = req.UserID
case entity.CaptchaActionDelete:
unit = req.UserID
case entity.CaptchaActionVote:
unit = req.UserID
}
verificationResult := cs.ValidationStrategy(ctx, unit, req.Action)
if !verificationResult {
resp.CaptchaID, resp.CaptchaImg, err = cs.GenerateCaptcha(ctx)
resp.Verify = true

View File

@ -5,6 +5,7 @@ import (
"time"
"github.com/answerdev/answer/internal/entity"
"github.com/davecgh/go-spew/spew"
)
// ValidationStrategy
@ -12,6 +13,7 @@ import (
// false need captcha
func (cs *CaptchaService) ValidationStrategy(ctx context.Context, unit, actionType string) bool {
info, err := cs.captchaRepo.GetActionType(ctx, unit, actionType)
spew.Dump("[ValidationStrategy=验证策略]", unit, actionType, info, err)
if err != nil {
//No record, no processing
//
@ -49,10 +51,12 @@ func (cs *CaptchaService) ValidationStrategy(ctx context.Context, unit, actionTy
func (cs *CaptchaService) CaptchaActionEmail(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
// You need a verification code every time
spew.Dump("[CaptchaActionEmail]", actioninfo)
return false
}
func (cs *CaptchaService) CaptchaActionPassword(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionPassword]", actioninfo)
setNum := 3
setTime := int64(60 * 30) //seconds
now := time.Now().Unix()
@ -66,6 +70,7 @@ func (cs *CaptchaService) CaptchaActionPassword(ctx context.Context, unit string
}
func (cs *CaptchaService) CaptchaActionEditUserinfo(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionEditUserinfo]", actioninfo)
setNum := 3
setTime := int64(60 * 30) //seconds
now := time.Now().Unix()
@ -79,6 +84,7 @@ func (cs *CaptchaService) CaptchaActionEditUserinfo(ctx context.Context, unit st
}
func (cs *CaptchaService) CaptchaActionQuestion(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionQuestion]", actioninfo)
setNum := 3
setTime := int64(5) //seconds
now := time.Now().Unix()
@ -89,6 +95,7 @@ func (cs *CaptchaService) CaptchaActionQuestion(ctx context.Context, unit string
}
func (cs *CaptchaService) CaptchaActionAnswer(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionAnswer]", actioninfo)
setNum := 10
setTime := int64(5) //seconds
now := time.Now().Unix()
@ -99,6 +106,7 @@ func (cs *CaptchaService) CaptchaActionAnswer(ctx context.Context, unit string,
}
func (cs *CaptchaService) CaptchaActionComment(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionComment]", actioninfo)
setNum := 30
setTime := int64(1) //seconds
now := time.Now().Unix()
@ -109,6 +117,7 @@ func (cs *CaptchaService) CaptchaActionComment(ctx context.Context, unit string,
}
func (cs *CaptchaService) CaptchaActionEdit(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionEdit]", actioninfo)
setNum := 10
if actioninfo.Num >= setNum {
return false
@ -117,6 +126,7 @@ func (cs *CaptchaService) CaptchaActionEdit(ctx context.Context, unit string, ac
}
func (cs *CaptchaService) CaptchaActionInvitationAnswer(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionInvitationAnswer]", actioninfo)
setNum := 30
if actioninfo.Num >= setNum {
return false
@ -128,6 +138,7 @@ func (cs *CaptchaService) CaptchaActionSearch(ctx context.Context, unit string,
now := time.Now().Unix()
setNum := 20
setTime := int64(60) //seconds
spew.Dump("[CaptchaActionSearch]", unit, actioninfo, now-int64(actioninfo.LastTime))
if now-int64(actioninfo.LastTime) <= setTime && actioninfo.Num >= setNum {
return false
}
@ -141,6 +152,7 @@ func (cs *CaptchaService) CaptchaActionReport(ctx context.Context, unit string,
setNum := 30
setTime := int64(1) //seconds
now := time.Now().Unix()
spew.Dump("[CaptchaActionReport]", actioninfo, now-int64(actioninfo.LastTime))
if now-actioninfo.LastTime <= setTime || actioninfo.Num >= setNum {
return false
}
@ -148,6 +160,7 @@ func (cs *CaptchaService) CaptchaActionReport(ctx context.Context, unit string,
}
func (cs *CaptchaService) CaptchaActionDelete(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionDelete]", actioninfo)
setNum := 5
setTime := int64(5) //seconds
now := time.Now().Unix()
@ -158,6 +171,7 @@ func (cs *CaptchaService) CaptchaActionDelete(ctx context.Context, unit string,
}
func (cs *CaptchaService) CaptchaActionVote(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionVote]", actioninfo)
setNum := 40
if actioninfo.Num >= setNum {
return false