answer/internal/service/action/captcha_service.go

179 lines
5.1 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package action
import (
"context"
"image/color"
"strings"
"github.com/answerdev/answer/internal/base/reason"
2023-07-17 17:44:08 +08:00
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/schema"
2022-09-27 17:59:05 +08:00
"github.com/mojocn/base64Captcha"
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
)
// CaptchaRepo captcha repository
type CaptchaRepo interface {
SetCaptcha(ctx context.Context, key, captcha string) (err error)
GetCaptcha(ctx context.Context, key string) (captcha string, err error)
2023-02-24 16:30:21 +08:00
DelCaptcha(ctx context.Context, key string) (err error)
2023-07-17 18:46:09 +08:00
SetActionType(ctx context.Context, unit, actionType, config string, amount int) (err error)
2023-07-17 17:44:08 +08:00
GetActionType(ctx context.Context, unit, actionType string) (actioninfo *entity.ActionRecordInfo, err error)
DelActionType(ctx context.Context, unit, actionType string) (err error)
2022-09-27 17:59:05 +08:00
}
// CaptchaService kit service
type CaptchaService struct {
captchaRepo CaptchaRepo
}
// NewCaptchaService captcha service
func NewCaptchaService(captchaRepo CaptchaRepo) *CaptchaService {
return &CaptchaService{
captchaRepo: captchaRepo,
}
}
// ActionRecord action record
func (cs *CaptchaService) ActionRecord(ctx context.Context, req *schema.ActionRecordReq) (resp *schema.ActionRecordResp, err error) {
resp = &schema.ActionRecordResp{}
2023-07-28 16:44:01 +08:00
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)
2023-07-17 17:44:08 +08:00
if !verificationResult {
2022-09-27 17:59:05 +08:00
resp.CaptchaID, resp.CaptchaImg, err = cs.GenerateCaptcha(ctx)
resp.Verify = true
}
return
}
2022-12-18 00:14:50 +08:00
func (cs *CaptchaService) UserRegisterCaptcha(ctx context.Context) (resp *schema.ActionRecordResp, err error) {
resp = &schema.ActionRecordResp{}
resp.CaptchaID, resp.CaptchaImg, err = cs.GenerateCaptcha(ctx)
resp.Verify = true
return
}
func (cs *CaptchaService) UserRegisterVerifyCaptcha(
ctx context.Context, id string, VerifyValue string,
) bool {
if id == "" || VerifyValue == "" {
return false
}
pass, err := cs.VerifyCaptcha(ctx, id, VerifyValue)
if err != nil {
return false
}
return pass
}
2022-09-27 17:59:05 +08:00
// ActionRecordVerifyCaptcha
// Verify that you need to enter a CAPTCHA, and that the CAPTCHA is correct
func (cs *CaptchaService) ActionRecordVerifyCaptcha(
2023-07-17 18:46:09 +08:00
ctx context.Context, actionType string, unit string, id string, VerifyValue string,
) bool {
2023-07-17 18:46:09 +08:00
verificationResult := cs.ValidationStrategy(ctx, unit, actionType)
2023-07-17 17:44:08 +08:00
if !verificationResult {
2022-12-18 00:14:50 +08:00
if id == "" || VerifyValue == "" {
return false
}
2022-09-27 17:59:05 +08:00
pass, err := cs.VerifyCaptcha(ctx, id, VerifyValue)
if err != nil {
return false
}
return pass
}
return true
}
2023-07-17 18:46:09 +08:00
func (cs *CaptchaService) ActionRecordAdd(ctx context.Context, actionType string, unit string) (int, error) {
info, err := cs.captchaRepo.GetActionType(ctx, unit, actionType)
if err != nil {
2022-09-27 17:59:05 +08:00
log.Error(err)
return 0, err
}
amount := 1
if info != nil {
amount = info.Num + 1
2022-09-27 17:59:05 +08:00
}
err = cs.captchaRepo.SetActionType(ctx, unit, actionType, "", amount)
2022-09-27 17:59:05 +08:00
if err != nil {
return 0, err
}
return amount, nil
2022-09-27 17:59:05 +08:00
}
2023-07-17 18:46:09 +08:00
func (cs *CaptchaService) ActionRecordDel(ctx context.Context, actionType string, unit string) {
err := cs.captchaRepo.DelActionType(ctx, unit, actionType)
2022-09-27 17:59:05 +08:00
if err != nil {
log.Error(err)
}
}
// GenerateCaptcha generate captcha
func (cs *CaptchaService) GenerateCaptcha(ctx context.Context) (key, captchaBase64 string, err error) {
driverString := base64Captcha.DriverString{
2023-07-04 14:32:34 +08:00
Height: 60,
Width: 200,
2022-09-27 17:59:05 +08:00
NoiseCount: 0,
ShowLineOptions: 2 | 4,
Length: 4,
Source: "1234567890qwertyuioplkjhgfdsazxcvbnm",
2023-07-04 14:32:34 +08:00
BgColor: &color.RGBA{R: 211, G: 211, B: 211, A: 0},
2022-09-27 17:59:05 +08:00
Fonts: []string{"wqy-microhei.ttc"},
}
driver := driverString.ConvertFonts()
id, content, answer := driver.GenerateIdQuestionAnswer()
item, err := driver.DrawCaptcha(content)
if err != nil {
return "", "", errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
}
err = cs.captchaRepo.SetCaptcha(ctx, id, answer)
if err != nil {
return "", "", err
}
captchaBase64 = item.EncodeB64string()
return id, captchaBase64, nil
}
// VerifyCaptcha generate captcha
func (cs *CaptchaService) VerifyCaptcha(ctx context.Context, key, captcha string) (isCorrect bool, err error) {
realCaptcha, err := cs.captchaRepo.GetCaptcha(ctx, key)
if err != nil {
2023-02-24 16:30:21 +08:00
log.Error("VerifyCaptcha GetCaptcha Error", err.Error())
return false, nil
}
err = cs.captchaRepo.DelCaptcha(ctx, key)
if err != nil {
log.Error("VerifyCaptcha DelCaptcha Error", err.Error())
2022-09-27 17:59:05 +08:00
return false, nil
}
return strings.TrimSpace(captcha) == realCaptcha, nil
}