Skip to content

Commit f035dcd

Browse files
Fluftechknowlogick
Fluf
authored andcommitted
Add Recaptcha functionality to Gitea (#4044)
1 parent 54fedd4 commit f035dcd

File tree

13 files changed

+163
-15
lines changed

13 files changed

+163
-15
lines changed

custom/conf/app.ini.sample

+7-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,13 @@ ENABLE_NOTIFY_MAIL = false
301301
ENABLE_REVERSE_PROXY_AUTHENTICATION = false
302302
ENABLE_REVERSE_PROXY_AUTO_REGISTRATION = false
303303
; Enable captcha validation for registration
304-
ENABLE_CAPTCHA = true
304+
ENABLE_CAPTCHA = false
305+
; Type of captcha you want to use. Options: image, recaptcha
306+
CAPTCHA_TYPE = image
307+
; Enable recaptcha to use Google's recaptcha service
308+
; Go to https://www.google.com/recaptcha/admin to sign up for a key
309+
RECAPTCHA_SECRET =
310+
RECAPTCHA_SITEKEY =
305311
; Default value for KeepEmailPrivate
306312
; Each new user will get the value of this setting copied into their profile
307313
DEFAULT_KEEP_EMAIL_PRIVATE = false

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
177177
- `ENABLE_REVERSE_PROXY_AUTHENTICATION`: **false**: Enable this to allow reverse proxy authentication.
178178
- `ENABLE_REVERSE_PROXY_AUTO_REGISTRATION`: **false**: Enable this to allow auto-registration
179179
for reverse authentication.
180-
- `ENABLE_CAPTCHA`: **true**: Enable this to use captcha validation for registration.
180+
- `ENABLE_CAPTCHA`: **false**: Enable this to use captcha validation for registration.
181+
- `CAPTCHA_TYPE`: **image**: \[image, recaptcha\]
182+
- `RECAPTCHA_SECRET`: **""**: Go to https://www.google.com/recaptcha/admin to get a secret for recaptcha
183+
- `RECAPTCHA_SITEKEY`: **""**: Go to https://www.google.com/recaptcha/admin to get a sitekey for recaptcha
181184

182185
## Webhook (`webhook`)
183186

modules/auth/user_form.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ func (f *InstallForm) Validate(ctx *macaron.Context, errs binding.Errors) bindin
7272

7373
// RegisterForm form for registering
7474
type RegisterForm struct {
75-
UserName string `binding:"Required;AlphaDashDot;MaxSize(35)"`
76-
Email string `binding:"Required;Email;MaxSize(254)"`
77-
Password string `binding:"Required;MaxSize(255)"`
78-
Retype string
75+
UserName string `binding:"Required;AlphaDashDot;MaxSize(35)"`
76+
Email string `binding:"Required;Email;MaxSize(254)"`
77+
Password string `binding:"Required;MaxSize(255)"`
78+
Retype string
79+
GRecaptchaResponse string `form:"g-recaptcha-response"`
7980
}
8081

8182
// Validate valideates the fields

modules/auth/user_form_auth_openid.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ func (f *SignInOpenIDForm) Validate(ctx *macaron.Context, errs binding.Errors) b
2222

2323
// SignUpOpenIDForm form for signin up with OpenID
2424
type SignUpOpenIDForm struct {
25-
UserName string `binding:"Required;AlphaDashDot;MaxSize(35)"`
26-
Email string `binding:"Required;Email;MaxSize(254)"`
25+
UserName string `binding:"Required;AlphaDashDot;MaxSize(35)"`
26+
Email string `binding:"Required;Email;MaxSize(254)"`
27+
GRecaptchaResponse string `form:"g-recaptcha-response"`
2728
}
2829

2930
// Validate valideates the fields

modules/recaptcha/recaptcha.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package recaptcha
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
"io/ioutil"
11+
"net/http"
12+
"net/url"
13+
"time"
14+
15+
"code.gitea.io/gitea/modules/setting"
16+
)
17+
18+
// Response is the structure of JSON returned from API
19+
type Response struct {
20+
Success bool `json:"success"`
21+
ChallengeTS time.Time `json:"challenge_ts"`
22+
Hostname string `json:"hostname"`
23+
ErrorCodes []string `json:"error-codes"`
24+
}
25+
26+
const apiURL = "https://www.google.com/recaptcha/api/siteverify"
27+
28+
// Verify calls Google Recaptcha API to verify token
29+
func Verify(response string) (bool, error) {
30+
resp, err := http.PostForm(apiURL,
31+
url.Values{"secret": {setting.Service.RecaptchaSecret}, "response": {response}})
32+
if err != nil {
33+
return false, fmt.Errorf("Failed to send CAPTCHA response: %s", err)
34+
}
35+
defer resp.Body.Close()
36+
body, err := ioutil.ReadAll(resp.Body)
37+
if err != nil {
38+
return false, fmt.Errorf("Failed to read CAPTCHA response: %s", err)
39+
}
40+
var jsonResponse Response
41+
err = json.Unmarshal(body, &jsonResponse)
42+
if err != nil {
43+
return false, fmt.Errorf("Failed to parse CAPTCHA response: %s", err)
44+
}
45+
46+
return jsonResponse.Success, nil
47+
}

modules/setting/setting.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ const (
7575
RepoCreatingPublic = "public"
7676
)
7777

78+
// enumerates all the types of captchas
79+
const (
80+
ImageCaptcha = "image"
81+
ReCaptcha = "recaptcha"
82+
)
83+
7884
// settings
7985
var (
8086
// AppVer settings
@@ -1165,6 +1171,9 @@ var Service struct {
11651171
EnableReverseProxyAuth bool
11661172
EnableReverseProxyAutoRegister bool
11671173
EnableCaptcha bool
1174+
CaptchaType string
1175+
RecaptchaSecret string
1176+
RecaptchaSitekey string
11681177
DefaultKeepEmailPrivate bool
11691178
DefaultAllowCreateOrganization bool
11701179
EnableTimetracking bool
@@ -1189,7 +1198,10 @@ func newService() {
11891198
Service.RequireSignInView = sec.Key("REQUIRE_SIGNIN_VIEW").MustBool()
11901199
Service.EnableReverseProxyAuth = sec.Key("ENABLE_REVERSE_PROXY_AUTHENTICATION").MustBool()
11911200
Service.EnableReverseProxyAutoRegister = sec.Key("ENABLE_REVERSE_PROXY_AUTO_REGISTRATION").MustBool()
1192-
Service.EnableCaptcha = sec.Key("ENABLE_CAPTCHA").MustBool()
1201+
Service.EnableCaptcha = sec.Key("ENABLE_CAPTCHA").MustBool(false)
1202+
Service.CaptchaType = sec.Key("CAPTCHA_TYPE").MustString(ImageCaptcha)
1203+
Service.RecaptchaSecret = sec.Key("RECAPTCHA_SECRET").MustString("")
1204+
Service.RecaptchaSitekey = sec.Key("RECAPTCHA_SITEKEY").MustString("")
11931205
Service.DefaultKeepEmailPrivate = sec.Key("DEFAULT_KEEP_EMAIL_PRIVATE").MustBool()
11941206
Service.DefaultAllowCreateOrganization = sec.Key("DEFAULT_ALLOW_CREATE_ORGANIZATION").MustBool(true)
11951207
Service.EnableTimetracking = sec.Key("ENABLE_TIMETRACKING").MustBool(true)

public/css/index.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/less/_form.less

+17
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,23 @@
8080
}
8181
}
8282
}
83+
84+
@media only screen and (min-width: 768px) {
85+
.g-recaptcha {
86+
margin: 0 auto !important;
87+
width: 304px;
88+
padding-left: 30px;
89+
}
90+
}
91+
@media screen and (max-height: 575px){
92+
#rc-imageselect, .g-recaptcha {
93+
transform:scale(0.77);
94+
-webkit-transform:scale(0.77);
95+
transform-origin:0 0;
96+
-webkit-transform-origin:0 0;
97+
}
98+
}
99+
83100
.user.activate,
84101
.user.forgot.password,
85102
.user.reset.password,

routers/user/auth.go

+33-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"code.gitea.io/gitea/modules/base"
1818
"code.gitea.io/gitea/modules/context"
1919
"code.gitea.io/gitea/modules/log"
20+
"code.gitea.io/gitea/modules/recaptcha"
2021
"code.gitea.io/gitea/modules/setting"
2122
"code.gitea.io/gitea/modules/util"
2223

@@ -641,6 +642,8 @@ func LinkAccount(ctx *context.Context) {
641642
ctx.Data["Title"] = ctx.Tr("link_account")
642643
ctx.Data["LinkAccountMode"] = true
643644
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
645+
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
646+
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
644647
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
645648
ctx.Data["ShowRegistrationButton"] = false
646649

@@ -666,6 +669,8 @@ func LinkAccountPostSignIn(ctx *context.Context, signInForm auth.SignInForm) {
666669
ctx.Data["LinkAccountMode"] = true
667670
ctx.Data["LinkAccountModeSignIn"] = true
668671
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
672+
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
673+
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
669674
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
670675
ctx.Data["ShowRegistrationButton"] = false
671676

@@ -732,6 +737,8 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
732737
ctx.Data["LinkAccountMode"] = true
733738
ctx.Data["LinkAccountModeRegister"] = true
734739
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
740+
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
741+
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
735742
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
736743
ctx.Data["ShowRegistrationButton"] = false
737744

@@ -755,12 +762,21 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
755762
return
756763
}
757764

758-
if setting.Service.EnableCaptcha && !cpt.VerifyReq(ctx.Req) {
765+
if setting.Service.EnableCaptcha && setting.Service.CaptchaType == setting.ImageCaptcha && !cpt.VerifyReq(ctx.Req) {
759766
ctx.Data["Err_Captcha"] = true
760767
ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplLinkAccount, &form)
761768
return
762769
}
763770

771+
if setting.Service.EnableCaptcha && setting.Service.CaptchaType == setting.ReCaptcha {
772+
valid, _ := recaptcha.Verify(form.GRecaptchaResponse)
773+
if !valid {
774+
ctx.Data["Err_Captcha"] = true
775+
ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplLinkAccount, &form)
776+
return
777+
}
778+
}
779+
764780
if (len(strings.TrimSpace(form.Password)) > 0 || len(strings.TrimSpace(form.Retype)) > 0) && form.Password != form.Retype {
765781
ctx.Data["Err_Password"] = true
766782
ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplLinkAccount, &form)
@@ -858,6 +874,9 @@ func SignUp(ctx *context.Context) {
858874

859875
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
860876

877+
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
878+
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
879+
861880
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
862881

863882
ctx.HTML(200, tplSignUp)
@@ -871,6 +890,9 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
871890

872891
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
873892

893+
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
894+
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
895+
874896
//Permission denied if DisableRegistration or AllowOnlyExternalRegistration options are true
875897
if !setting.Service.ShowRegistrationButton {
876898
ctx.Error(403)
@@ -882,12 +904,21 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
882904
return
883905
}
884906

885-
if setting.Service.EnableCaptcha && !cpt.VerifyReq(ctx.Req) {
907+
if setting.Service.EnableCaptcha && setting.Service.CaptchaType == setting.ImageCaptcha && !cpt.VerifyReq(ctx.Req) {
886908
ctx.Data["Err_Captcha"] = true
887909
ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplSignUp, &form)
888910
return
889911
}
890912

913+
if setting.Service.EnableCaptcha && setting.Service.CaptchaType == setting.ReCaptcha {
914+
valid, _ := recaptcha.Verify(form.GRecaptchaResponse)
915+
if !valid {
916+
ctx.Data["Err_Captcha"] = true
917+
ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplSignUp, &form)
918+
return
919+
}
920+
}
921+
891922
if form.Password != form.Retype {
892923
ctx.Data["Err_Password"] = true
893924
ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplSignUp, &form)

routers/user/auth_openid.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/modules/context"
1616
"code.gitea.io/gitea/modules/generate"
1717
"code.gitea.io/gitea/modules/log"
18+
"code.gitea.io/gitea/modules/recaptcha"
1819
"code.gitea.io/gitea/modules/setting"
1920

2021
"github.com/go-macaron/captcha"
@@ -308,6 +309,8 @@ func RegisterOpenID(ctx *context.Context) {
308309
ctx.Data["PageIsOpenIDRegister"] = true
309310
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
310311
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
312+
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
313+
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
311314
ctx.Data["OpenID"] = oid
312315
userName, _ := ctx.Session.Get("openid_determined_username").(string)
313316
if userName != "" {
@@ -333,14 +336,26 @@ func RegisterOpenIDPost(ctx *context.Context, cpt *captcha.Captcha, form auth.Si
333336
ctx.Data["PageIsOpenIDRegister"] = true
334337
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
335338
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
339+
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
340+
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
336341
ctx.Data["OpenID"] = oid
337342

338-
if setting.Service.EnableCaptcha && !cpt.VerifyReq(ctx.Req) {
343+
if setting.Service.EnableCaptcha && setting.Service.CaptchaType == setting.ImageCaptcha && !cpt.VerifyReq(ctx.Req) {
339344
ctx.Data["Err_Captcha"] = true
340345
ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplSignUpOID, &form)
341346
return
342347
}
343348

349+
if setting.Service.EnableCaptcha && setting.Service.CaptchaType == setting.ReCaptcha {
350+
ctx.Req.ParseForm()
351+
valid, _ := recaptcha.Verify(form.GRecaptchaResponse)
352+
if !valid {
353+
ctx.Data["Err_Captcha"] = true
354+
ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplSignUpOID, &form)
355+
return
356+
}
357+
}
358+
344359
len := setting.MinPasswordLength
345360
if len < 256 {
346361
len = 256

templates/base/footer.tmpl

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
{{if .RequireU2F}}
6868
<script src="{{AppSubUrl}}/vendor/plugins/u2f/index.js"></script>
6969
{{end}}
70+
{{if .EnableCaptcha}}
71+
{{if eq .CaptchaType "recaptcha"}}
72+
<script src="https://www.google.com/recaptcha/api.js" async></script>
73+
{{end}}
74+
{{end}}
7075
{{if .RequireTribute}}
7176
<script src="{{AppSubUrl}}/vendor/plugins/tribute/tribute.min.js"></script>
7277

templates/user/auth/signup_inner.tmpl

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<label for="retype">{{.i18n.Tr "re_type"}}</label>
3030
<input id="retype" name="retype" type="password" value="{{.retype}}" autocomplete="off" required>
3131
</div>
32-
{{if .EnableCaptcha}}
32+
{{if and .EnableCaptcha (eq .CaptchaType "image")}}
3333
<div class="inline field">
3434
<label></label>
3535
{{.Captcha.CreateHtml}}
@@ -39,6 +39,11 @@
3939
<input id="captcha" name="captcha" value="{{.captcha}}" autocomplete="off">
4040
</div>
4141
{{end}}
42+
{{if and .EnableCaptcha (eq .CaptchaType "recaptcha")}}
43+
<div class="inline field required">
44+
<div class="g-recaptcha" data-sitekey="{{ .RecaptchaSitekey }}"></div>
45+
</div>
46+
{{end}}
4247

4348
<div class="inline field">
4449
<label></label>

templates/user/auth/signup_openid_register.tmpl

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<label for="email">{{.i18n.Tr "email"}}</label>
2121
<input id="email" name="email" type="email" value="{{.email}}" required>
2222
</div>
23-
{{if .EnableCaptcha}}
23+
{{if and .EnableCaptcha (eq .CaptchaType "image")}}
2424
<div class="inline field">
2525
<label></label>
2626
{{.Captcha.CreateHtml}}
@@ -30,6 +30,11 @@
3030
<input id="captcha" name="captcha" value="{{.captcha}}" autocomplete="off">
3131
</div>
3232
{{end}}
33+
{{if and .EnableCaptcha (eq .CaptchaType "recaptcha")}}
34+
<div class="inline field required">
35+
<div class="g-recaptcha" data-sitekey="{{ .RecaptchaSitekey }}"></div>
36+
</div>
37+
{{end}}
3338
<div class="inline field">
3439
<label for="openid">OpenID URI</label>
3540
<input id="openid" value="{{ .OpenID }}" readonly>

0 commit comments

Comments
 (0)