Changeset 4609 for TI12-security/trunk/python
- Timestamp:
- 12/12/08 10:36:33 (12 years ago)
- Location:
- TI12-security/trunk/python
- Files:
-
- 14 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TI12-security/trunk/python/ndg.security.server/ndg/security/server/wsgi/sslclientauthn.py
r4606 r4609 18 18 import logging 19 19 log = logging.getLogger(__name__) 20 import os 20 21 import httplib 21 from ndg.security.common.X509 import X509 Cert, X509CertError22 from ndg.security.common.X509 import X509Stack, X509Cert, X509CertError 22 23 23 24 class SSLClientAuthNMiddleware(object): … … 31 32 } 32 33 33 _isSSLClientCertSet = lambda self: \34 SSLClientAuthNMiddleware.sslClientCertKeyName in self._environ34 _isSSLClientCertSet = lambda self: bool(self._environ.get( 35 SSLClientAuthNMiddleware.sslClientCertKeyName)) 35 36 isSSLClientCertSet = property(fget=_isSSLClientCertSet, 36 37 doc="Check for client cert. set in environ") … … 45 46 46 47 opt = SSLClientAuthNMiddleware.propertyDefaults.copy() 47 if app_conf is not None: 48 # Update from application config dictionary - filter from using 49 # prefix 48 49 # If no prefix is set, there is no way to distinguish options set for 50 # this app and those applying to other applications 51 if app_conf is not None and prefix: 52 # Update from application config dictionary - filter using prefix 50 53 SSLClientAuthNMiddleware._filterOpts(opt, app_conf, prefix=prefix) 51 54 52 55 # Similarly, filter keyword input 53 SSLClientAuthNMiddleware._filterOpts(opt, kw, prefix=prefix)56 SSLClientAuthNMiddleware._filterOpts(opt, local_conf, prefix=prefix) 54 57 55 58 # Update options from keywords - matching app_conf ones will be … … 111 114 112 115 for caCertFilePath in caCertFilePathList: 113 self._caCertStack.push(X509.load_cert(caCertFilePath)) 116 x509Cert = X509Cert.Read(os.path.expandvars(caCertFilePath)) 117 self._caCertStack.push(x509Cert) 114 118 115 119 caCertFilePathList = property(fset=_setCACertsFromFileList, … … 117 121 "peer certificate must validate against " 118 122 "one") 123 def _getPathMatchList(self): 124 return self._pathMatchList 119 125 120 126 def _setPathMatchList(self, pathList): 121 '''Read CA certificates from file and add them to an X.509 Cert. 122 stack 123 127 ''' 124 128 @type pathList: list or tuple 125 129 @param pathList: list of URL paths to apply SSL client authentication … … 134 138 if isinstance(pathList, basestring): 135 139 # Try parsing a space separated list of file paths 136 pathList = pathList.split()140 self._pathMatchList = pathList.split() 137 141 138 142 elif not isinstance(pathList, (list, tuple)): 139 143 raise TypeError('Expecting a list or tuple for "pathMatchList"') 144 else: 145 self._pathMatchList = pathList 146 147 pathMatchList = property(fget=_getPathMatchList, 148 fset=_setPathMatchList, 149 doc='List of URL paths to which to apply SSL ' 150 'client authentication') 140 151 141 152 @classmethod … … 174 185 def __call__(self, environ, start_response): 175 186 176 self._path = environ.get('PATH_INFO').rstrip('/') 187 self._path = environ.get('PATH_INFO') 188 if self._path != '/': 189 self._path.rstrip('/') 190 191 self._environ = environ 177 192 178 193 if not self.pathMatch: 179 return self._setResponse() 180 181 self._environ = environ 182 183 if not self.sslClientCertSet: 184 return self._setErrorResponse() 185 186 if self.isValidClientCert(): 187 return self._setResponse() 188 else: 189 return self._setErrorResponse() 190 191 def _setResponse(self): 194 log.debug("ignoring path [%s]" % self._path) 195 return self._setResponse(environ, start_response) 196 197 if not self.isSSLClientCertSet: 198 log.error("No SSL Client path set for request to [%s]"%self._path) 199 return self._setErrorResponse(environ, start_response, 200 msg='No client SSL Certificate set') 201 202 if self.isValidClientCert(environ): 203 return self._setResponse(environ, start_response) 204 else: 205 return self._setErrorResponse(environ, start_response) 206 207 def _setResponse(self, environ, start_response): 192 208 if self._app: 193 209 return self._app(environ, start_response) … … 200 216 return response 201 217 202 def _setErrorResponse(self, msg='Invalid SSL client certificate'): 218 def _setErrorResponse(self, environ, start_response, 219 msg='Invalid SSL client certificate'): 203 220 response = msg 204 221 status = '%d %s' % (self.errorResponseCode, … … 210 227 return response 211 228 212 def isValidClientCert(self): 213 x509Cert = X509Cert.Parse(filePath) 229 def isValidClientCert(self, environ): 230 sslClientCert = environ[SSLClientAuthNMiddleware.sslClientCertKeyName] 231 x509Cert = X509Cert.Parse(sslClientCert) 214 232 215 233 if len(self._caCertStack) == 0:
Note: See TracChangeset
for help on using the changeset viewer.