@@ -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 )
0 commit comments