Package: django-twilio-2fa
Django 2FA using Twilio VerifyPrerequisitesPython 3.6+Twilioaccountdjango2.29+twilio7.8.2+phonenumbers8.12.26+InstallationInstall usingpip install django-twilio-2fa.If install fails, trypip install --upgrade pipAdd to yourINSTALLED_APPS:INSTALLED_APPS=(..."django_twilio_2fa","django_widget_tweaks",# only required if using included templates...)Add the projecturls.py:urlpatterns=[...# View-based 2FApath("2fa/",# Can be changed to any pathinclude("django_twilio_2fa.urls")),# API-based 2FApath("2fa-api/",# Can be changed to any pathinclude("django_twilio_2fa.api.urls")),...]Other PagesView-based 2FA FlowSettingsErrorsSignalsSignal names are prefixedtwilio_2fa_.All signals are sent with at least the following arguments:request: CurrentRequestinstance orNoneuser:Userinstance orNonetwilio_errorWhen the Twilio client throws an error, this signal is emitted -- whether the error was handled.This is a special signal that only provides the exception instance asexcand no other arguments.set_user_dataWhen a user registers or changes their 2FA data, this signal is emitted the updated information and should be used to update the user's instance.Additional arguments sent with this signal:field: field name (phone_numberis the only option)value: updated valueverification_sentThis signal is triggered anytime a verification is sent.Additional arguments sent with this signal:method: Verification methodverification_sid: Twilio SID for verificationstart_timestamp:DateTimeof the original sendlast_timestamp:DateTimeof the last send attemptverification_successThis signal is triggered when a user completes verification successfully.Theverification_status_changedsignal is also triggered during a successful verification.Additional arguments sent with this signal:verification_sid: Twilio verification SIDverification_status_changedThis signal is triggered when the Twilio verification status is changed.Options forstatus:approvedandcanceled.Additional arguments sent with this signal:status: Status verification was changed toverification_sid: Twilio verification SIDverification_failedThis signal is triggered when the Twilio verification attempt has failed.Additional arguments sent with this signal:verification_sid: Twilio verification SIDCustomizationThe presentation code usesBootstrap 5,Font Awesome 5, anddjango-widget-tweaks. None are an absolute requirement and can be removed using custom templates or, in the case of Font Awesome, defining theMETHOD_DISPLAY_CBsetting.All templates are in thetwilio_2fadirectory. To override these templates, you can put your version in your owntwilio_2fadirectory anywhere your templates are stored._base.htmlThis is the primary template that all main templates extends.It defines a single block for content:content(outlined in yellow above). Fordjango_widget_tweaks, thecontentblock is wrapped byWIDGET_ERROR_CLASS.The header can also be changed using theheaderblock (outlined in red). Header icon classes changed using theheader_icon_classblock (outlined in blue) and text changed using theheader_textblock (outlined in green)._messages.htmlThis template shows messages fromdjango.contrib.messagesand is included in each of the main templates._form_errors.htmlThis template displays a form field's errors.fieldshould be passed in the context.failed.htmlThis template is shown when the user's verification failed either from a timeout of the verification, maximum tries are exceeded, an API failure with Twilio, or other general error.It conditionally allows users to retry verification based on thecan_retrysession variable.registration_form.htmlThis template shows the registration form to the user and serves as the base template forregister.htmlandchange.html.register.htmlThis template shows the registration form to the user.IfALLOW_REGISTRATIONisFalse, the user is not shown this view and will be redirected to the failure page if no phone number is returned byPHONE_NUMBER_CB.It is based onregistration_form.html.change.htmlThis template shows the change form to the user.It is based onregistration_form.html.start.htmlThis template allows the user to select the verification method.If only one method exists, the user will not see this page.success.htmlThis template is shown on a successful verification ifVERIFY_SUCCESS_URLis not set.verify.htmlThis template shows the token form.DevelopmentPerform the following steps in the root directory:Create a virtual environment and activate.Installdjango_twilio_2fa:pip install -e .Perform the following steps in thetest_appdirectory:
3. Update path to this package in the requirements.txt
4. Install requirements:pip install -r requirements.txt.
5. Copy.env-sampleto.envand update with your twilio settings.
6. Run migrations:python manage.py migrate.
7. Run the server:python manage.py runserver.The test app should now be available athttp://localtest.me:8000.To run tests, runpython manage.py testfrom thetest_appdirectory.To-DoInternationalizationE-mail verificationWhatsApp integrationTOTP integrationPush for web integrationAbstraction for 2FA outside of web flowChangelog0.32 - bug fixes for lost users0.31 - minor fix oferror_displayssetting0.30 - added more messages for user displays; updated view-based templates to use messages; addeddisplaykey to API-based responses0.29 - downgraded requirements for inclusivity0.28 - added better customization of user messaging0.27 - allow sending verifications to different values; set default status code for errors to 4000.26 - added handling of Twilio Error 602000.25 - bug fix for handling unauthenticated users0.24 - refactored to abstract process; added API endpoints; allow for userless 2FA0.23 - Twilio rate limiting error handling0.22 - Added internationalization and e-mail verification (thanks tojgoodsell-summitgrp)