Title: [101694] trunk/Tools
Revision
101694
Author
[email protected]
Date
2011-12-01 11:58:38 -0800 (Thu, 01 Dec 2011)

Log Message

Add an HTDigestParser class to webkitpy

Fixes <http://webkit.org/b/73575> webkitpy doesn't provide a way to parse htdigest files

This class can be used to parse Apache's htdigest files and check whether a given
username/realm/password tuple is present in the file. Eventually this will be used for
authenticating users on build.webkit.org (<http://webkit.org/b/73353>).

Reviewed by Eric Seidel.

* Scripts/webkitpy/common/net/htdigestparser.py: Added.
(HTDigestParser.__init__): Stores the parsed representation of the file.
(HTDigestParser.authenticate): Hashes the username/realm/password tuple to generate a hashed
password and returns whether the resulting tuple is present in the file.
(HTDigestParser.entries): Just returns the parsed representation of the file.
(HTDigestParser.parse_file): Splits each line on colons and checks that each line has the
expected syntax ('username:realm:hashed_password'). If any line is invalid, we treat the
whole file as invalid and all authentication attempts will fail.

* Scripts/webkitpy/common/net/htdigestparser_unittest.py: Added.
(HTDigestParserTest.assertEntriesEqual): Helper method to assert that fake_htdigest_file,
optionally appended with some extra data, generates the expected entries.
(HTDigestParserTest.test_authenticate): Tests that tuples present in the file can
authenticate, and tuples not present cannot.
(HTDigestParserTest.test_entries): Tests that we get the expected entries from
fake_htdigest_file.

(HTDigestParserTest.test_empty_file):
(HTDigestParserTest.test_too_few_colons):
(HTDigestParserTest.test_too_many_colons):
(HTDigestParserTest.test_invalid_hash):
Test various forms of invalid files.

(HTDigestParserTest.fake_htdigest_file): Returns a fake valid htdigest file for testing.

Modified Paths

Added Paths

Diff

Modified: trunk/Tools/ChangeLog (101693 => 101694)


--- trunk/Tools/ChangeLog	2011-12-01 19:45:29 UTC (rev 101693)
+++ trunk/Tools/ChangeLog	2011-12-01 19:58:38 UTC (rev 101694)
@@ -1,3 +1,40 @@
+2011-12-01  Adam Roben  <[email protected]>
+
+        Add an HTDigestParser class to webkitpy
+
+        Fixes <http://webkit.org/b/73575> webkitpy doesn't provide a way to parse htdigest files
+
+        This class can be used to parse Apache's htdigest files and check whether a given
+        username/realm/password tuple is present in the file. Eventually this will be used for
+        authenticating users on build.webkit.org (<http://webkit.org/b/73353>).
+
+        Reviewed by Eric Seidel.
+
+        * Scripts/webkitpy/common/net/htdigestparser.py: Added.
+        (HTDigestParser.__init__): Stores the parsed representation of the file.
+        (HTDigestParser.authenticate): Hashes the username/realm/password tuple to generate a hashed
+        password and returns whether the resulting tuple is present in the file.
+        (HTDigestParser.entries): Just returns the parsed representation of the file.
+        (HTDigestParser.parse_file): Splits each line on colons and checks that each line has the
+        expected syntax ('username:realm:hashed_password'). If any line is invalid, we treat the
+        whole file as invalid and all authentication attempts will fail.
+
+        * Scripts/webkitpy/common/net/htdigestparser_unittest.py: Added.
+        (HTDigestParserTest.assertEntriesEqual): Helper method to assert that fake_htdigest_file,
+        optionally appended with some extra data, generates the expected entries.
+        (HTDigestParserTest.test_authenticate): Tests that tuples present in the file can
+        authenticate, and tuples not present cannot.
+        (HTDigestParserTest.test_entries): Tests that we get the expected entries from
+        fake_htdigest_file.
+
+        (HTDigestParserTest.test_empty_file):
+        (HTDigestParserTest.test_too_few_colons):
+        (HTDigestParserTest.test_too_many_colons):
+        (HTDigestParserTest.test_invalid_hash):
+        Test various forms of invalid files.
+
+        (HTDigestParserTest.fake_htdigest_file): Returns a fake valid htdigest file for testing.
+
 2011-12-01  Philippe Normand  <[email protected]>
 
         [GTK] Make the new 64-bit Release bot part of the core set

Added: trunk/Tools/Scripts/webkitpy/common/net/htdigestparser.py (0 => 101694)


--- trunk/Tools/Scripts/webkitpy/common/net/htdigestparser.py	                        (rev 0)
+++ trunk/Tools/Scripts/webkitpy/common/net/htdigestparser.py	2011-12-01 19:58:38 UTC (rev 101694)
@@ -0,0 +1,54 @@
+# Copyright (C) 2011 Apple Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1.  Redistributions of source code must retain the above copyright
+#     notice, this list of conditions and the following disclaimer.
+# 2.  Redistributions in binary form must reproduce the above copyright
+#     notice, this list of conditions and the following disclaimer in the
+#     documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
+# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+"""htdigestparser - a parser for htdigest files"""
+
+import hashlib
+import string
+
+
+class HTDigestParser(object):
+    def __init__(self, digest_file):
+        self._entries = self.parse_file(digest_file)
+
+    def authenticate(self, username, realm, password):
+        hashed_password = hashlib.md5(':'.join((username, realm, password))).hexdigest()
+        return [username, realm, hashed_password] in self.entries()
+
+    def entries(self):
+        return self._entries
+
+    def parse_file(self, digest_file):
+        entries = [line.rstrip().split(':') for line in digest_file]
+
+        # Perform some sanity-checking to ensure the file is valid.
+        valid_characters = set(string.hexdigits)
+        for entry in entries:
+            if len(entry) != 3:
+                return []
+            hashed_password = entry[-1]
+            if len(hashed_password) != 32:
+                return []
+            if not set(hashed_password).issubset(valid_characters):
+                return []
+
+        return entries

Added: trunk/Tools/Scripts/webkitpy/common/net/htdigestparser_unittest.py (0 => 101694)


--- trunk/Tools/Scripts/webkitpy/common/net/htdigestparser_unittest.py	                        (rev 0)
+++ trunk/Tools/Scripts/webkitpy/common/net/htdigestparser_unittest.py	2011-12-01 19:58:38 UTC (rev 101694)
@@ -0,0 +1,82 @@
+# Copyright (C) 2011 Apple Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1.  Redistributions of source code must retain the above copyright
+#     notice, this list of conditions and the following disclaimer.
+# 2.  Redistributions in binary form must reproduce the above copyright
+#     notice, this list of conditions and the following disclaimer in the
+#     documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
+# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import StringIO
+import os
+import unittest
+
+from webkitpy.common.net.htdigestparser import HTDigestParser
+
+
+class HTDigestParserTest(unittest.TestCase):
+    def assertEntriesEqual(self, entries, additional_content=None):
+        digest_file = self.fake_htdigest_file()
+        if additional_content is not None:
+            digest_file.seek(pos=0, mode=os.SEEK_END)
+            digest_file.write(additional_content)
+            digest_file.seek(pos=0, mode=os.SEEK_SET)
+        self.assertEqual(entries, HTDigestParser(digest_file).entries())
+
+    def test_authenticate(self):
+        htdigest = HTDigestParser(self.fake_htdigest_file())
+        self.assertTrue(htdigest.authenticate('user1', 'realm 1', 'password1'))
+        self.assertTrue(htdigest.authenticate('user2', 'realm 2', 'password2'))
+        self.assertTrue(htdigest.authenticate('user3', 'realm 1', 'password3'))
+        self.assertTrue(htdigest.authenticate('user3', 'realm 3', 'password3'))
+
+        self.assertFalse(htdigest.authenticate('user1', 'realm', 'password1'))
+        self.assertFalse(htdigest.authenticate('user1', 'realm 2', 'password1'))
+        self.assertFalse(htdigest.authenticate('user2', 'realm 2', 'password1'))
+        self.assertFalse(htdigest.authenticate('user2', 'realm 1', 'password1'))
+        self.assertFalse(htdigest.authenticate('', '', ''))
+
+    def test_entries(self):
+        entries = [
+            ['user1', 'realm 1', '36b8aa27fa5e9051095d37b619f92762'],
+            ['user2', 'realm 2', '14f827686fa97778f02fe1314a3337c8'],
+            ['user3', 'realm 1', '1817fc8a24119cc57fbafc8a630ea5a5'],
+            ['user3', 'realm 3', 'a05f5a2335e9d87bbe75bbe5e53248f0'],
+        ]
+        self.assertEntriesEqual(entries)
+        self.assertEntriesEqual(entries, additional_content='')
+
+    def test_empty_file(self):
+        self.assertEqual([], HTDigestParser(StringIO.StringIO()).entries())
+
+    def test_too_few_colons(self):
+        self.assertEntriesEqual([], additional_content='user1:realm 1\n')
+
+    def test_too_many_colons(self):
+        self.assertEntriesEqual([], additional_content='user1:realm 1:36b8aa27fa5e9051095d37b619f92762:garbage\n')
+
+    def test_invalid_hash(self):
+        self.assertEntriesEqual([], additional_content='user1:realm 1:36b8aa27fa5e9051095d37b619f92762000000\n')
+        self.assertEntriesEqual([], additional_content='user1:realm 1:36b8aa27fa5e9051095d37b619f9276\n')
+        self.assertEntriesEqual([], additional_content='user1:realm 1:36b8aa27fa5e9051095d37b619f9276z\n')
+        self.assertEntriesEqual([], additional_content='user1:realm 1: 36b8aa27fa5e9051095d37b619f92762\n')
+
+    def fake_htdigest_file(self):
+        return StringIO.StringIO("""user1:realm 1:36b8aa27fa5e9051095d37b619f92762
+user2:realm 2:14f827686fa97778f02fe1314a3337c8
+user3:realm 1:1817fc8a24119cc57fbafc8a630ea5a5
+user3:realm 3:a05f5a2335e9d87bbe75bbe5e53248f0
+""")
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to