From ade88a877d7f3c1fba466359198b8aa8ce5f28f8 Mon Sep 17 00:00:00 2001 From: zeripath Date: Thu, 2 May 2019 14:09:39 +0100 Subject: [PATCH] Allow Recaptcha service url to be configured (#6820) --- custom/conf/app.ini.sample | 2 ++ docs/content/doc/advanced/config-cheat-sheet.en-us.md | 1 + modules/recaptcha/recaptcha.go | 5 +++-- modules/setting/service.go | 2 ++ modules/templates/helper.go | 4 ++++ routers/user/auth.go | 7 +++++-- routers/user/auth_openid.go | 2 ++ templates/base/footer.tmpl | 2 +- 8 files changed, 20 insertions(+), 5 deletions(-) diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index 159ab845b7..04b2b9f92e 100644 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -362,6 +362,8 @@ CAPTCHA_TYPE = image ; Go to https://www.google.com/recaptcha/admin to sign up for a key RECAPTCHA_SECRET = RECAPTCHA_SITEKEY = +; Change this to use recaptcha.net or other recaptcha service +RECAPTCHA_URL = https://www.google.com/recaptcha/ ; Default value for KeepEmailPrivate ; Each new user will get the value of this setting copied into their profile DEFAULT_KEEP_EMAIL_PRIVATE = false diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index 9fe8ef231b..5c37f4f1c4 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -214,6 +214,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`. - `CAPTCHA_TYPE`: **image**: \[image, recaptcha\] - `RECAPTCHA_SECRET`: **""**: Go to https://www.google.com/recaptcha/admin to get a secret for recaptcha. - `RECAPTCHA_SITEKEY`: **""**: Go to https://www.google.com/recaptcha/admin to get a sitekey for recaptcha. +- `RECAPTCHA_URL`: **https://www.google.com/recaptcha/**: Set the recaptcha url - allows the use of recaptcha net. - `DEFAULT_ENABLE_DEPENDENCIES`: **true**: Enable this to have dependencies enabled by default. - `ENABLE_USER_HEATMAP`: **true**: Enable this to display the heatmap on users profiles. - `EMAIL_DOMAIN_WHITELIST`: **\**: If non-empty, list of domain names that can only be used to register diff --git a/modules/recaptcha/recaptcha.go b/modules/recaptcha/recaptcha.go index 1009185961..2d7bb6a5a6 100644 --- a/modules/recaptcha/recaptcha.go +++ b/modules/recaptcha/recaptcha.go @@ -13,6 +13,7 @@ import ( "time" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" ) // Response is the structure of JSON returned from API @@ -23,11 +24,11 @@ type Response struct { ErrorCodes []string `json:"error-codes"` } -const apiURL = "https://www.google.com/recaptcha/api/siteverify" +const apiURL = "/api/siteverify" // Verify calls Google Recaptcha API to verify token func Verify(response string) (bool, error) { - resp, err := http.PostForm(apiURL, + resp, err := http.PostForm(util.URLJoin(setting.Service.RecaptchaURL, apiURL), url.Values{"secret": {setting.Service.RecaptchaSecret}, "response": {response}}) if err != nil { return false, fmt.Errorf("Failed to send CAPTCHA response: %s", err) diff --git a/modules/setting/service.go b/modules/setting/service.go index 08bfb6c414..7e4fb8d7d9 100644 --- a/modules/setting/service.go +++ b/modules/setting/service.go @@ -30,6 +30,7 @@ var Service struct { CaptchaType string RecaptchaSecret string RecaptchaSitekey string + RecaptchaURL string DefaultKeepEmailPrivate bool DefaultAllowCreateOrganization bool EnableTimetracking bool @@ -63,6 +64,7 @@ func newService() { Service.CaptchaType = sec.Key("CAPTCHA_TYPE").MustString(ImageCaptcha) Service.RecaptchaSecret = sec.Key("RECAPTCHA_SECRET").MustString("") Service.RecaptchaSitekey = sec.Key("RECAPTCHA_SITEKEY").MustString("") + Service.RecaptchaURL = sec.Key("RECAPTCHA_URL").MustString("https://www.google.com/recaptcha/") Service.DefaultKeepEmailPrivate = sec.Key("DEFAULT_KEEP_EMAIL_PRIVATE").MustBool() Service.DefaultAllowCreateOrganization = sec.Key("DEFAULT_ALLOW_CREATE_ORGANIZATION").MustBool(true) Service.EnableTimetracking = sec.Key("ENABLE_TIMETRACKING").MustBool(true) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index b6c5cc5945..94e0748872 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -20,6 +20,8 @@ import ( "strings" "time" + "code.gitea.io/gitea/modules/util" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/log" @@ -115,6 +117,8 @@ func NewFuncMap() []template.FuncMap { "EscapePound": func(str string) string { return strings.NewReplacer("%", "%25", "#", "%23", " ", "%20", "?", "%3F").Replace(str) }, + "PathEscapeSegments": util.PathEscapeSegments, + "URLJoin": util.URLJoin, "RenderCommitMessage": RenderCommitMessage, "RenderCommitMessageLink": RenderCommitMessageLink, "RenderCommitBody": RenderCommitBody, diff --git a/routers/user/auth.go b/routers/user/auth.go index 433a4a87dc..b8f697b3ca 100644 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -662,6 +662,7 @@ func LinkAccount(ctx *context.Context) { ctx.Data["LinkAccountMode"] = true ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha ctx.Data["CaptchaType"] = setting.Service.CaptchaType + ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration ctx.Data["ShowRegistrationButton"] = false @@ -710,6 +711,7 @@ func LinkAccountPostSignIn(ctx *context.Context, signInForm auth.SignInForm) { ctx.Data["LinkAccountMode"] = true ctx.Data["LinkAccountModeSignIn"] = true ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha + ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL ctx.Data["CaptchaType"] = setting.Service.CaptchaType ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration @@ -778,6 +780,7 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au ctx.Data["LinkAccountMode"] = true ctx.Data["LinkAccountModeRegister"] = true ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha + ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL ctx.Data["CaptchaType"] = setting.Service.CaptchaType ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration @@ -918,7 +921,7 @@ func SignUp(ctx *context.Context) { ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up" ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha - + ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL ctx.Data["CaptchaType"] = setting.Service.CaptchaType ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey @@ -934,7 +937,7 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up" ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha - + ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL ctx.Data["CaptchaType"] = setting.Service.CaptchaType ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey diff --git a/routers/user/auth_openid.go b/routers/user/auth_openid.go index 5ab9909270..2612f70a67 100644 --- a/routers/user/auth_openid.go +++ b/routers/user/auth_openid.go @@ -312,6 +312,7 @@ func RegisterOpenID(ctx *context.Context) { ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha ctx.Data["CaptchaType"] = setting.Service.CaptchaType ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey + ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL ctx.Data["OpenID"] = oid userName, _ := ctx.Session.Get("openid_determined_username").(string) if userName != "" { @@ -337,6 +338,7 @@ func RegisterOpenIDPost(ctx *context.Context, cpt *captcha.Captcha, form auth.Si ctx.Data["PageIsOpenIDRegister"] = true ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha + ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL ctx.Data["CaptchaType"] = setting.Service.CaptchaType ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey ctx.Data["OpenID"] = oid diff --git a/templates/base/footer.tmpl b/templates/base/footer.tmpl index 2481b2187c..5d1c2e9280 100644 --- a/templates/base/footer.tmpl +++ b/templates/base/footer.tmpl @@ -46,7 +46,7 @@ {{end}} {{if .EnableCaptcha}} {{if eq .CaptchaType "recaptcha"}} - + {{end}} {{end}} {{if .RequireTribute}}