Skip to content
This repository has been archived by the owner on May 5, 2020. It is now read-only.

Commit

Permalink
Merge pull request #39 from wieden-kennedy/master
Browse files Browse the repository at this point in the history
Allow numeric codes (should also close issue #38)
  • Loading branch information
relekang committed Nov 3, 2014
2 parents c72e99f + 9be61f2 commit 57cc06c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ django-nopassword settings

.. attribute:: NOPASSWORD_TWILIO_AUTH_TOKEN
Account secret for Twilio

.. attribute:: NOPASSWORD_NUMERIC_CODES
A boolean flag if set to True, codes will contain numeric characters only (0-9). Default: False

Django settings used in django-nopassword
+++++++++++++++++++++++++++++++++++++++++
Expand Down
6 changes: 5 additions & 1 deletion nopassword/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# -*- coding: utf-8 -*-
from .email import EmailBackend
from .sms import TwilioBackend

try:
from .sms import TwilioBackend
except ImportError:
pass
6 changes: 5 additions & 1 deletion nopassword/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ def generate_code(cls, length=20):
m = getattr(hashlib, hash_algorithm)()
m.update(getattr(settings, 'SECRET_KEY', None).encode('utf-8'))
m.update(os.urandom(16))
return m.hexdigest()[:length]
if getattr(settings, 'NOPASSWORD_NUMERIC_CODES', False):
hashed = str(int(m.hexdigest(), 16))[-length:]
else:
hashed = m.hexdigest()[:length]
return hashed
6 changes: 6 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def test_shorter_code(self):
self.code = LoginCode.create_code_for_user(self.user)
self.assertEqual(len(self.code.code), 8)

@override_settings(NOPASSWORD_NUMERIC_CODES=True)
def test_numeric_code(self):
self.code = LoginCode.create_code_for_user(self.user)
self.assertEqual(len(self.code.code), 20)
self.assertTrue(self.code.code.isdigit())

def test_next_value(self):
self.code = LoginCode.create_code_for_user(self.user, next='/secrets/')
self.assertEqual(self.code.next, '/secrets/')
Expand Down

0 comments on commit 57cc06c

Please sign in to comment.