Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/service/captcha/captcha.go

49 lignes
1 013 o
Go
Brut Vue normale Historique

2017-05-07 10:25:09 +02:00
package captcha
import (
"errors"
2017-05-07 10:25:09 +02:00
"net/http"
"time"
"github.com/dchest/captcha"
)
const lifetime = time.Minute * 20
var (
server = captcha.Server(captcha.StdWidth, captcha.StdHeight)
ErrInvalidCaptcha = errors.New("invalid captcha")
2017-05-07 10:25:09 +02:00
)
func init() {
captcha.SetCustomStore(captcha.NewMemoryStore(1<<10, lifetime))
}
// Captcha is to be embedded into any form struct requiring a Captcha
2017-05-07 10:25:09 +02:00
type Captcha struct {
CaptchaID, Solution string
}
// GetID returns a new Captcha ID
func GetID() string {
return captcha.New()
2017-05-07 10:25:09 +02:00
}
// Extract a Captcha struct from an HTML form
func Extract(r *http.Request) Captcha {
return Captcha{
CaptchaID: r.FormValue("captchaID"),
Solution: r.FormValue("solution"),
}
}
// ServeFiles serves Captcha images and audio
2017-05-07 10:25:09 +02:00
func ServeFiles(w http.ResponseWriter, r *http.Request) {
server.ServeHTTP(w, r)
}
// Authenticate check's if a Captcha solution is valid
2017-05-07 10:25:09 +02:00
func Authenticate(req Captcha) bool {
return captcha.VerifyString(req.CaptchaID, req.Solution)
}