Skip to content

Commit

Permalink
Ability to send custom SMS using Twilio during OTP authentication (ja…
Browse files Browse the repository at this point in the history
…zzband#383)

Users can set their own SMS message by overriding two_factor/twilio/sms_message.html

Fixes jazzband#382
  • Loading branch information
aseem-hegshetye authored Oct 7, 2020
1 parent 6d5a2c8 commit 32684ca
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ example/settings_private.py
/htmlcov/
/docs/_build/
.eggs/

.idea/
15 changes: 13 additions & 2 deletions tests/test_gateways.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest.mock import Mock, patch
from urllib.parse import urlencode

from django.template.loader import render_to_string
from django.test import TestCase
from django.test.utils import override_settings
from django.urls import reverse
Expand Down Expand Up @@ -58,9 +59,19 @@ def test_gateway(self, client):
from_='+456', to='+123', method='GET', timeout=15,
url='http://testserver/twilio/inbound/two_factor/%s/?locale=en-us' % code)

twilio.send_sms(device=Mock(number=PhoneNumber.from_string('+123')), token=code)
twilio.send_sms(
device=Mock(number=PhoneNumber.from_string('+123')),
token=code
)

client.return_value.messages.create.assert_called_with(
to='+123', body='Your authentication token is %s' % code, from_='+456')
to='+123',
body=render_to_string(
'two_factor/twilio/sms_message.html',
{'token': code}
),
from_='+456'
)

client.return_value.calls.create.reset_mock()
with translation.override('en-gb'):
Expand Down
12 changes: 10 additions & 2 deletions two_factor/gateways/twilio/gateway.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from urllib.parse import urlencode

from django.conf import settings
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils import translation
from django.utils.translation import gettext, pgettext
from django.utils.translation import pgettext
from twilio.rest import Client

from two_factor.middleware.threadlocals import get_current_request
Expand Down Expand Up @@ -35,6 +36,7 @@ class Twilio(object):
.. _Twilio: http://www.twilio.com/
"""

def __init__(self):
self.client = Client(getattr(settings, 'TWILIO_ACCOUNT_SID'),
getattr(settings, 'TWILIO_AUTH_TOKEN'))
Expand All @@ -52,7 +54,13 @@ def make_call(self, device, token):
url=uri, method='GET', timeout=15)

def send_sms(self, device, token):
body = gettext('Your authentication token is %s') % token
"""
send sms using template 'two_factor/twilio/sms_message.html'
"""
body = render_to_string(
'two_factor/twilio/sms_message.html',
{'token': token}
)
self.client.messages.create(
to=device.number.as_e164,
from_=getattr(settings, 'TWILIO_CALLER_ID'),
Expand Down
5 changes: 5 additions & 0 deletions two_factor/templates/two_factor/twilio/sms_message.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% load i18n %}
{% blocktrans trimmed %}
Your OTP token is {{ token }}
{% endblocktrans %}

0 comments on commit 32684ca

Please sign in to comment.