mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Add new captcha: cloudflare turnstile (#22369)
Added a new captcha(cloudflare turnstile) and its corresponding document. Cloudflare turnstile official instructions are here: https://developers.cloudflare.com/turnstile Signed-off-by: ByLCY <bylcy@bylcy.dev> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Jason Song <i@wolfogre.com>
This commit is contained in:
		@@ -14,6 +14,7 @@ import (
 | 
			
		||||
	"code.gitea.io/gitea/modules/mcaptcha"
 | 
			
		||||
	"code.gitea.io/gitea/modules/recaptcha"
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
	"code.gitea.io/gitea/modules/turnstile"
 | 
			
		||||
 | 
			
		||||
	"gitea.com/go-chi/captcha"
 | 
			
		||||
)
 | 
			
		||||
@@ -47,12 +48,14 @@ func SetCaptchaData(ctx *Context) {
 | 
			
		||||
	ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
 | 
			
		||||
	ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
 | 
			
		||||
	ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
 | 
			
		||||
	ctx.Data["CfTurnstileSitekey"] = setting.Service.CfTurnstileSitekey
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	gRecaptchaResponseField = "g-recaptcha-response"
 | 
			
		||||
	hCaptchaResponseField   = "h-captcha-response"
 | 
			
		||||
	mCaptchaResponseField   = "m-captcha-response"
 | 
			
		||||
	gRecaptchaResponseField  = "g-recaptcha-response"
 | 
			
		||||
	hCaptchaResponseField    = "h-captcha-response"
 | 
			
		||||
	mCaptchaResponseField    = "m-captcha-response"
 | 
			
		||||
	cfTurnstileResponseField = "cf-turnstile-response"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// VerifyCaptcha verifies Captcha data
 | 
			
		||||
@@ -73,6 +76,8 @@ func VerifyCaptcha(ctx *Context, tpl base.TplName, form interface{}) {
 | 
			
		||||
		valid, err = hcaptcha.Verify(ctx, ctx.Req.Form.Get(hCaptchaResponseField))
 | 
			
		||||
	case setting.MCaptcha:
 | 
			
		||||
		valid, err = mcaptcha.Verify(ctx, ctx.Req.Form.Get(mCaptchaResponseField))
 | 
			
		||||
	case setting.CfTurnstile:
 | 
			
		||||
		valid, err = turnstile.Verify(ctx, ctx.Req.Form.Get(cfTurnstileResponseField))
 | 
			
		||||
	default:
 | 
			
		||||
		ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
 | 
			
		||||
		return
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user