@@ -503,6 +503,151 @@ <h4>OAuth2 with Discord<a class="headerlink" href="#oauth2-with-discord" title="
503503Discord username as the first name and discriminator as the last name.</ p >
504504</ div >
505505</ section >
506+ < hr >
507+ < section id ="captcha ">
508+ < h3 > Authentication with CAPTCHA< a class ="headerlink " href ="#captcha " title ="Link to this heading "> </ a > </ h3 >
509+
510+ < p > CAPTCHAs are essential security measures that prevent automated bot abuse on public forms. To implement Google reCAPTCHA or hCAPTCHA in your authentication form, follow these steps:</ p >
511+
512+ < section id ="reCAPTCHA ">
513+ < h4 > Enabling reCAPTCHA< a class ="headerlink " href ="#reCAPTCHA " title ="Link to this heading "> </ a > </ h4 >
514+ < p > in < code class ="docutils literal notranslate "> settings.py</ code > add your keys:</ p >
515+ < div class ="highlight-python notranslate ">
516+ < div class ="highlight ">
517+ < pre >
518+ RECAPTCHA_API_SECRET_V3 = "your_recaptcha_secret_key_v3"
519+ RECAPTCHA_API_KEY_V3 = "your_recaptcha_site_key_v3"
520+
521+ RECAPTCHA_API_KEY_V2 = "your_recaptcha_site_key_v2"
522+ RECAPTCHA_API_SECRET_V2 = "your_recaptcha_secret_key_v2"
523+
524+ </ pre >
525+ </ div >
526+ </ div >
527+
528+ < p > in < code class ="docutils literal notranslate "> common.py</ code > add:</ p >
529+
530+ < div class ="highlight-python notranslate ">
531+ < div class ="highlight ">
532+ < pre >
533+ #import the functionality
534+ < span class ="kn "> from</ span > < span class ="nn "> . </ span > < span class ="kn "> import</ span > settings
535+ < span class ="kn "> from</ span > < span class ="nn "> py4web.utils.recaptcha </ span > < span class ="kn "> import</ span > ReCaptcha
536+
537+ # < span class ="s2 "> for recaptcha v3</ span >
538+ < span class ="nf "> recaptcha</ span > = < span class ="nn "> ReCaptcha</ span > (< span class ="nd "> settings</ span > .RECAPTCHA_API_KEY_V3, < span class ="nd "> settings</ span > .RECAPTCHA_API_SECRET_V3, "v3")
539+ or
540+ # < span class ="s2 "> for recaptcha v2</ span >
541+ < span class ="nf "> recaptcha</ span > = < span class ="nn "> ReCaptcha</ span > (< span class ="nd "> settings</ span > .RECAPTCHA_API_KEY_V2, < span class ="nd "> settings</ span > .RECAPTCHA_API_SECRET_V2, "v2")
542+
543+
544+ # in the section that auth is defined
545+ # Example:
546+ auth = Auth(session, db, define_tables=False)
547+
548+ # Add this line at the end of auth declaration to enable recaptcha on login, register and request_reset_password forms.
549+ # or enable it on the action that you want by especifying the action name
550+
551+ #Example:
552+
553+ auth.extra_form_fields = {"login": [< span class ="nf "> recaptcha</ span > .field], "register": [< span class ="nf "> recaptcha</ span > .field], "request_reset_password": [< span class ="nf "> recaptcha</ span > .field], }
554+
555+
556+ #In section where auth is enabled, add the recaptcha fixture
557+ # Example:
558+
559+ # #######################################################
560+ # Enable authentication
561+ # #######################################################
562+ auth.enable(uses=(session, T, db, < span class ="nf "> recaptcha</ span > .fixture),env=dict(T=T))
563+
564+ </ pre >
565+
566+ </ div >
567+
568+ </ div >
569+ < p > in < code class ="docutils literal notranslate "> auth.html</ code > use:</ p >
570+ < div class ="highlight-python notranslate ">
571+ < div class ="highlight ">
572+ < pre >
573+ [[try:]]
574+ [[=form]]
575+ [[except:]]
576+ [[pass]]
577+ [[=recaptcha]]
578+
579+ </ pre >
580+ </ div >
581+ </ div >
582+ < p >
583+ After completing these steps, the reCAPTCHA field will be added to the login, register, and request_reset_password forms.
584+ </ p >
585+ </ section >
586+ < hr >
587+ < section id ="hCAPTCHA ">
588+ < h4 > Enabling hCAPTCHA< a class ="headerlink " href ="#hCAPTCHA " title ="Link to this heading "> </ a > </ h4 >
589+ < p > in < code class ="docutils literal notranslate "> settings.py</ code > add your HCAPTCHA_SITE_KEY and HCAPTCHA_SECRET_KEY:</ p >
590+ < div class ="highlight-python notranslate ">
591+ < div class ="highlight ">
592+ < pre >
593+ HCAPTCHA_SITE_KEY = "your_hcaptcha_site_key"
594+ HCAPTCHA_SECRET_KEY = "your_hcaptcha_secret_key"
595+ </ pre >
596+ </ div >
597+ </ div >
598+ < p > in < code class ="docutils literal notranslate "> common.py</ code > add:</ p >
599+ < div class ="highlight-python notranslate ">
600+ < div class ="highlight ">
601+ < pre >
602+ #import the functionality
603+ < span class ="kn "> from</ span > < span class ="nn "> . </ span > < span class ="kn "> import</ span > settings
604+ < span class ="kn "> from</ span > < span class ="nn "> py4web.utils.hcaptcha </ span > < span class ="kn "> import</ span > Hcaptcha
605+
606+ < span class ="nf "> hcaptcha</ span > = < span class ="nn "> Hcaptcha</ span > (< span class ="nd "> settings</ span > .HCAPTCHA_SITE_KEY, < span class ="nd "> settings</ span > .HCAPTCHA_SECRET_KEY)
607+
608+
609+ # in the section that auth is defined
610+ # Example:
611+ auth = Auth(session, db, define_tables=False)
612+
613+ # Add this line at the end of auth declaration to enable hcaptcha on login, register and request_reset_password forms.
614+ # or enable it on the action that you want by especifying the action name
615+
616+ #Example:
617+
618+ auth.extra_form_fields = {"login": [< span class ="nf "> hcaptcha</ span > .field], "register": [< span class ="nf "> hcaptcha</ span > .field], "request_reset_password": [< span class ="nf "> hcaptcha</ span > .field], }
619+
620+
621+ #In section where auth is enabled, add the hcaptcha fixture
622+ # Example:
623+
624+ # #######################################################
625+ # Enable authentication
626+ # #######################################################
627+ auth.enable(uses=(session, T, db, < span class ="nf "> hcaptcha</ span > .fixture),env=dict(T=T))
628+
629+ </ pre >
630+
631+ </ div >
632+
633+ </ div >
634+ < p > in < code class ="docutils literal notranslate "> auth.html</ code > use:</ p >
635+ < div class ="highlight-python notranslate ">
636+ < div class ="highlight ">
637+ < pre >
638+ [[try:]]
639+ [[=form]]
640+ [[except:]]
641+ [[pass]]
642+ [[=hcaptcha]]
643+
644+ </ pre >
645+ </ div >
646+ </ div >
647+ < p >
648+ After completing these steps, the hCAPTCHA field will be added to the login, register, and request_reset_password forms.
649+ </ p >
650+ </ section >
506651</ section >
507652< section id ="auth-api-plugins ">
508653< h3 > Auth API Plugins< a class ="headerlink " href ="#auth-api-plugins " title ="Link to this heading "> </ a > </ h3 >
0 commit comments