Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plivo gateway. #672

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ setting. Then, you may want to configure the following settings:

* ``'two_factor.gateways.twilio.gateway.Twilio'`` for sending real text messages using
Twilio_.
* ``'two_factor.gateways.plivo.gateway.Plivo'`` for sending SMS using Plivo_.
* ``'two_factor.gateways.fake.Fake'`` for development, recording tokens to the
default logger.

Expand Down Expand Up @@ -146,6 +147,26 @@ Additionally, you need to enable the ``ThreadLocals`` middleware:

.. autoclass:: two_factor.gateways.twilio.gateway.Twilio

Plivo Gateway
--------------
To use the Plivo you need to get the Auth ID and Auth Token from the Plivo console.
You need to buy a source number from Plivo too, which is used by the gateway too.
To use Plivo as a authentication service you need to setup a PHLO for Two-Factor
authentication.
The dependency needed is the plivo module.

.. code-block:: console

pip install plivo

Sample configuration in settings could look like this:

.. code-block:: python

PLIVO_AUTH_ID = 'NANWFHZJDKYJG2YNNNN2'
PLIVO_AUTH_TOKEN = 'ZGYwMTVkMGE5NGiiiWFmHGHmYmNhOWY3NTRhYzQ4'
PLIVO_SOURCE_NUMBER = '+1 206-426-9999'

Fake Gateway
------------
.. autoclass:: two_factor.gateways.fake.Fake
Expand Down
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# Additional runtime dependencies

plivo
twilio
phonenumberslite

Expand Down
Empty file.
32 changes: 32 additions & 0 deletions two_factor/gateways/plivo/gateway.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 5 additions & 0 deletions two_factor/templates/two_factor/plivo/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 %}