@@ -17,11 +17,8 @@ import (
1717 "code.gitea.io/gitea/modules/base"
1818 "code.gitea.io/gitea/modules/context"
1919 "code.gitea.io/gitea/modules/eventsource"
20- "code.gitea.io/gitea/modules/hcaptcha"
2120 "code.gitea.io/gitea/modules/log"
22- "code.gitea.io/gitea/modules/mcaptcha"
2321 "code.gitea.io/gitea/modules/password"
24- "code.gitea.io/gitea/modules/recaptcha"
2522 "code.gitea.io/gitea/modules/session"
2623 "code.gitea.io/gitea/modules/setting"
2724 "code.gitea.io/gitea/modules/timeutil"
@@ -163,6 +160,10 @@ func SignIn(ctx *context.Context) {
163160 ctx .Data ["PageIsLogin" ] = true
164161 ctx .Data ["EnableSSPI" ] = auth .IsSSPIEnabled ()
165162
163+ if setting .Service .EnableCaptcha && setting .Service .RequireCaptchaForLogin {
164+ context .SetCaptchaData (ctx )
165+ }
166+
166167 ctx .HTML (http .StatusOK , tplSignIn )
167168}
168169
@@ -189,6 +190,16 @@ func SignInPost(ctx *context.Context) {
189190 }
190191
191192 form := web .GetForm (ctx ).(* forms.SignInForm )
193+
194+ if setting .Service .EnableCaptcha && setting .Service .RequireCaptchaForLogin {
195+ context .SetCaptchaData (ctx )
196+
197+ context .VerifyCaptcha (ctx , tplSignIn , form )
198+ if ctx .Written () {
199+ return
200+ }
201+ }
202+
192203 u , source , err := auth_service .UserSignIn (form .UserName , form .Password )
193204 if err != nil {
194205 if user_model .IsErrUserNotExist (err ) || user_model .IsErrEmailAddressNotExist (err ) {
@@ -383,14 +394,7 @@ func SignUp(ctx *context.Context) {
383394
384395 ctx .Data ["SignUpLink" ] = setting .AppSubURL + "/user/sign_up"
385396
386- ctx .Data ["EnableCaptcha" ] = setting .Service .EnableCaptcha
387- ctx .Data ["RecaptchaURL" ] = setting .Service .RecaptchaURL
388- ctx .Data ["Captcha" ] = context .GetImageCaptcha ()
389- ctx .Data ["CaptchaType" ] = setting .Service .CaptchaType
390- ctx .Data ["RecaptchaSitekey" ] = setting .Service .RecaptchaSitekey
391- ctx .Data ["HcaptchaSitekey" ] = setting .Service .HcaptchaSitekey
392- ctx .Data ["McaptchaSitekey" ] = setting .Service .McaptchaSitekey
393- ctx .Data ["McaptchaURL" ] = setting .Service .McaptchaURL
397+ context .SetCaptchaData (ctx )
394398 ctx .Data ["PageIsSignUp" ] = true
395399
396400 // Show Disabled Registration message if DisableRegistration or AllowOnlyExternalRegistration options are true
@@ -406,14 +410,7 @@ func SignUpPost(ctx *context.Context) {
406410
407411 ctx .Data ["SignUpLink" ] = setting .AppSubURL + "/user/sign_up"
408412
409- ctx .Data ["EnableCaptcha" ] = setting .Service .EnableCaptcha
410- ctx .Data ["RecaptchaURL" ] = setting .Service .RecaptchaURL
411- ctx .Data ["Captcha" ] = context .GetImageCaptcha ()
412- ctx .Data ["CaptchaType" ] = setting .Service .CaptchaType
413- ctx .Data ["RecaptchaSitekey" ] = setting .Service .RecaptchaSitekey
414- ctx .Data ["HcaptchaSitekey" ] = setting .Service .HcaptchaSitekey
415- ctx .Data ["McaptchaSitekey" ] = setting .Service .McaptchaSitekey
416- ctx .Data ["McaptchaURL" ] = setting .Service .McaptchaURL
413+ context .SetCaptchaData (ctx )
417414 ctx .Data ["PageIsSignUp" ] = true
418415
419416 // Permission denied if DisableRegistration or AllowOnlyExternalRegistration options are true
@@ -427,31 +424,9 @@ func SignUpPost(ctx *context.Context) {
427424 return
428425 }
429426
430- if setting .Service .EnableCaptcha {
431- var valid bool
432- var err error
433- switch setting .Service .CaptchaType {
434- case setting .ImageCaptcha :
435- valid = context .GetImageCaptcha ().VerifyReq (ctx .Req )
436- case setting .ReCaptcha :
437- valid , err = recaptcha .Verify (ctx , form .GRecaptchaResponse )
438- case setting .HCaptcha :
439- valid , err = hcaptcha .Verify (ctx , form .HcaptchaResponse )
440- case setting .MCaptcha :
441- valid , err = mcaptcha .Verify (ctx , form .McaptchaResponse )
442- default :
443- ctx .ServerError ("Unknown Captcha Type" , fmt .Errorf ("Unknown Captcha Type: %s" , setting .Service .CaptchaType ))
444- return
445- }
446- if err != nil {
447- log .Debug ("%s" , err .Error ())
448- }
449-
450- if ! valid {
451- ctx .Data ["Err_Captcha" ] = true
452- ctx .RenderWithErr (ctx .Tr ("form.captcha_incorrect" ), tplSignUp , & form )
453- return
454- }
427+ context .VerifyCaptcha (ctx , tplSignUp , form )
428+ if ctx .Written () {
429+ return
455430 }
456431
457432 if ! form .IsEmailDomainAllowed () {
0 commit comments