[Checkins] SVN: z3c.bobopublisher/trunk/s integration with zope.security

Fabio Tranchitella kobold at kobold.it
Sun Aug 16 04:59:35 EDT 2009


Log message for revision 102845:
  integration with zope.security

Changed:
  U   z3c.bobopublisher/trunk/setup.py
  U   z3c.bobopublisher/trunk/src/z3c/bobopublisher/README.txt
  A   z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware/
  A   z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware/__init__.py
  A   z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware/proxy.py
  A   z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware/security.py
  D   z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware.py
  U   z3c.bobopublisher/trunk/src/z3c/bobopublisher/tests.py
  U   z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/PKG-INFO
  U   z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/SOURCES.txt
  U   z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/entry_points.txt

-=-
Modified: z3c.bobopublisher/trunk/setup.py
===================================================================
--- z3c.bobopublisher/trunk/setup.py	2009-08-16 06:36:09 UTC (rev 102844)
+++ z3c.bobopublisher/trunk/setup.py	2009-08-16 08:59:35 UTC (rev 102845)
@@ -42,7 +42,8 @@
     [paste.app_factory]
     main = z3c.bobopublisher.application:Application
     [paste.filter_app_factory]
-    proxy = z3c.bobopublisher.middleware:make_proxy_middleware
+    proxy = z3c.bobopublisher.middleware.proxy:make_proxy_middleware
+    security = z3c.bobopublisher.middleware.security:make_security_middleware
     """,
     include_package_data=True,
     zip_safe=False,

Modified: z3c.bobopublisher/trunk/src/z3c/bobopublisher/README.txt
===================================================================
--- z3c.bobopublisher/trunk/src/z3c/bobopublisher/README.txt	2009-08-16 06:36:09 UTC (rev 102844)
+++ z3c.bobopublisher/trunk/src/z3c/bobopublisher/README.txt	2009-08-16 08:59:35 UTC (rev 102845)
@@ -166,7 +166,7 @@
 In this example, we configure a proxied application to use the ProxyFactory
 from zope.security:
 
-    >>> from z3c.bobopublisher.middleware import ProxyMiddleware
+    >>> from z3c.bobopublisher.middleware.proxy import ProxyMiddleware
     >>> proxyapp = TestApp(ProxyMiddleware(Application(),
     ...     'zope.security.checker.ProxyFactory'))
 
@@ -178,6 +178,47 @@
     ForbiddenAttribute: ('get', <Root object at ...>)
 
 
+Integration with zope.security
+------------------------------
+
+z3c.bobopublisher provides a middleware to ease the integration with
+zope.security; it uses the value referenced by the given key in the WSGI
+environment and adapts it to `zope.security.interfaces.IPrincipal':
+
+    [filter-app:security]
+    use = egg:z3c.bobopublisher#security
+    key = remote_user
+    next = application
+
+In this example, we configure an application to use the remote_user key from
+the WSGI environment for zope.security:
+
+    >>> from z3c.bobopublisher.middleware.security import SecurityMiddleware
+    >>> secureapp = TestApp(SecurityMiddleware(Application(), 'remote_user'))
+
+Using the test application, we are able to call the page and get its result,
+but we don't have any adapter registered for IPrincipal:
+
+    >>> secureapp.get('/', extra_environ={'remote_user': 'test'}).body
+    Traceback (most recent call last):
+    ...
+    TypeError: ('Could not adapt', 'test', <InterfaceClass zope.security.interfaces.IPrincipal>)
+
+We register a dummy adapter for IPrincipal:
+
+    >>> from zope.interface import Interface
+    >>> from zope.security.interfaces import IPrincipal
+    >>> from zope.security.testing import Principal
+    >>> def principal(key):
+    ...     return key == 'test' and Principal(u'test') or None
+    >>> getGlobalSiteManager().registerAdapter(principal, (Interface,), IPrincipal)
+
+Using the test application, we are able to call the page and get its result:
+
+    >>> secureapp.get('/', extra_environ={'remote_user': 'test'}).body
+    'ABC'
+
+
 Defining browser pages with ZCML
 --------------------------------
 

Copied: z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware/proxy.py (from rev 102843, z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware.py)
===================================================================
--- z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware/proxy.py	                        (rev 0)
+++ z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware/proxy.py	2009-08-16 08:59:35 UTC (rev 102845)
@@ -0,0 +1,38 @@
+##############################################################################
+#
+# Copyright (c) 2009 Fabio Tranchitella <fabio at tranchitella.it>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+"""
+$Id$
+"""
+
+from zope.dottedname.resolve import resolve
+
+
+class ProxyMiddleware(object):
+    """WSGI middleware to add support for object proxies"""
+
+    def __init__(self, app, proxy):
+        self.app = app
+        if isinstance(proxy, str):
+            proxy = resolve(proxy)
+        self.proxy = proxy
+
+    def __call__(self, environ, start_response):
+        environ['bobopublisher.proxy'] = self.proxy
+        return self.app(environ, start_response)
+
+
+def make_proxy_middleware(app, global_conf, proxy):
+    """Configure a ProxyMiddleware middleware"""
+    return ProxyMiddleware(app, proxy)


Property changes on: z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware/proxy.py
___________________________________________________________________
Added: svn:keywords
   + Id
Added: svn:mergeinfo
   + 

Added: z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware/security.py
===================================================================
--- z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware/security.py	                        (rev 0)
+++ z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware/security.py	2009-08-16 08:59:35 UTC (rev 102845)
@@ -0,0 +1,66 @@
+##############################################################################
+#
+# Copyright (c) 2009 Fabio Tranchitella <fabio at tranchitella.it>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+"""
+$Id: middleware.py 102811 2009-08-15 15:20:10Z kobold $
+"""
+
+from zope.interface import implements
+from zope.security.interfaces import IPrincipal, IParticipation
+from zope.security.management import newInteraction, endInteraction
+
+
+class Participation(object):
+    """Participation object
+    
+    Verify the class:
+
+        >>> from zope.interface.verify import verifyClass
+        >>> verifyClass(IParticipation, Participation)
+        True
+    
+    Verify the object:
+
+        >>> from zope.interface.verify import verifyObject
+        >>> obj = Participation(None)
+        >>> verifyObject(IParticipation, obj)
+        True
+ 
+    """
+
+    implements(IParticipation)
+
+    def __init__(self, principal):
+        self.principal = principal
+        self.interaction = None
+
+
+class SecurityMiddleware(object):
+    """WSGI middleware for integration with zope.security"""
+
+    def __init__(self, app, key):
+        self.app = app
+        self.key = key
+
+    def __call__(self, environ, start_response):
+        principal = IPrincipal(environ.get(self.key))
+        newInteraction(Participation(principal))
+        result = self.app(environ, start_response)
+        endInteraction()
+        return result
+
+
+def make_security_middleware(app, global_conf, key):
+    """Configure a ProxyMiddleware middleware"""
+    return SecurityMiddleware(app, key)

Deleted: z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware.py
===================================================================
--- z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware.py	2009-08-16 06:36:09 UTC (rev 102844)
+++ z3c.bobopublisher/trunk/src/z3c/bobopublisher/middleware.py	2009-08-16 08:59:35 UTC (rev 102845)
@@ -1,38 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2009 Fabio Tranchitella <fabio at tranchitella.it>
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-
-"""
-$Id$
-"""
-
-from zope.dottedname.resolve import resolve
-
-
-class ProxyMiddleware(object):
-    """WSGI application filter to add support for object proxy"""
-
-    def __init__(self, app, proxy):
-        self.app = app
-        if isinstance(proxy, str):
-            proxy = resolve(proxy)
-        self.proxy = proxy
-
-    def __call__(self, environ, start_response):
-        environ['bobopublisher.proxy'] = self.proxy
-        return self.app(environ, start_response)
-
-
-def make_proxy_middleware(app, global_conf, proxy):
-    """Configure a ProxyMiddleware middleware"""
-    return ProxyMiddleware(app, proxy)

Modified: z3c.bobopublisher/trunk/src/z3c/bobopublisher/tests.py
===================================================================
--- z3c.bobopublisher/trunk/src/z3c/bobopublisher/tests.py	2009-08-16 06:36:09 UTC (rev 102844)
+++ z3c.bobopublisher/trunk/src/z3c/bobopublisher/tests.py	2009-08-16 08:59:35 UTC (rev 102845)
@@ -29,6 +29,7 @@
 
 doctests = [
     'z3c.bobopublisher.absoluteurl',
+    'z3c.bobopublisher.middleware.security',
 ]
 
 

Modified: z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/PKG-INFO
===================================================================
--- z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/PKG-INFO	2009-08-16 06:36:09 UTC (rev 102844)
+++ z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/PKG-INFO	2009-08-16 08:59:35 UTC (rev 102845)
@@ -174,7 +174,7 @@
         In this example, we configure a proxied application to use the ProxyFactory
         from zope.security:
         
-        >>> from z3c.bobopublisher.middleware import ProxyMiddleware
+        >>> from z3c.bobopublisher.middleware.proxy import ProxyMiddleware
         >>> proxyapp = TestApp(ProxyMiddleware(Application(),
         ...     'zope.security.checker.ProxyFactory'))
         
@@ -186,6 +186,47 @@
         ForbiddenAttribute: ('get', <Root object at ...>)
         
         
+        Integration with zope.security
+        ------------------------------
+        
+        z3c.bobopublisher provides a middleware to ease the integration with
+        zope.security; it uses the value referenced by the given key in the WSGI
+        environment and adapts it to `zope.security.interfaces.IPrincipal':
+        
+        [filter-app:security]
+        use = egg:z3c.bobopublisher#security
+        key = remote_user
+        next = application
+        
+        In this example, we configure an application to use the remote_user key from
+        the WSGI environment for zope.security:
+        
+        >>> from z3c.bobopublisher.middleware.security import SecurityMiddleware
+        >>> secureapp = TestApp(SecurityMiddleware(Application(), 'remote_user'))
+        
+        Using the test application, we are able to call the page and get its result,
+        but we don't have any adapter registered for IPrincipal:
+        
+        >>> secureapp.get('/', extra_environ={'remote_user': 'test'}).body
+        Traceback (most recent call last):
+        ...
+        TypeError: ('Could not adapt', 'test', <InterfaceClass zope.security.interfaces.IPrincipal>)
+        
+        We register a dummy adapter for IPrincipal:
+        
+        >>> from zope.interface import Interface
+        >>> from zope.security.interfaces import IPrincipal
+        >>> from zope.security.testing import Principal
+        >>> def principal(key):
+        ...     return key == 'test' and Principal(u'test') or None
+        >>> getGlobalSiteManager().registerAdapter(principal, (Interface,), IPrincipal)
+        
+        Using the test application, we are able to call the page and get its result:
+        
+        >>> secureapp.get('/', extra_environ={'remote_user': 'test'}).body
+        'ABC'
+        
+        
         Defining browser pages with ZCML
         --------------------------------
         
@@ -241,7 +282,7 @@
         ---------
         
         z3c.bobopublisher provides a ZCML directive which can be used to publish static
-        resources from a directory in the filesystem:
+        resources from a directory or from a single file in the filesystem:
         
         >>> import os, tempfile
         >>> tempdir = tempfile.mktemp()
@@ -254,8 +295,12 @@
         ...       name="images"
         ...       directory="%s"
         ...       />
+        ...   <resource
+        ...       name="resource.txt"
+        ...       file="%s/resource.txt"
+        ...       />
         ... </configure>
-        ... """ % tempdir, context=context, execute=True)
+        ... """ % (tempdir, tempdir), context=context, execute=True)
         
         By the default resources are registered for the IRoot interface, as shown below:
         
@@ -276,6 +321,19 @@
         >>> 'Last-Modified' in response.headers
         True
         
+        Single file resources work in a similar way:
+        
+        >>> response = testapp.get('/resource.txt', status=200)
+        >>> response.content_type, response.charset, response.body
+        ('text/plain', 'UTF-8', 'RESOURCE')
+        
+        >>> response.headers['Cache-Control']
+        'public,max-age=86400'
+        >>> 'Expires' in response.headers
+        True
+        >>> 'Last-Modified' in response.headers
+        True
+        
         It is not possible to quit from the path of the resource directory:
         
         >>> testapp.get('/images/../images/resource.txt', status=404).body

Modified: z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/SOURCES.txt
===================================================================
--- z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/SOURCES.txt	2009-08-16 06:36:09 UTC (rev 102844)
+++ z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/SOURCES.txt	2009-08-16 08:59:35 UTC (rev 102845)
@@ -22,8 +22,10 @@
 src/z3c/bobopublisher/meta.zcml
 src/z3c/bobopublisher/metaconfigure.py
 src/z3c/bobopublisher/metadirectives.py
-src/z3c/bobopublisher/middleware.py
 src/z3c/bobopublisher/publication.py
 src/z3c/bobopublisher/resources.py
 src/z3c/bobopublisher/tests.py
-src/z3c/bobopublisher/traversing.py
\ No newline at end of file
+src/z3c/bobopublisher/traversing.py
+src/z3c/bobopublisher/middleware/__init__.py
+src/z3c/bobopublisher/middleware/proxy.py
+src/z3c/bobopublisher/middleware/security.py
\ No newline at end of file

Modified: z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/entry_points.txt
===================================================================
--- z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/entry_points.txt	2009-08-16 06:36:09 UTC (rev 102844)
+++ z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/entry_points.txt	2009-08-16 08:59:35 UTC (rev 102845)
@@ -2,5 +2,6 @@
     [paste.app_factory]
     main = z3c.bobopublisher.application:Application
     [paste.filter_app_factory]
-    proxy = z3c.bobopublisher.middleware:make_proxy_middleware
+    proxy = z3c.bobopublisher.middleware.proxy:make_proxy_middleware
+    security = z3c.bobopublisher.middleware.security:make_security_middleware
     
\ No newline at end of file



More information about the Checkins mailing list