forked from NyaaPantsu/nyaa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
captcha.go
49 lines (39 loc) · 1.04 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
46
47
48
49
package captcha
import (
"errors"
"time"
"github.com/dchest/captcha"
"github.com/gin-gonic/gin"
)
const lifetime = time.Minute * 20
var (
server = captcha.Server(captcha.StdWidth, captcha.StdHeight)
// ErrInvalidCaptcha : Error when captcha is invalid
ErrInvalidCaptcha = errors.New("invalid captcha")
)
func init() {
captcha.SetCustomStore(captcha.NewMemoryStore(1<<10, lifetime))
}
// Captcha is to be embedded into any form struct requiring a Captcha
type Captcha struct {
CaptchaID, Solution string
}
// GetID returns a new Captcha ID
func GetID() string {
return captcha.New()
}
// Extract a Captcha struct from an HTML form
func Extract(c *gin.Context) Captcha {
return Captcha{
CaptchaID: c.PostForm("captchaID"),
Solution: c.PostForm("solution"),
}
}
// ServeFiles serves Captcha images and audio
func ServeFiles(c *gin.Context) {
server.ServeHTTP(c.Writer, c.Request)
}
// Authenticate check's if a Captcha solution is valid
func Authenticate(req Captcha) bool {
return captcha.VerifyString(req.CaptchaID, req.Solution)
}