source: TI12-security/trunk/python/ndg.security.server/ndg/security/server/sso/sso/config/ssoServiceMiddleware.py @ 3896

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.server/ndg/security/server/sso/sso/config/ssoServiceMiddleware.py@3896
Revision 3896, 5.8 KB checked in by pjkersha, 12 years ago (diff)

Updates for ndg.security.common.authz.pep and ndg.security.common.authz.pdp for BrowsePDP Gatekeeper code for ows_server.

Line 
1'''
2Security middleware - set-up configuration items
3
4P J Kershaw 18/03/08
5'''
6from os.path import expandvars as xpdvars
7import logging
8log = logging.getLogger(__name__)
9
10class ndg:
11    '''Class structure to define a namespace for SSO Service config attached
12    Pylons global variable 'g'
13    '''
14    class security:
15        class server:
16            class ssoservice:
17                cfg = None
18        class client:
19            '''Client class is also needed for BaseController handler to handle
20            responses from Single Sign On IdP'''
21            class ssoclient:
22                class cfg:
23                    '''Placeholder for server and sslServer attributes'''
24
25class SSOMiddleware:
26           
27    def __init__(self, app, cfg, appGlobals, **kw):
28        log.debug("SSOMiddleware.__init__ ...")
29        self.app = app
30        ndg.security.server.ssoservice.cfg = SSOServiceConfig(cfg, **kw)
31       
32        # Copy into client for the benefit of
33        # ndg.security.client.ssoclient.ssoclient.lib.base.BaseController
34        # used to process responses back from SSO IdP
35        ndg.security.client.ssoclient.cfg.server = \
36            ndg.security.server.ssoservice.cfg.server
37        ndg.security.client.ssoclient.cfg.sslServer = \
38            ndg.security.server.ssoservice.cfg.sslServer
39           
40        appGlobals.ndg = ndg
41         
42    def __call__(self, environ, start_response):
43       
44        return self.app(environ, start_response)
45
46
47import sys
48from ConfigParser import SafeConfigParser as ConfigParser
49from ndg.security.common.wssecurity import WSSecurityConfig
50
51class SSOServiceConfigError(Exception):
52    """Handle errors from parsing security config items"""
53       
54class SSOServiceConfig(object):
55    """Get Security related parameters from the Pylons NDG config file"""
56
57    def __init__(self, cfg=None, **parseKw):
58        '''Get PKI settings for Attribute Authority and Session Manager from
59        the configuration file
60       
61        @type cfg: config file object or string
62        @param cfg: reference to NDG configuration file or config file object
63        '''
64       
65        self.wss = {}
66       
67        if isinstance(cfg, basestring):
68            # Assume file path to be read
69            self.read(cfg)
70        else:
71            # Assume existing config type object
72            self.cfg = cfg
73
74        if self.cfg:
75            self.parse(**parseKw)
76
77       
78    def read(self, cfgFilePath):
79        '''Read content of config file into object'''
80        self.cfg = ConfigParser()
81        self.cfg.read(cfgFilePath)
82 
83
84    def parse(self, 
85              defSection='DEFAULT', 
86              layoutSection='layout',
87              wssSection='NDG_SECURITY.wssecurity'):
88        '''Extract content of config file object into self'''
89             
90        if self.cfg.has_option(defSection, 'tracefile'):       
91            self.tracefile = eval(self.cfg.get(defSection,'tracefile'))   
92        else:
93            self.tracefile = None
94           
95        self.smURI = self.cfg.get(defSection, 'sessionMgrURI')       
96        self.aaURI = self.cfg.get(defSection, 'attAuthorityURI')
97       
98        # ... for SSL connections to security web services
99        try:
100            self.sslCACertFilePathList = \
101            xpdvars(self.cfg.get(defSection, 'sslCACertFilePathList')).split()
102               
103        except AttributeError:
104            raise SSOServiceConfigError, \
105                        'No "sslCACertFilePathList" security setting'
106
107        # If no separate WS-Security config file is set then read these params
108        # from the current config file
109        if self.cfg.has_option(defSection, 'wssCfgFilePath'):
110            path = self.cfg.get(defSection,'wssCfgFilePath', None) 
111            wssCfgFilePath = xpdvars(path)
112        else:
113            wssCfgFilePath = None
114           
115        wss = WSSecurityConfig(cfg=wssCfgFilePath or self.cfg)
116        wss.parse(section=wssSection)
117
118       
119        # Cast to standard dict because WSSecurityConfig object can't be
120        # passed via **kw and dict(wss) doesn't work
121        # TODO: check for cleaner solution - dict(wss)
122        self.wss = dict(wss.items())
123
124
125        # Hostname
126        self.server = self.cfg.get(defSection, 'server', '')
127
128        # For secure connections
129        self.sslServer = self.cfg.get(defSection, 'sslServer', '')
130       
131        # These URLs are referred from template files
132        self.getCredentials = '%s/getCredentials' % self.sslServer       
133        self.logoutURI = '%s/logout' % self.server
134                     
135        # Where Are You From URI         
136        self.wayfuri='%s/wayf' % self.server
137
138        self.localLink=self.cfg.get(layoutSection, 'localLink', None)
139        self.localImage=self.cfg.get(layoutSection, 'localImage', None)
140        self.localAlt=self.cfg.get(layoutSection, 'localAlt', 'Visit Local Site')
141        self.ndgLink=self.cfg.get(layoutSection, 'ndgLink', 'http://ndg.nerc.ac.uk')
142        self.ndgImage=self.cfg.get(layoutSection, 'ndgImage', None)
143        self.ndgAlt=self.cfg.get(layoutSection, 'ndgAlt','Visit NDG')
144        self.stfcLink=self.cfg.get(layoutSection, 'stfcLink')
145        self.stfcImage=self.cfg.get(layoutSection, 'stfcImage')
146        self.helpIcon=self.cfg.get(layoutSection, 'helpIcon')
147        self.LeftAlt=self.cfg.get(layoutSection, 'HdrLeftAlt')
148        self.LeftLogo=self.cfg.get(layoutSection, 'HdrLeftLogo')
149        self.pageLogo="bodcHdr"
150        self.icons_xml=self.cfg.get(layoutSection,'Xicon')
151        self.icons_plot=self.cfg.get(layoutSection,'plot')
152        self.icons_prn=self.cfg.get(layoutSection, 'printer')
153       
154        self.disclaimer = self.cfg.get('DEFAULT', 'disclaimer')
155           
156           
157    def __repr__(self):
158        return '\n'.join(["%s=%s" % (k,v) for k,v in self.__dict__.items() \
159                if k[:2] != "__"])
160   
Note: See TracBrowser for help on using the repository browser.