Commits

Denis Darii committed a75d75b

implemented the first version of dynamic_password

Comments (0)

Files changed (5)

 Verifies provided password according to DYNAMIC_PASSWORD_PATTERN in your
 ``settings.py``. You can also enable this feature only for STAFF users, see below.
 
+You can format the value of your DYNAMIC_PASSWORD_PATTERN according to:
+http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior
+
 To do: explain in more details how it works.
 
 
 
 Add this line to your settings.py:
 
-    DYNAMIC_PASSWORD_PATTERN = '<PASSWORD><yyyy>'
+    DYNAMIC_PASSWORD_PATTERN = '<PASSWORD>%m'
 
 change it's value according to your needs.
 Some examples::
 
-    DYNAMIC_PASSWORD_PATTERN = '<PASSWORD><dd>'
-    DYNAMIC_PASSWORD_PATTERN = '<PASSWORD><dd>'
-    DYNAMIC_PASSWORD_PATTERN = '<mm><PASSWORD><dd>'
-    DYNAMIC_PASSWORD_PATTERN = '<dd><PASSWORD><yy>'
+    DYNAMIC_PASSWORD_PATTERN = '<PASSWORD>%d'
+    DYNAMIC_PASSWORD_PATTERN = '<PASSWORD>%m'
+    DYNAMIC_PASSWORD_PATTERN = '%m<PASSWORD>%d'
+    DYNAMIC_PASSWORD_PATTERN = '%d<PASSWORD>%Y'
+
+You can format this value according to: http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior
 
 Optional: set DYNAMIC_PASSWORD_ONLY_STAFF
 ----------------------------------------------------

dynamic_password/backends.py

-from django.conf import settings
 from django.contrib.auth.models import User
 from dynamic_password.utils import get_clean_password
 
 
     Set DYNAMIC_PASSWORD_PATTERN in your settings.py. For example:
 
-    DYNAMIC_PASSWORD_PATTERN = '<PASSWORD><dd>'
-    DYNAMIC_PASSWORD_PATTERN = '<PASSWORD><dd>'
-    DYNAMIC_PASSWORD_PATTERN = '<mm><PASSWORD><dd>'
-    DYNAMIC_PASSWORD_PATTERN = '<dd><PASSWORD><yy>'
+    DYNAMIC_PASSWORD_PATTERN = '<PASSWORD>%d'
+    DYNAMIC_PASSWORD_PATTERN = '<PASSWORD>%m'
+    DYNAMIC_PASSWORD_PATTERN = '%m<PASSWORD>%d'
+    DYNAMIC_PASSWORD_PATTERN = '%d<PASSWORD>%Y'
     """
 
     supports_inactive_user = False

dynamic_password/tests.py

+from datetime import datetime
 from django.test import TestCase
 from dynamic_password.utils import get_clean_password
+from django.test.utils import override_settings
 
 
 class DynamicPasswordTest(TestCase):
+    def setUp(self):
+        self.now = datetime.now()
+
+    @override_settings(DYNAMIC_PASSWORD_PATTERN='<PASSWORD>%d')
     def test_get_clean_password(self):
-        self.assertEqual(get_clean_password('denis85'), 'denis')
-        self.assertEqual(get_clean_password('apr126'), 'apr1')
+        self.assertEqual(get_clean_password(self.now.strftime('denis%d')), 'denis')
+        self.assertEqual(get_clean_password(self.now.strftime('apr1%d')), 'apr1')

dynamic_password/utils.py

+from datetime import datetime
+import difflib
+from django.conf import settings
+
+
 def get_clean_password(raw_password):
-    return raw_password[:-2]
+    clean_password = ''
+    password_pattern = getattr(settings, 'DYNAMIC_PASSWORD_PATTERN', '<PASSWORD>')
+    now = datetime.now()
+    dynamic_password = now.strftime(password_pattern)
+    diff = difflib.SequenceMatcher(None, raw_password, dynamic_password.replace('<PASSWORD>', ''))
+    for block in diff.get_matching_blocks():
+        if block[2]:
+            clean_password += raw_password[0:block[0]]
+    return clean_password

example/example/settings.py

 AUTHENTICATION_BACKENDS = (
     'dynamic_password.backends.DynamicPasswordBackend',
     )
-DYNAMIC_PASSWORD_PATTERN = '<PASSWORD><dd>'
+DYNAMIC_PASSWORD_PATTERN = '<PASSWORD>%d'
 DYNAMIC_PASSWORD_ONLY_STAFF = False
 # END dynamic_password configuration