Skip to content

Commit 0f71515

Browse files
authored
Update chapter-13.rst (#1015)
Added Hcaptcha, ReCaptcha documentation. Added a example about how to trigger functions after Auth events.
1 parent b841840 commit 0f71515

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

docs/chapter-13.rst

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,175 @@ The second one forces the login if needed:
150150
Here ``@action.uses(auth.user)`` tells py4web that this action requires
151151
a logged in user and should redirect to login if no user is logged in.
152152

153+
Custom actions after Auth events
154+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
155+
156+
After every Auth event, like: password_reset, login, register, verify_email, etc, it is possible to trigger an action.
157+
For exmaple, to redirect a user to specific page after sign up and successfully email verification, we can do the following:
158+
159+
in ``common.py``
160+
.. code:: python
161+
#function triggered after a sign up with email verification sign up.
162+
def after_register_callback(_, user_row):
163+
redirect(URL('pending_registration'))
164+
165+
#function triggered after a successfull email verification.
166+
def after_verify_email_callback(_, user_row):
167+
redirect(URL('success_verification'))
168+
169+
170+
In ``Auth `` section before auth.definetables() or auth.fix_actions(), add:
171+
172+
.. code:: python
173+
# custom action after email verification
174+
auth.on_accept['verify_email'] = after_verify_email_callback
175+
auth.on_accept['register'] = after_register_callback
176+
177+
178+
Example:
179+
180+
.. code:: python
181+
# #######################################################
182+
# Instantiate the object and actions that handle auth
183+
# #######################################################
184+
auth = Auth(session, db, define_tables=False)
185+
auth.use_username = False
186+
auth.param.registration_requires_confirmation = settings.VERIFY_EMAIL #False
187+
auth.param.registration_requires_approval = settings.REQUIRES_APPROVAL #False
188+
auth.param.login_after_registration = settings.LOGIN_AFTER_REGISTRATION #False
189+
auth.param.allowed_actions = settings.ALLOWED_ACTIONS
190+
auth.param.login_expiration_time = 3600
191+
auth.param.password_complexity = {"entropy": 50}
192+
auth.param.block_previous_password_num = 3
193+
auth.param.default_login_enabled = settings.DEFAULT_LOGIN_ENABLED #True
194+
195+
auth.on_accept['verify_email'] = after_verify_email_callback
196+
auth.on_accept['register'] = after_register_callback
197+
198+
auth.define_tables()
199+
auth.fix_actions()
200+
201+
202+
203+
Authentication with CAPTCHA
204+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
205+
206+
CAPTCHAs are essential security measures that prevent automated bot abuse on public forms.
207+
To implement Google reCAPTCHA or hCAPTCHA in your authentication form, follow these steps:
208+
209+
Enabling Google reCAPTCHA
210+
^^^^^^^^^^^^^^^^^^^^^^^^^
211+
212+
In ``settings.py`` add your keys:
213+
214+
.. code:: python
215+
216+
RECAPTCHA_API_SECRET_V3 = "your_recaptcha_secret_key_v3"
217+
RECAPTCHA_API_KEY_V3 = "your_recaptcha_site_key_v3"
218+
219+
RECAPTCHA_API_KEY_V2 = "your_recaptcha_site_key_v2"
220+
RECAPTCHA_API_SECRET_V2 = "your_recaptcha_secret_key_v2"
221+
222+
223+
In ``common.py`` add:
224+
225+
.. code:: python
226+
227+
#import the functionality
228+
from . import settings
229+
from py4web.utils.recaptcha import ReCaptcha
230+
231+
# To use recaptcha v3
232+
recaptcha = ReCaptcha(settings.RECAPTCHA_API_KEY_V3, settings.RECAPTCHA_API_SECRET_V3, "v3")
233+
or
234+
# To use recaptcha v2
235+
recaptcha = ReCaptcha(settings.RECAPTCHA_API_KEY_V2, settings.RECAPTCHA_API_SECRET_V2, "v2")
236+
237+
238+
# in the section that auth is defined
239+
# Example:
240+
auth = Auth(session, db, define_tables=False)
241+
242+
# Add this line at the end of auth declaration to enable recaptcha on login, register and request_reset_password forms.
243+
# or enable it on the action that you want by especifying the action name
244+
245+
#Example:
246+
247+
auth.extra_form_fields = {"login": [recaptcha.field], "register": [recaptcha.field], "request_reset_password": [recaptcha.field], }
248+
249+
250+
#In section where auth is enabled, add the recaptcha fixture
251+
# Example:
252+
253+
# #######################################################
254+
# Enable authentication line
255+
# #######################################################
256+
auth.enable(uses=(session, T, db, recaptcha.fixture),env=dict(T=T))
257+
258+
Finally in ``auth.html`` add:
259+
260+
.. code:: python
261+
[[try:]]
262+
[[=form]]
263+
[[except:]]
264+
[[pass]]
265+
[[=recaptcha]]
266+
267+
268+
After completing these steps, the reCAPTCHA field will be added to the login, register, and request_reset_password forms.
269+
270+
Enabling hCAPTCHA
271+
^^^^^^^^^^^^^^^^^
272+
273+
in ``settings.py`` add your HCAPTCHA_SITE_KEY and HCAPTCHA_SECRET_KEY:
274+
275+
.. code:: python
276+
HCAPTCHA_SITE_KEY = "your_hcaptcha_site_key"
277+
HCAPTCHA_SECRET_KEY = "your_hcaptcha_secret_key"
278+
279+
280+
In ``common.py`` add:
281+
282+
.. code:: python
283+
#import the functionality
284+
from . import settings
285+
from py4web.utils.hcaptcha import Hcaptcha
286+
287+
hcaptcha = Hcaptcha(settings.HCAPTCHA_SITE_KEY, settings.HCAPTCHA_SECRET_KEY)
288+
289+
290+
# in the section that auth is defined
291+
# Example:
292+
auth = Auth(session, db, define_tables=False)
293+
294+
# Add this line at the end of auth declaration to enable hcaptcha on login, register and request_reset_password forms.
295+
# or enable it on the action that you want by especifying the action name
296+
297+
#Example:
298+
auth.extra_form_fields = {"login": [hcaptcha.field], "register": [hcaptcha.field], "request_reset_password": [hcaptcha.field], }
299+
300+
#In section where auth is enabled, add the hcaptcha fixture
301+
# Example:
302+
303+
# #######################################################
304+
# Enable authentication
305+
# #######################################################
306+
auth.enable(uses=(session, T, db, hcaptcha.fixture),env=dict(T=T))
307+
308+
309+
Finally in ``auth.html`` add:
310+
311+
.. code:: python
312+
[[try:]]
313+
[[=form]]
314+
[[except:]]
315+
[[pass]]
316+
[[=hcaptcha]]
317+
318+
After completing these steps, the hCAPTCHA field will be added to the login, register, and request_reset_password forms.
319+
320+
321+
153322
Two Factor Authentication
154323
~~~~~~~~~~~~~~~~~~~~~~~~~
155324

@@ -431,6 +600,8 @@ You will also have to register your OAuth2 redirect URI in your created applicat
431600
As Discord users have no concept of first/last name, the user in the auth table will contain the
432601
Discord username as the first name and discriminator as the last name.
433602

603+
604+
434605
Auth API Plugins
435606
~~~~~~~~~~~~~~~~
436607

0 commit comments

Comments
 (0)