Skip to content

Commit ade88a8

Browse files
zeripathlafriks
authored andcommitted
Allow Recaptcha service url to be configured (#6820)
1 parent 159294f commit ade88a8

File tree

8 files changed

+20
-5
lines changed

8 files changed

+20
-5
lines changed

custom/conf/app.ini.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ CAPTCHA_TYPE = image
362362
; Go to https://www.google.com/recaptcha/admin to sign up for a key
363363
RECAPTCHA_SECRET =
364364
RECAPTCHA_SITEKEY =
365+
; Change this to use recaptcha.net or other recaptcha service
366+
RECAPTCHA_URL = https://www.google.com/recaptcha/
365367
; Default value for KeepEmailPrivate
366368
; Each new user will get the value of this setting copied into their profile
367369
DEFAULT_KEEP_EMAIL_PRIVATE = false

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
214214
- `CAPTCHA_TYPE`: **image**: \[image, recaptcha\]
215215
- `RECAPTCHA_SECRET`: **""**: Go to https://www.google.com/recaptcha/admin to get a secret for recaptcha.
216216
- `RECAPTCHA_SITEKEY`: **""**: Go to https://www.google.com/recaptcha/admin to get a sitekey for recaptcha.
217+
- `RECAPTCHA_URL`: **https://www.google.com/recaptcha/**: Set the recaptcha url - allows the use of recaptcha net.
217218
- `DEFAULT_ENABLE_DEPENDENCIES`: **true**: Enable this to have dependencies enabled by default.
218219
- `ENABLE_USER_HEATMAP`: **true**: Enable this to display the heatmap on users profiles.
219220
- `EMAIL_DOMAIN_WHITELIST`: **\<empty\>**: If non-empty, list of domain names that can only be used to register

modules/recaptcha/recaptcha.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"code.gitea.io/gitea/modules/setting"
16+
"code.gitea.io/gitea/modules/util"
1617
)
1718

1819
// Response is the structure of JSON returned from API
@@ -23,11 +24,11 @@ type Response struct {
2324
ErrorCodes []string `json:"error-codes"`
2425
}
2526

26-
const apiURL = "https://www.google.com/recaptcha/api/siteverify"
27+
const apiURL = "/api/siteverify"
2728

2829
// Verify calls Google Recaptcha API to verify token
2930
func Verify(response string) (bool, error) {
30-
resp, err := http.PostForm(apiURL,
31+
resp, err := http.PostForm(util.URLJoin(setting.Service.RecaptchaURL, apiURL),
3132
url.Values{"secret": {setting.Service.RecaptchaSecret}, "response": {response}})
3233
if err != nil {
3334
return false, fmt.Errorf("Failed to send CAPTCHA response: %s", err)

modules/setting/service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var Service struct {
3030
CaptchaType string
3131
RecaptchaSecret string
3232
RecaptchaSitekey string
33+
RecaptchaURL string
3334
DefaultKeepEmailPrivate bool
3435
DefaultAllowCreateOrganization bool
3536
EnableTimetracking bool
@@ -63,6 +64,7 @@ func newService() {
6364
Service.CaptchaType = sec.Key("CAPTCHA_TYPE").MustString(ImageCaptcha)
6465
Service.RecaptchaSecret = sec.Key("RECAPTCHA_SECRET").MustString("")
6566
Service.RecaptchaSitekey = sec.Key("RECAPTCHA_SITEKEY").MustString("")
67+
Service.RecaptchaURL = sec.Key("RECAPTCHA_URL").MustString("https://www.google.com/recaptcha/")
6668
Service.DefaultKeepEmailPrivate = sec.Key("DEFAULT_KEEP_EMAIL_PRIVATE").MustBool()
6769
Service.DefaultAllowCreateOrganization = sec.Key("DEFAULT_ALLOW_CREATE_ORGANIZATION").MustBool(true)
6870
Service.EnableTimetracking = sec.Key("ENABLE_TIMETRACKING").MustBool(true)

modules/templates/helper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"strings"
2121
"time"
2222

23+
"code.gitea.io/gitea/modules/util"
24+
2325
"code.gitea.io/gitea/models"
2426
"code.gitea.io/gitea/modules/base"
2527
"code.gitea.io/gitea/modules/log"
@@ -115,6 +117,8 @@ func NewFuncMap() []template.FuncMap {
115117
"EscapePound": func(str string) string {
116118
return strings.NewReplacer("%", "%25", "#", "%23", " ", "%20", "?", "%3F").Replace(str)
117119
},
120+
"PathEscapeSegments": util.PathEscapeSegments,
121+
"URLJoin": util.URLJoin,
118122
"RenderCommitMessage": RenderCommitMessage,
119123
"RenderCommitMessageLink": RenderCommitMessageLink,
120124
"RenderCommitBody": RenderCommitBody,

routers/user/auth.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ func LinkAccount(ctx *context.Context) {
662662
ctx.Data["LinkAccountMode"] = true
663663
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
664664
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
665+
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
665666
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
666667
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
667668
ctx.Data["ShowRegistrationButton"] = false
@@ -710,6 +711,7 @@ func LinkAccountPostSignIn(ctx *context.Context, signInForm auth.SignInForm) {
710711
ctx.Data["LinkAccountMode"] = true
711712
ctx.Data["LinkAccountModeSignIn"] = true
712713
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
714+
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
713715
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
714716
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
715717
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
@@ -778,6 +780,7 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
778780
ctx.Data["LinkAccountMode"] = true
779781
ctx.Data["LinkAccountModeRegister"] = true
780782
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
783+
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
781784
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
782785
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
783786
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
@@ -918,7 +921,7 @@ func SignUp(ctx *context.Context) {
918921
ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
919922

920923
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
921-
924+
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
922925
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
923926
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
924927

@@ -934,7 +937,7 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
934937
ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
935938

936939
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
937-
940+
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
938941
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
939942
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
940943

routers/user/auth_openid.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ func RegisterOpenID(ctx *context.Context) {
312312
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
313313
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
314314
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
315+
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
315316
ctx.Data["OpenID"] = oid
316317
userName, _ := ctx.Session.Get("openid_determined_username").(string)
317318
if userName != "" {
@@ -337,6 +338,7 @@ func RegisterOpenIDPost(ctx *context.Context, cpt *captcha.Captcha, form auth.Si
337338
ctx.Data["PageIsOpenIDRegister"] = true
338339
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
339340
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
341+
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
340342
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
341343
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
342344
ctx.Data["OpenID"] = oid

templates/base/footer.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
{{end}}
4747
{{if .EnableCaptcha}}
4848
{{if eq .CaptchaType "recaptcha"}}
49-
<script src="https://www.google.com/recaptcha/api.js" async></script>
49+
<script src='{{ URLJoin .RecaptchaURL "api.js"}}' async></script>
5050
{{end}}
5151
{{end}}
5252
{{if .RequireTribute}}

0 commit comments

Comments
 (0)