django-otp-twilio

PyPI Documentation Source

This is a django-otp plugin that delivers tokens via Twilio’s SMS service.

See django-otp for more information on the OTP framework.

Installation

django-otp-twilio can be installed via pip:

pip install django-otp-twilio

Once installed it should be added to INSTALLED_APPS after django_otp core:

INSTALLED_APPS = [
    ...
    'django_otp',
    'django_otp.plugins.otp_totp',
    'django_otp.plugins.otp_hotp',
    'django_otp.plugins.otp_static',

    'otp_twilio',
]

Twilio SMS Devices

class otp_twilio.models.TwilioSMSDevice(*args, **kwargs)[source]

A SideChannelDevice that delivers a token via the Twilio SMS service.

The tokens are valid for OTP_TWILIO_TOKEN_VALIDITY seconds. Once a token has been accepted, it is no longer valid.

number

CharField: The mobile phone number to deliver to. Twilio recommends using the E.164 format. For US numbers, this would look like ‘+15555555555’. At the time of writing, Twilio will try to infer the correct E.164 format if it is not used, but this should not be relied upon.

exception DoesNotExist
exception MultipleObjectsReturned
generate_challenge()[source]

Sends the current TOTP token to self.number.

Returns:

OTP_TWILIO_CHALLENGE_MESSAGE on success.

Raises:

Exception if delivery fails.

get_throttle_factor()[source]

This must be implemented to return the throttle factor.

The number of seconds required between verification attempts will be \(c2^{n-1}\) where c is this factor and n is the number of previous failures. A factor of 1 translates to delays of 1, 2, 4, 8, etc. seconds. A factor of 0 disables the throttling.

Normally this is just a wrapper for a plugin-specific setting like OTP_EMAIL_THROTTLE_FACTOR.

verify_token(token)[source]

Verifies a token by content and expiry.

On success, the token is cleared and the device saved.

Parameters:

token (str) – The OTP token provided by the user.

Return type:

bool

Admin

The following ModelAdmin subclass is registered with the default admin site. We recommend its use with custom admin sites as well:

class otp_twilio.admin.TwilioSMSDeviceAdmin(model, admin_site)[source]

ModelAdmin for TwilioSMSDevice.

Settings

OTP_TWILIO_URL

Default: "https://api.twilio.com""

Twilio API URL. This can be used to change edge location

OTP_TWILIO_ACCOUNT

Default: None

Your Twilio account ID.

OTP_TWILIO_API_KEY

Default: None

Your Twilio API key, if no API key is specified requests are made using the Twilio Account ID.

OTP_TWILIO_AUTH

Default: None

Your Twilio auth token used for API requests. (Either API Key token or account auth token)

OTP_TWILIO_CHALLENGE_MESSAGE

Default: "Sent by SMS"

The message returned by generate_challenge(). This may contain '{token}', which will be replaced by the token. This completely negates any security benefit to the device, but it’s handy for development, especially in combination with OTP_TWILIO_NO_DELIVERY.

OTP_TWILIO_FROM

Default: None

The phone number to send SMS messages from. This must be one of your Twilio numbers.

OTP_TWILIO_MESSAGING_SERVICE_SID

Default: None

The Twilio messaging service SID to send SMS messages from. This must be one of your Twilio messaging services. If supplied, this takes precedence over OTP_TWILIO_FROM.

OTP_TWILIO_NO_DELIVERY

Default: False

Send tokens to the ‘otp_twilio.models’ logger instead of delivering them by SMS. Useful for development.

OTP_TWILIO_THROTTLE_FACTOR

Default: 1

This controls the rate of throttling. The sequence of 1, 2, 4, 8… seconds is multiplied by this factor to define the delay imposed after 1, 2, 3, 4… successive failures. Set to 0 to disable throttling completely.

OTP_TWILIO_TOKEN_TEMPLATE

Default: "{token}"

A string template for generating the token message. By default, this is just the token itself, but you can customize it. The template will be rendered with Python string formatting (template.format(token=token)).

OTP_TWILIO_TOKEN_VALIDITY

Default: 30

The number of seconds for which a delivered token will be valid.

Changes

Change Log

License

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org/>