mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.6 KiB
94 lines
2.6 KiB
4 years ago
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||
2 years ago
|
// SPDX-License-Identifier: MIT
|
||
4 years ago
|
|
||
|
package context
|
||
|
|
||
|
import (
|
||
2 years ago
|
"fmt"
|
||
4 years ago
|
"sync"
|
||
|
|
||
2 years ago
|
"code.gitea.io/gitea/modules/base"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/cache"
|
||
2 years ago
|
"code.gitea.io/gitea/modules/hcaptcha"
|
||
|
"code.gitea.io/gitea/modules/log"
|
||
|
"code.gitea.io/gitea/modules/mcaptcha"
|
||
|
"code.gitea.io/gitea/modules/recaptcha"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
2 years ago
|
"code.gitea.io/gitea/modules/turnstile"
|
||
4 years ago
|
|
||
|
"gitea.com/go-chi/captcha"
|
||
|
)
|
||
|
|
||
3 years ago
|
var (
|
||
|
imageCaptchaOnce sync.Once
|
||
|
cpt *captcha.Captcha
|
||
|
)
|
||
4 years ago
|
|
||
|
// GetImageCaptcha returns global image captcha
|
||
|
func GetImageCaptcha() *captcha.Captcha {
|
||
|
imageCaptchaOnce.Do(func() {
|
||
|
cpt = captcha.NewCaptcha(captcha.Options{
|
||
|
SubURL: setting.AppSubURL,
|
||
|
})
|
||
4 years ago
|
cpt.Store = cache.GetCache()
|
||
4 years ago
|
})
|
||
|
return cpt
|
||
|
}
|
||
2 years ago
|
|
||
|
// 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
|
||
2 years ago
|
ctx.Data["CfTurnstileSitekey"] = setting.Service.CfTurnstileSitekey
|
||
2 years ago
|
}
|
||
|
|
||
|
const (
|
||
2 years ago
|
gRecaptchaResponseField = "g-recaptcha-response"
|
||
|
hCaptchaResponseField = "h-captcha-response"
|
||
|
mCaptchaResponseField = "m-captcha-response"
|
||
|
cfTurnstileResponseField = "cf-turnstile-response"
|
||
2 years ago
|
)
|
||
|
|
||
|
// VerifyCaptcha verifies Captcha data
|
||
|
// No-op if captchas are not enabled
|
||
1 year ago
|
func VerifyCaptcha(ctx *Context, tpl base.TplName, form any) {
|
||
2 years ago
|
if !setting.Service.EnableCaptcha {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var valid bool
|
||
|
var err error
|
||
|
switch setting.Service.CaptchaType {
|
||
|
case setting.ImageCaptcha:
|
||
|
valid = GetImageCaptcha().VerifyReq(ctx.Req)
|
||
|
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))
|
||
2 years ago
|
case setting.CfTurnstile:
|
||
|
valid, err = turnstile.Verify(ctx, ctx.Req.Form.Get(cfTurnstileResponseField))
|
||
2 years ago
|
default:
|
||
|
ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
|
||
|
return
|
||
|
}
|
||
|
if err != nil {
|
||
|
log.Debug("%v", err)
|
||
|
}
|
||
|
|
||
|
if !valid {
|
||
|
ctx.Data["Err_Captcha"] = true
|
||
|
ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tpl, form)
|
||
|
}
|
||
|
}
|