Twilio Middleware not found (Seahub 6.2.5 /Community Edition)

I activated Two-Factor-Authentication in the Community Edition Version 6.2.5. This works nicely with the Authenticator. If I configure Twilo according to the Server Manual, the NGINX seahub.error.log indicates that the necessary python module (of seahub) is not available (see blow log entries).

Any idea how to fix this?

2018/03/11 08:51:37 [error] 1025#1025: *17665 FastCGI sent in stderr: “Traceback (most recent call last):
File “build/bdist.linux-x86_64/egg/flup/server/fcgi_base.py”, line 558, in run
protocolStatus, appStatus = self.server.handler(self)
File “build/bdist.linux-x86_64/egg/flup/server/fcgi_base.py”, line 1118, in handler
result = self.application(environ, start_response)
File “/home/seafile/ligio/seafile-server-6.2.5/seahub/thirdpart/Django-1.8.18-py2.7.egg/django/core/handlers/wsgi.py”, line 170, in call
self.load_middleware()
File “/home/seafile/ligio/seafile-server-6.2.5/seahub/thirdpart/Django-1.8.18-py2.7.egg/django/core/handlers/base.py”, line 50, in load_middleware
mw_class = import_string(middleware_path)
File “/home/seafile/ligio/seafile-server-6.2.5/seahub/thirdpart/Django-1.8.18-py2.7.egg/django/utils/module_loading.py”, line 26, in import_string
module = import_module(module_path)
File “/home/seafile/ligio/seafile-server-6.2.5/seahub/thirdpart/importlib-1.0.4-py2.6.egg/importlib/init.py”, line 37, in import_module
import(name)
ImportError: No module named seahub_extra.two_factor.gateways.twilio.middleware” while reading response header from upstream, client

Did you run sudo pip install twilio ?

1 Like

Yes, and pip list shows that twilio (6.10.4) is installed.

Seems to be somethng in your Python script. Why did you put " behind the name of the module?

After I changed the configuration by replacing “seahub_extra” (as in the manual) by “sheahub”, the gateway and middleware are loaded and available to the user

TWO_FACTOR_SMS_GATEWAY = ‘seahub.two_factor.gateways.twilio.gateway.Twilio’
TWILIO_ACCOUNT_SID = ‘’
TWILIO_AUTH_TOKEN = ‘’
TWILIO_CALLER_ID = ‘’
EXTRA_MIDDLEWARE_CLASSES = (
‘seahub.two_factor.gateways.twilio.middleware.ThreadLocals’,
)

Suggest to update the Server Manual Section on Two_Factor Authentication accordingly.

The remaining problem is now that since the Twilio Version 6.* the Python Helper has removed the TwilioRestClient (replaced by Client). The API has also changed, so more modifications are required.

2018-03-16 18:24:02,780 [ERROR] seahub.two_factor.views.core:115 render_next_step Could not generate challenge
Traceback (most recent call last):
File “/home/seafile/ligio/seafile-server-6.2.5/seahub/seahub/two_factor/views/core.py”, line 112, in render_next_step
self.get_device().generate_challenge()
File “/home/seafile/ligio/seafile-server-6.2.5/seahub/seahub/two_factor/models/phone.py”, line 80, in generate_challenge
send_sms(device=self, token=token)
File “/home/seafile/ligio/seafile-server-6.2.5/seahub/seahub/two_factor/gateways/init.py”, line 16, in send_sms
gateway = get_gateway_class(getattr(settings, ‘TWO_FACTOR_SMS_GATEWAY’))()
File “/home/seafile/ligio/seafile-server-6.2.5/seahub/seahub/two_factor/gateways/twilio/gateway.py”, line 46, in init
getattr(settings, ‘TWILIO_AUTH_TOKEN’))
File “/home/seafile/.local/lib/python2.7/site-packages/twilio/base/obsolete.py”, line 20, in new_func
.format(func.name)
ObsoleteException: TwilioRestClient has been removed from this version of the library. Please refer to current documentation for guidance.

Given the API change since Twilio Python Helper Version 6.*, I modified the seafile-server-6.2.5/seahub/seahub/two_factor/gateways/twilio/gateway.py
by replacing “TwilioRestClient” with “Client” and “sms.messages” by “messages”
Below the diff of the original with the modified file

13c13
< from twilio.rest import TwilioRestClient

from twilio.rest import Client
45c45
< self.client = TwilioRestClient(getattr(settings, ‘TWILIO_ACCOUNT_SID’),


    self.client = Client(getattr(settings, 'TWILIO_ACCOUNT_SID'),

62c62
< self.client.sms.messages.create(

    self.client.messages.create(

P.S. If you use an Alphanumeric ID (e.g., ‘Seafile’) as TWILIO_CALLER_ID in the configuration, you pay-as-you go per SMS and you need not to purchase an phone number (assuming that SMS with Alphanumeric ID is supported in your country). On the mobile, you receive the SMS with the identifier.

Now, the 2FA with Twilio SMS works nicely under Seafile 6.2.5 Community Edition on Ubuntu.