$OpenBSD: patch-radicale_auth_htpasswd_py,v 1.1 2014/03/26 10:17:02 sthen Exp $

bcrypt support, based on
http://evilshit.wordpress.com/2013/11/19/how-to-install-a-caldav-and-carddav-server-using-radicale/#bcrypt

--- radicale/auth/htpasswd.py.orig	Fri May 17 00:27:26 2013
+++ radicale/auth/htpasswd.py	Tue Mar 25 14:44:21 2014
@@ -29,6 +29,7 @@ supported, but md5 is not (see ``htpasswd`` man page t
 
 import base64
 import hashlib
+import bcrypt
 
 from .. import config
 
@@ -58,11 +59,21 @@ def _sha1(hash_value, password):
     return sha1.digest() == base64.b64decode(hash_value)
 
 
+def _bcrypt(hash_value, password):
+    """Check if ``hash_value`` and ``password`` match using bcrypt method."""
+    hash_value = hash_value.encode("ascii")
+    password = password.encode(config.get("encoding", "stock"))
+    return bcrypt.checkpw(password, hash_value)
+
+
 def is_authenticated(user, password):
     """Check if ``user``/``password`` couple is valid."""
     for line in open(FILENAME).readlines():
         if line.strip():
             login, hash_value = line.strip().split(":")
             if login == user:
-                return globals()["_%s" % ENCRYPTION](hash_value, password)
+                if hash_value[0:5] == '{SHA}':
+                    return _sha1(hash_value, password)
+                else:
+                    return globals()["_%s" % ENCRYPTION](hash_value, password)
     return False
