[CalendarServer-changes] [12246] twext/trunk/twext/who/test/auth_resource.rpy

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:20:39 PDT 2014


Revision: 12246
          http://trac.calendarserver.org//changeset/12246
Author:   wsanchez at apple.com
Date:     2014-01-06 16:46:20 -0800 (Mon, 06 Jan 2014)
Log Message:
-----------
Build a resource tree so we can test each auth type without changing the code.

Modified Paths:
--------------
    twext/trunk/twext/who/test/auth_resource.rpy

Modified: twext/trunk/twext/who/test/auth_resource.rpy
===================================================================
--- twext/trunk/twext/who/test/auth_resource.rpy	2014-01-07 00:29:48 UTC (rev 12245)
+++ twext/trunk/twext/who/test/auth_resource.rpy	2014-01-07 00:46:20 UTC (rev 12246)
@@ -14,51 +14,116 @@
 # limitations under the License.
 ##
 
+#
+# Run:
+#     twistd -n web --path twext/who/test
+#
+# And open this URL:
+#     http://localhost:8080/auth_resource.rpy
+#
+
 cache()
 
+from tempfile import NamedTemporaryFile
+from textwrap import dedent
+
 from twisted.cred.portal import Portal
 from twisted.web.resource import IResource
 from twisted.web.guard import (
     HTTPAuthSessionWrapper,
     BasicCredentialFactory,
-    # DigestCredentialFactory,
+    DigestCredentialFactory,
 )
 from twisted.web.static import Data
 
-# from twext.who.test.test_xml import xmlService as DirectoryService
-from twext.who.opendirectory import DirectoryService
-from twext.who.opendirectory import NoQOPDigestCredentialFactory as DigestCredentialFactory
+from twext.who.directory import DirectoryRecord
+from twext.who.test.test_xml import xmlService as XMLDirectoryService
+# from twext.who.opendirectory import (
+#     DirectoryService as OpenDirectoryDirectoryService,
+#     NoQOPDigestCredentialFactory,
+# )
 from twext.who.checker import UsernamePasswordCredentialChecker
 from twext.who.checker import HTTPDigestCredentialChecker
 
 
 
 class Realm(object):
-    def requestAvatar(self, avatarId, mind, *interfaces):
-        resource = Data(
-            "Hello, {0!r}!".format(avatarId),
-            "text/plain"
-        )
+    @staticmethod
+    def hello(avatarId):
+        message = ["Hello, {0!r}!".format(avatarId)]
 
-        return IResource, resource, lambda: None
+        if isinstance(avatarId, DirectoryRecord):
+            message.append(avatarId.description().encode("utf-8"))
 
+        return "\n\n".join(message)
 
 
-# directory = DirectoryService("/tmp/auth.xml")
-directory = DirectoryService()
+    def requestAvatar(self, avatarId, mind, *interfaces):
+        if IResource in interfaces:
+            interface = IResource
+            resource = Data(self.hello(avatarId), "text/plain")
+        else:
+            interface = None
+            resource = None
 
-checkers = [
-    HTTPDigestCredentialChecker(directory),
-    # UsernamePasswordCredentialChecker(directory),
-]
+        return interface, resource, lambda: None
 
+
+
 realm = Realm()
 
-portal = Portal(realm, checkers)
+rootResource = Data(
+    data=dedent(
+        """
+        <html>
+         <head>
+          <title>Authentication tests</title>
+         </head>
+         <body>
+          <ul>
+           <li>XML Directory Service</li>
+           <ul>
+            <li><a href="auth_resource.rpy/XMLBasic" >Basic </a></li>
+            <li><a href="auth_resource.rpy/XMLDigest">Digest</a></li>
+           </ul>
+          </ul>
+         </body>
+        </html>
+        """[1:]
+    ),
+    type="text/html",
+)
 
-factories = [
-    DigestCredentialFactory("md5", "Digest Realm"),
-    # BasicCredentialFactory("Basic Realm"),
-]
+xmlFileBasic = NamedTemporaryFile(delete=True)
+rootResource.putChild(
+    "XMLBasic",
+    HTTPAuthSessionWrapper(
+        Portal(
+            realm,
+            [
+                UsernamePasswordCredentialChecker(
+                    XMLDirectoryService(xmlFileBasic.name)
+                )
+            ]
+        ),
+        [BasicCredentialFactory("XML Basic Realm")]
+    )
+)
 
-resource = HTTPAuthSessionWrapper(portal, factories)
+xmlFileDigest = NamedTemporaryFile(delete=True)
+rootResource.putChild(
+    "XMLDigest",
+    HTTPAuthSessionWrapper(
+        Portal(
+            realm,
+            [
+                HTTPDigestCredentialChecker(
+                    XMLDirectoryService(xmlFileDigest.name)
+                )
+            ]
+        ),
+        [DigestCredentialFactory("md5", "XML Digest Realm")]
+    )
+)
+
+resource = rootResource
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/78031b7a/attachment.html>


More information about the calendarserver-changes mailing list