6481e90a0c
* Gofmt friendly Keeping Go source code in line with what they preconize * Golint Friendly Next So I have made some variables unexported Added comments in every function that I know what it does Removed some deprecated stuff that I was sure of Added a comment on possible deprecated methods "Is it deprecated?" Changed some variable/method name according to golint recommendations * Update filelist.go
49 lignes
1 Kio
Go
49 lignes
1 Kio
Go
package captcha
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/dchest/captcha"
|
|
)
|
|
|
|
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(r *http.Request) Captcha {
|
|
return Captcha{
|
|
CaptchaID: r.FormValue("captchaID"),
|
|
Solution: r.FormValue("solution"),
|
|
}
|
|
}
|
|
|
|
// ServeFiles serves Captcha images and audio
|
|
func ServeFiles(w http.ResponseWriter, r *http.Request) {
|
|
server.ServeHTTP(w, r)
|
|
}
|
|
|
|
// Authenticate check's if a Captcha solution is valid
|
|
func Authenticate(req Captcha) bool {
|
|
return captcha.VerifyString(req.CaptchaID, req.Solution)
|
|
}
|