2021-01-26 15:36:53 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-01-26 15:36:53 +00:00
|
|
|
|
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2022-11-22 21:13:18 +00:00
|
|
|
"fmt"
|
2021-01-26 15:36:53 +00:00
|
|
|
"sync"
|
|
|
|
|
2022-11-22 21:13:18 +00:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
2021-01-27 14:56:54 +00:00
|
|
|
"code.gitea.io/gitea/modules/cache"
|
2022-11-22 21:13:18 +00:00
|
|
|
"code.gitea.io/gitea/modules/hcaptcha"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/mcaptcha"
|
|
|
|
"code.gitea.io/gitea/modules/recaptcha"
|
2021-01-26 15:36:53 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2023-02-05 07:29:03 +00:00
|
|
|
"code.gitea.io/gitea/modules/turnstile"
|
2021-01-26 15:36:53 +00:00
|
|
|
|
2024-08-26 23:57:40 +00:00
|
|
|
mc "code.forgejo.org/go-chi/cache"
|
2024-08-26 23:47:10 +00:00
|
|
|
"code.forgejo.org/go-chi/captcha"
|
2021-01-26 15:36:53 +00:00
|
|
|
)
|
|
|
|
|
2022-01-20 17:46:10 +00:00
|
|
|
var (
|
|
|
|
imageCaptchaOnce sync.Once
|
2024-08-26 23:47:10 +00:00
|
|
|
imageCachePrefix = "captcha:"
|
2022-01-20 17:46:10 +00:00
|
|
|
)
|
2021-01-26 15:36:53 +00:00
|
|
|
|
2024-08-26 23:47:10 +00:00
|
|
|
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 {
|
2021-01-26 15:36:53 +00:00
|
|
|
imageCaptchaOnce.Do(func() {
|
2024-08-26 23:47:10 +00:00
|
|
|
captcha.SetCustomStore(&imageCaptchaStore{c: cache.GetCache()})
|
2021-01-26 15:36:53 +00:00
|
|
|
})
|
2024-08-26 23:47:10 +00:00
|
|
|
return captcha.New()
|
2021-01-26 15:36:53 +00:00
|
|
|
}
|
2022-11-22 21:13:18 +00:00
|
|
|
|
|
|
|
// SetCaptchaData sets common captcha data
|
|
|
|
func SetCaptchaData(ctx *Context) {
|
|
|
|
if !setting.Service.EnableCaptcha {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
|
|
|
|
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
|
|
|
|
ctx.Data["Captcha"] = GetImageCaptcha()
|
|
|
|
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
|
|
|
|
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
|
|
|
|
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
|
|
|
|
ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
|
|
|
|
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
|
2023-02-05 07:29:03 +00:00
|
|
|
ctx.Data["CfTurnstileSitekey"] = setting.Service.CfTurnstileSitekey
|
2022-11-22 21:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2024-08-26 23:47:10 +00:00
|
|
|
imgCaptchaIDField = "img-captcha-id"
|
|
|
|
imgCaptchaResponseField = "img-captcha-response"
|
2023-02-05 07:29:03 +00:00
|
|
|
gRecaptchaResponseField = "g-recaptcha-response"
|
|
|
|
hCaptchaResponseField = "h-captcha-response"
|
|
|
|
mCaptchaResponseField = "m-captcha-response"
|
|
|
|
cfTurnstileResponseField = "cf-turnstile-response"
|
2022-11-22 21:13:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// VerifyCaptcha verifies Captcha data
|
|
|
|
// No-op if captchas are not enabled
|
2023-07-04 18:36:08 +00:00
|
|
|
func VerifyCaptcha(ctx *Context, tpl base.TplName, form any) {
|
2022-11-22 21:13:18 +00:00
|
|
|
if !setting.Service.EnableCaptcha {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var valid bool
|
|
|
|
var err error
|
|
|
|
switch setting.Service.CaptchaType {
|
|
|
|
case setting.ImageCaptcha:
|
2024-08-26 23:47:10 +00:00
|
|
|
valid = captcha.VerifyString(ctx.Req.Form.Get(imgCaptchaIDField), ctx.Req.Form.Get(imgCaptchaResponseField))
|
2022-11-22 21:13:18 +00:00
|
|
|
case setting.ReCaptcha:
|
|
|
|
valid, err = recaptcha.Verify(ctx, ctx.Req.Form.Get(gRecaptchaResponseField))
|
|
|
|
case setting.HCaptcha:
|
|
|
|
valid, err = hcaptcha.Verify(ctx, ctx.Req.Form.Get(hCaptchaResponseField))
|
|
|
|
case setting.MCaptcha:
|
|
|
|
valid, err = mcaptcha.Verify(ctx, ctx.Req.Form.Get(mCaptchaResponseField))
|
2023-02-05 07:29:03 +00:00
|
|
|
case setting.CfTurnstile:
|
|
|
|
valid, err = turnstile.Verify(ctx, ctx.Req.Form.Get(cfTurnstileResponseField))
|
2022-11-22 21:13:18 +00:00
|
|
|
default:
|
2024-04-07 11:17:06 +00:00
|
|
|
ctx.ServerError("Unknown Captcha Type", fmt.Errorf("unknown Captcha Type: %s", setting.Service.CaptchaType))
|
2022-11-22 21:13:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2024-04-07 11:17:06 +00:00
|
|
|
log.Debug("Captcha Verify failed: %v", err)
|
2022-11-22 21:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
ctx.Data["Err_Captcha"] = true
|
|
|
|
ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tpl, form)
|
|
|
|
}
|
|
|
|
}
|