-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
captcha.go
45 lines (39 loc) · 1.07 KB
/
captcha.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package captcha
import (
"strings"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/mojocn/base64Captcha"
)
var store = base64Captcha.DefaultMemStore
func VerifyCode(codeID string, code string) error {
if codeID == "" {
return constant.ErrCaptchaCode
}
vv := store.Get(codeID, true)
vv = strings.TrimSpace(vv)
code = strings.TrimSpace(code)
if strings.EqualFold(vv, code) {
return nil
}
return constant.ErrCaptchaCode
}
func CreateCaptcha() (*dto.CaptchaResponse, error) {
var driverString base64Captcha.DriverString
driverString.Source = "1234567890QWERTYUPLKJHGFDSAZXCVBNMqwertyupkjhgfdsazxcvbnm"
driverString.Width = 120
driverString.Height = 50
driverString.NoiseCount = 0
driverString.Length = 4
driverString.Fonts = []string{"RitaSmith.ttf", "actionj.ttf", "chromohv.ttf"}
driver := driverString.ConvertFonts()
c := base64Captcha.NewCaptcha(driver, store)
id, b64s, _, err := c.Generate()
if err != nil {
return nil, err
}
return &dto.CaptchaResponse{
CaptchaID: id,
ImagePath: b64s,
}, nil
}