diff --git a/requirements_dev.txt b/requirements_dev.txt index 90fc3a374..0e31ac9c3 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -4,6 +4,7 @@ # Additional runtime dependencies +plivo twilio phonenumberslite diff --git a/two_factor/gateways/plivo/__init__.py b/two_factor/gateways/plivo/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/two_factor/gateways/plivo/gateway.py b/two_factor/gateways/plivo/gateway.py new file mode 100644 index 000000000..237822b92 --- /dev/null +++ b/two_factor/gateways/plivo/gateway.py @@ -0,0 +1,32 @@ +import plivo +from django.conf import settings +from django.template.loader import render_to_string + + +class Plivo: + """ + Gateway for sending text messages using Plivo. + """ + + def __init__(self): + self.client = plivo.RestClient( + auth_id=getattr(settings, 'PLIVO_AUTH_ID'), + auth_token=getattr(settings, 'PLIVO_AUTH_TOKEN') + ) + self.source_number = getattr(settings, 'PLIVO_SOURCE_NUMBER') + + def make_call(self, device, token): + raise NotImplementedError + + def send_sms(self, device, token): + text = render_to_string( + 'two_factor/plivo/sms_message.html', + {'token': token} + ) + send_kwargs = { + 'src': self.source_number, + 'dst': device.number.as_e164, + 'text': text, + } + + self.client.messages.create(**send_kwargs) diff --git a/two_factor/templates/two_factor/plivo/sms_message.html b/two_factor/templates/two_factor/plivo/sms_message.html new file mode 100644 index 000000000..822cfde1c --- /dev/null +++ b/two_factor/templates/two_factor/plivo/sms_message.html @@ -0,0 +1,5 @@ +{% load i18n %} +{% blocktrans trimmed %} + Your OTP token is {{ token }} +{% endblocktrans %} +