mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-08-03 00:45:22 +02:00
[CHORE] Move captcha library
- This is a fork of https://github.com/dchest/captcha, as https://gitea.com/go-chi/captcha is a fork of github.com/go-macaron/captcha which is a fork (although not properly credited) of a older version of https://github.com/dchest/captcha. Hence why I've just forked the original. - The fork includes some QoL improvements (uses standard library for determistic RNG instead of rolling your own crypto), and removal of audio support (500KiB unused data that bloated the binary otherwise). Flips the image over the x-asis. https://code.forgejo.org/go-chi/captcha/compare/47270f2b55862b38f9f65f615b53c1e04e814ef0..main - This move is needed for the next commit, because gitea.com/go-chi/captcha included the gitea.com/go-chi/cache dependency.
This commit is contained in:
parent
190b5a3859
commit
0404662e99
6 changed files with 83 additions and 17 deletions
|
@ -15,24 +15,47 @@ import (
|
|||
"code.gitea.io/gitea/modules/recaptcha"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/turnstile"
|
||||
mc "gitea.com/go-chi/cache"
|
||||
|
||||
"gitea.com/go-chi/captcha"
|
||||
"code.forgejo.org/go-chi/captcha"
|
||||
)
|
||||
|
||||
var (
|
||||
imageCaptchaOnce sync.Once
|
||||
cpt *captcha.Captcha
|
||||
imageCachePrefix = "captcha:"
|
||||
)
|
||||
|
||||
// GetImageCaptcha returns global image captcha
|
||||
func GetImageCaptcha() *captcha.Captcha {
|
||||
type imageCaptchaStore struct {
|
||||
c mc.Cache
|
||||
}
|
||||
|
||||
func (c *imageCaptchaStore) Set(id string, digits []byte) {
|
||||
if err := c.c.Put(imageCachePrefix+id, string(digits), int64(captcha.Expiration.Seconds())); err != nil {
|
||||
log.Error("Couldn't store captcha cache for %q: %v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *imageCaptchaStore) Get(id string, clear bool) (digits []byte) {
|
||||
val, ok := c.c.Get(imageCachePrefix + id).(string)
|
||||
if !ok {
|
||||
return digits
|
||||
}
|
||||
|
||||
if clear {
|
||||
if err := c.c.Delete(imageCachePrefix + id); err != nil {
|
||||
log.Error("Couldn't delete captcha cache for %q: %v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
return []byte(val)
|
||||
}
|
||||
|
||||
// GetImageCaptcha returns image captcha ID.
|
||||
func GetImageCaptcha() string {
|
||||
imageCaptchaOnce.Do(func() {
|
||||
cpt = captcha.NewCaptcha(captcha.Options{
|
||||
SubURL: setting.AppSubURL,
|
||||
})
|
||||
cpt.Store = cache.GetCache()
|
||||
captcha.SetCustomStore(&imageCaptchaStore{c: cache.GetCache()})
|
||||
})
|
||||
return cpt
|
||||
return captcha.New()
|
||||
}
|
||||
|
||||
// SetCaptchaData sets common captcha data
|
||||
|
@ -52,6 +75,8 @@ func SetCaptchaData(ctx *Context) {
|
|||
}
|
||||
|
||||
const (
|
||||
imgCaptchaIDField = "img-captcha-id"
|
||||
imgCaptchaResponseField = "img-captcha-response"
|
||||
gRecaptchaResponseField = "g-recaptcha-response"
|
||||
hCaptchaResponseField = "h-captcha-response"
|
||||
mCaptchaResponseField = "m-captcha-response"
|
||||
|
@ -69,7 +94,7 @@ func VerifyCaptcha(ctx *Context, tpl base.TplName, form any) {
|
|||
var err error
|
||||
switch setting.Service.CaptchaType {
|
||||
case setting.ImageCaptcha:
|
||||
valid = GetImageCaptcha().VerifyReq(ctx.Req)
|
||||
valid = captcha.VerifyString(ctx.Req.Form.Get(imgCaptchaIDField), ctx.Req.Form.Get(imgCaptchaResponseField))
|
||||
case setting.ReCaptcha:
|
||||
valid, err = recaptcha.Verify(ctx, ctx.Req.Form.Get(gRecaptchaResponseField))
|
||||
case setting.HCaptcha:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue