Added digest authentication for cookbook editing
[cookbook.git] / bin / cook.py
index 5f006b4e3bcfd763f9b5d8d409f34ef40623ce91..42fc032b40c9e4431fff2649d73f3840f6352cbc 100755 (executable)
 # You should have received a copy of the GNU General Public License
 # along with Cookbook.  If not, see <http://www.gnu.org/licenses/>.
 
+from __future__ import with_statement
+
 import os.path
+import uuid
 
 import cookbook
 from cookbook import Cookbook
@@ -93,8 +96,37 @@ if __name__ == '__main__':
                     'tools.encode.encoding': 'utf8',
                     'tools.staticdir.root': static_dir,
                     })
-            app_config = { '/static': { 'tools.staticdir.on': True,
-                                        'tools.staticdir.dir': '', } }
+            if cherrypy.__version__.startswith('3.2'):
+                get_ha1 = cherrypy.lib.auth_digest.get_ha1_file_htdigest(
+                    '.htaccess')
+                digest_auth = {
+                    'tools.auth_digest.on': True,
+                    'tools.auth_digest.realm': 'cookbook',
+                    'tools.auth_digest.get_ha1': get_ha1,
+                    'tools.auth_digest.key': str(uuid.uuid4()),
+                    }
+            else:
+                passwds = {}
+                with open('.htaccess', 'r') as f:
+                    for line in f:
+                        user,realm,ha1 = line.strip().split(':')
+                        passwds[user] = ha1  # use the ha1 as the password
+                digest_auth = {
+                    'tools.digest_auth.on': True,
+                    'tools.digest_auth.realm': 'cookbook',
+                    'tools.digest_auth.users': passwds,
+                    }
+                print passwds
+                del(passwds)
+            app_config = {
+                '/static': {
+                    'tools.staticdir.on': True,
+                    'tools.staticdir.dir': '',
+                    },
+                '/edit': digest_auth,
+                '/add_tag': digest_auth,
+                '/remove_tag': digest_auth,
+                }
             cherrypy.quickstart(root=s, config=app_config)
         elif cherrypy.__version__.startswith('2.'):
             cherrypy.root = s