Python module now supports the same authorization hash functions as C.
authorRichard Kettlewell <rjk@greenend.org.uk>
Sun, 5 Apr 2009 11:28:33 +0000 (12:28 +0100)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sun, 5 Apr 2009 11:28:33 +0000 (12:28 +0100)
This means we need at least Python 2.5 (which shouldn't be a very
onerous requirement any more!)

configure.ac
python/disorder.py.in
tests/Makefile.am
tests/hashes.py [new file with mode: 0755]

index eb49d6a..94cffce 100644 (file)
@@ -342,7 +342,7 @@ if test $want_gtk = yes; then
   fi
 fi
 if test $want_tests = yes && test $want_python = yes; then
-  AM_PATH_PYTHON([2.4])
+  AM_PATH_PYTHON([2.5])
   subdirs="${subdirs} python tests"
 fi
 AC_SUBST([subdirs])
index e873e49..f6fe1a4 100644 (file)
@@ -50,7 +50,7 @@ import os
 import pwd
 import socket
 import binascii
-import sha
+import hashlib
 import sys
 import locale
 
@@ -66,6 +66,18 @@ _unquoted = re.compile("[^\"' \\t\\n\\r][^ \t\n\r]*")
 
 _response = re.compile("([0-9]{3}) ?(.*)")
 
+# hashes
+_hashes = {
+  "sha1": hashlib.sha1,
+  "SHA1": hashlib.sha1,
+  "sha256": hashlib.sha256,
+  "SHA256": hashlib.sha256,
+  "sha384": hashlib.sha384,
+  "SHA384": hashlib.sha384,
+  "sha512": hashlib.sha512,
+  "SHA512": hashlib.sha512,
+};
+
 version = "_version_"
 
 ########################################################################
@@ -387,8 +399,7 @@ class client:
             password = self.config['password']
           else:
             password = self.password
-          # TODO support algorithms other than SHA-1
-          h = sha.sha()
+          h = _hashes[algo]()
           h.update(password)
           h.update(binascii.unhexlify(challenge))
           self._simple("user", user, h.hexdigest())
index 2508499..9936f17 100644 (file)
@@ -1,3 +1,4 @@
+
 #
 # This file is part of DisOrder.
 # Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
@@ -26,7 +27,7 @@ disorder_udplog_DEPENDENCIES=../lib/libdisorder.a
 
 TESTS=cookie.py dbversion.py dump.py files.py play.py queue.py \
        recode.py search.py user-upgrade.py user.py aliases.py  \
-       schedule.py
+       schedule.py hashes.py
 
 TESTS_ENVIRONMENT=${PYTHON} -u
 
diff --git a/tests/hashes.py b/tests/hashes.py
new file mode 100755 (executable)
index 0000000..813951b
--- /dev/null
@@ -0,0 +1,50 @@
+#! /usr/bin/env python
+#
+# This file is part of DisOrder.
+# Copyright (C) 2009 Richard Kettlewell
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+import dtest,disorder,re
+
+def set_auth_algo(h):
+    configpath = "%s/config" % dtest.testroot
+    config = open(configpath, "r").readlines()
+    config = filter(lambda l: not re.match('authorization_algorithm', l),
+                    config)
+    config.append('authorization_algorithm %s\n' % h)
+    open(configpath, "w").write("".join(config))
+
+def test():
+    """Authentication hash tests"""
+    created = False
+    for h in ['sha1', 'SHA1', 'sha256', 'SHA256',
+              'sha384', 'SHA384', 'sha512', "SHA512" ]:
+        print " setting authorization hash to %s" % h
+        set_auth_algo(h)
+        dtest.start_daemon()
+        if not created:
+            dtest.create_user()
+            created = True
+        print " exercising C implementation"
+        dtest.command(["disorder",
+                       "--config", disorder._configfile, "--no-per-user-config",
+                       "--user", "root", "version"])
+        print " exercising Python implementation"
+        c = disorder.client()
+        c.version()
+        dtest.stop_daemon()
+
+if __name__ == '__main__':
+    dtest.run()