source: TI12-security/trunk/python/ndg.security.server/ndg/security/server/wsgi/attributeauthority.py @ 5644

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.server/ndg/security/server/wsgi/attributeauthority.py@5644
Revision 5644, 3.5 KB checked in by pjkersha, 11 years ago (diff)

Added ndg.security.server.wsgi.attributeauthority.AttributeAuthorityMiddleware?: sets an ndg.security.server.attributeauthority.AttributeAuthority? instance as a key in environ.

Line 
1"""WSGI Middleware to set an Attribute Authority instance in tyhe WSGI environ
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "19/08/09"
7__copyright__ = "(C) 2009 Science and Technology Facilities Council"
8__contact__ = "Philip.Kershaw@stfc.ac.uk"
9__license__ = "BSD - see LICENSE file in top-level directory"
10__revision__ = "$Id$"
11import logging
12log = logging.getLogger(__name__)
13import os
14
15from ndg.security.server.wsgi import NDGSecurityMiddlewareBase
16from ndg.security.server.attributeauthority import AttributeAuthority
17
18
19class AttributeAuthorityMiddleware(NDGSecurityMiddlewareBase):
20    '''WSGI to add an NDG Security Attribute Authority in the environ
21    '''
22    DEFAULT_KEYNAME = 'ndg.security.server.attributeauthority'
23    ENVIRON_KEYNAME_PARAMNAME = 'environKeyName'
24       
25    def __init__(self, app, global_conf, prefix='attributeauthority.',
26                 **app_conf):
27        '''Set-up an Attribute Authority instance
28       
29        @type app: callable following WSGI interface
30        @param app: next middleware application in the chain     
31        @type global_conf: dict       
32        @param global_conf: PasteDeploy global configuration dictionary
33        @type prefix: basestring
34        @param prefix: prefix for configuration items
35        @type app_conf: dict       
36        @param app_conf: PasteDeploy application specific configuration
37        dictionary
38        '''
39        # Stop in debugger at beginning of SOAP stub if environment variable
40        # is set
41        self.__debug = bool(os.environ.get('NDGSEC_INT_DEBUG'))
42        if self.__debug:
43            import pdb
44            pdb.set_trace()
45       
46        self._app = app
47
48        # Set key name for attribute authority set in environ
49        environKeyOptName = prefix + \
50                        AttributeAuthorityMiddleware.ENVIRON_KEYNAME_PARAMNAME
51        self.__keyName = app_conf.pop(environKeyOptName,
52                                AttributeAuthorityMiddleware.DEFAULT_KEYNAME)
53       
54        self.__aa = AttributeAuthority.fromProperties(propPrefix=prefix,
55                                                      **app_conf)
56       
57    def __call__(self, environ, start_response):
58        '''Set the Attribute Authority instantiated at initialisation in
59        environ
60       
61        @type environ: dict
62        @param environ: WSGI environment variables dictionary
63        @type start_response: function
64        @param start_response: standard WSGI start response function
65        @rtype: iterable
66        @return: next application in the WSGI stack
67        '''
68        environ[self.keyName] = self.aa
69        return self._app(environ, start_response)
70   
71    def _get_aa(self):
72        return self.__aa
73   
74    def _set_aa(self, val):
75        if not isinstance(val, AttributeAuthority):
76            raise TypeError('Expecting %r for "aa" attribute; got %r' %
77                            (AttributeAuthority, type(val)))
78           
79    aa = property(fget=_get_aa,
80                  fset=_set_aa,
81                  doc="Attribute Authority instance")
82
83    def _getKeyName(self):
84        return self.__keyName
85
86    def _setKeyName(self, val):
87        if not isinstance(val, basestring):
88            raise TypeError('Expecting %r for "keyName" attribute; got %r' %
89                            (basestring, type(val)))
90        self.__keyName = val
91       
92    keyName = property(fget=_getKeyName, 
93                       fset=_setKeyName, 
94                       doc="Key name used to index Attribute Authority in "
95                           "environ dictionary")
Note: See TracBrowser for help on using the repository browser.