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
Chris MacLeod c9b72206a5 Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds

The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.

* Style changes and old code removal in router

Router needs a lot of work done to its (lack of) error handling.

* Dead code removal and style changes

Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.

* Finish dead code removal and style changes

Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.

* Fix accidental find-and-replace

* Style, error checking, saftey, bug fix changes

* Redo error checking erased during merge

* Re-add merge-erased fix. Make Safe safe.
2017-05-09 21:34:40 -05:00

49 lignes
1 013 o
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 = 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)
}