From 02bf849e8d090451d4adbd67c42942982517fb74 Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Thu, 28 Nov 2019 22:51:25 +0100
Subject: [PATCH] Add optional support for SMS and phone calls using Twilio.

---
 biscuit/core/settings.py | 8 ++++++++
 biscuit/core/urls.py     | 5 +++++
 pyproject.toml           | 2 ++
 3 files changed, 15 insertions(+)

diff --git a/biscuit/core/settings.py b/biscuit/core/settings.py
index a73da45c9..0230cf03e 100644
--- a/biscuit/core/settings.py
+++ b/biscuit/core/settings.py
@@ -324,4 +324,12 @@ CRON_CLASSES = [
 
 ANONYMIZE_ENABLED = _settings.get('maintenance.anonymisable', True)
 
+if _settings.get('2fa.twilio.sid', None):
+    MIDDLEWARE.insert(MIDDLEWARE.index('django_otp.middleware.OTPMiddleware')+1, 'two_factor.middleware.threadlocals.ThreadLocals')
+    TWILIO_SID = _settings.get('2fa.twilio.sid')
+    TWILIO_TOKEN = _settings.get('2fa.twilio.token')
+    TWILIO_CALLER_ID = _settings.get('2fa.twilio.callerid')
+    TWO_FACTOR_CALL_GATEWAY = 'two_factor.gateways.twilio.gateway.Twilio'
+    TWO_FACTOR_SMS_GATEWAY = 'two_factor.gateways.twilio.gateway.Twilio'
+
 _settings.populate_obj(sys.modules[__name__])
diff --git a/biscuit/core/urls.py b/biscuit/core/urls.py
index 82259c9a3..35431ae22 100644
--- a/biscuit/core/urls.py
+++ b/biscuit/core/urls.py
@@ -38,6 +38,11 @@ urlpatterns = [
     path('', include(tf_urls))
 ]
 
+# Add URLs for optional features
+if hasattr(settings, 'TWILIO_ACCOUNT_SID'):
+    from two_factor.gateways.twilio.urls import urlpatterns as tf_twilio_urls  # noqa
+    urlpatterns += [path('', include(tf_twilio_urls))]
+
 # Serve javascript-common if in development
 if settings.DEBUG:
     urlpatterns += static('/javascript/',
diff --git a/pyproject.toml b/pyproject.toml
index c4b7bea60..cde81ad91 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -54,9 +54,11 @@ django-otp = "^0.7.4"
 django-formtools = "^2.1"
 django-two-factor-auth = "^1.9"
 django-otp-yubikey = '^0.5.2'
+twilio = { version = "^6.33", optional = true }
 
 [tool.poetry.extras]
 ldap = ["django-auth-ldap"]
+twilio = ["twilio"]
 
 [tool.poetry.dev-dependencies]
 sphinx = "^2.1"
-- 
GitLab