migration for users account
authorgniibe <gniibe@fsij.org>
Thu, 19 Sep 2013 07:52:31 +0000 (16:52 +0900)
committergniibe <gniibe@fsij.org>
Thu, 19 Sep 2013 07:52:31 +0000 (16:52 +0900)
auth_migrate.py [new file with mode: 0644]

diff --git a/auth_migrate.py b/auth_migrate.py
new file mode 100644 (file)
index 0000000..3ed03d2
--- /dev/null
@@ -0,0 +1,36 @@
+from django.db.models import get_model
+from django.contrib.auth.models import User
+from hashlib import sha1
+
+class MyUserAuthBackend(object):
+
+    def check_legacy_password(self, db_password, supplied_password):
+        return constant_time_compare(sha1(supplied_password).hexdigest(), db_password)
+
+    def authenticate(self, username=None, password=None):
+        """ Authenticate a user."""
+        try:
+            user = User.objects.get(username=username)
+
+            if '$' not in user.password:
+                if self.check_legacy_password(user.password, password):
+                    user.set_password(password)
+                    user.save()
+                    return user
+                else:
+                    return None
+
+            else:
+                if user.check_password(password):
+                    return user
+
+        except User.DoesNotExist:
+            return None
+
+
+    def get_user(self, user_id):
+        """ Get a User object from the user_id. """
+        try:
+            return User.objects.get(pk=user_id)
+        except User.DoesNotExist:
+            return None