Changeset 6265
- Timestamp:
- 05/01/10 14:56:55 (11 years ago)
- Location:
- TI12-security/trunk/NDGSecurity/python/ndg_security_server/ndg/security/server/wsgi
- Files:
-
- 2 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
TI12-security/trunk/NDGSecurity/python/ndg_security_server/ndg/security/server/wsgi/authz/__init__.py
r6264 r6265 16 16 from urlparse import urlunsplit 17 17 from httplib import UNAUTHORIZED, FORBIDDEN 18 19 from authkit.authenticate.multi import MultiHandler 18 20 19 21 from ndg.security.common.utils.classfactory import importClass … … 28 30 from ndg.security.server.wsgi import (NDGSecurityMiddlewareBase, 29 31 NDGSecurityMiddlewareConfigError) 30 from ndg.security.server.wsgi.authn import (SessionMiddlewareBase, 31 SessionHandlerMiddleware) 32 33 from ndg.security.server.wsgi.authz.result_handler.basic import \ 34 PEPResultHandlerMiddleware 32 from ndg.security.server.wsgi.session import (SessionMiddlewareBase, 33 SessionHandlerMiddleware) 35 34 36 35 from ndg.security.common.authz.msi import (Policy, PIP, PIPBase, … … 65 64 # Key names for PEP context information 66 65 PEPCTX_SESSION_KEYNAME = 'pepCtx' 67 PEPCTX_REQUEST_ KEYNAME = 'request'68 PEPCTX_RESPONSE_ KEYNAME = 'response'69 PEPCTX_TIMESTAMP_ KEYNAME = 'timestamp'66 PEPCTX_REQUEST_SESSION_KEYNAME = 'request' 67 PEPCTX_RESPONSE_SESSION_KEYNAME = 'response' 68 PEPCTX_TIMESTAMP_SESSION_KEYNAME = 'timestamp' 70 69 POLICY_FILEPATH_PARAMNAME = 'filePath' 71 70 … … 170 169 # Record the result in the user's session to enable later 171 170 # interrogation by the AuthZResultHandlerMiddleware 171 PEPFilter.setSession(session, request, response) 172 172 173 173 if response.status == Response.DECISION_PERMIT: … … 192 192 193 193 @classmethod 194 def setSession(cls, session, save=True): 194 def setSession(cls, session, request, response, save=True): 195 """Set PEP context information in the Beaker session using standard key 196 names 197 198 @param session: beaker session 199 @type session: beaker.session.SessionObject 200 @param request: authorisation request 201 @type request: ndg.security.common.authz.msi.Request 202 @param response: authorisation response 203 @type response: ndg.security.common.authz.msi.Response 204 @param save: determines whether session is saved or not 205 @type save: bool 206 """ 195 207 session[cls.PEPCTX_SESSION_KEYNAME] = { 196 cls.PEPCTX_REQUEST_ KEYNAME: request,197 cls.PEPCTX_RESPONSE_ KEYNAME: response,198 cls.PEPCTX_TIMESTAMP_ KEYNAME: time()208 cls.PEPCTX_REQUEST_SESSION_KEYNAME: request, 209 cls.PEPCTX_RESPONSE_SESSION_KEYNAME: response, 210 cls.PEPCTX_TIMESTAMP_SESSION_KEYNAME: time() 199 211 } 200 212 … … 601 613 602 614 return attributeResponse 603 604 605 from authkit.authenticate.multi import MultiHandler 615 606 616 607 617 class AuthorizationMiddlewareError(Exception): 608 618 """Base class for AuthorizationMiddlewareBase exceptions""" 619 609 620 610 621 class AuthorizationMiddlewareConfigError(Exception): 611 622 """AuthorizationMiddlewareBase configuration related exceptions""" 612 623 624 625 # Import here to avoid import error 626 from ndg.security.server.wsgi.authz.result_handler.basic import \ 627 PEPResultHandlerMiddleware 613 628 614 629 class AuthorizationMiddlewareBase(NDGSecurityMiddlewareBase): -
TI12-security/trunk/NDGSecurity/python/ndg_security_server/ndg/security/server/wsgi/authz/result_handler/basic.py
r6264 r6265 18 18 19 19 from ndg.security.server.wsgi import NDGSecurityMiddlewareBase 20 from ndg.security.server.wsgi. authn import SessionMiddlewareBase20 from ndg.security.server.wsgi.session import SessionMiddlewareBase 21 21 from ndg.security.server.wsgi.authz import PEPFilter 22 22 … … 71 71 # Get response message from PDP recorded by PEP 72 72 pepCtx = self.session.get(PEPFilter.PEPCTX_SESSION_KEYNAME, {}) 73 pdpResponse = pepCtx.get(PEPFilter.PEPCTX_ _RESPONSE_SESSION_KEYNAME)74 msg = getattr(pdpResponse, 'message', '') 73 pdpResponse = pepCtx.get(PEPFilter.PEPCTX_RESPONSE_SESSION_KEYNAME) 74 msg = getattr(pdpResponse, 'message', '') or '' 75 75 76 76 response = ("Access is forbidden for this resource:%s" -
TI12-security/trunk/NDGSecurity/python/ndg_security_server/ndg/security/server/wsgi/authz/result_handler/genshi.py
r6264 r6265 11 11 __revision__ = "$Id: $" 12 12 __license__ = "BSD - see LICENSE file in top-level directory" 13 from string import Template 14 from genshi.template import TemplateLoader 15 16 from httplib import UNAUTHORIZED, FORBIDDEN 17 18 from ndg.security.server.wsgi import NDGSecurityMiddlewareBase 19 from ndg.security.server.wsgi.session import SessionMiddlewareBase 20 21 22 class GenshiPEPResultHandlerMiddleware(SessionMiddlewareBase): 23 """Genshi based PEP result handler 24 """ 25 PROPERTY_NAMES = ( 26 'messageTemplate', 27 'templateName', 28 'templateRootDir' 29 ) 30 __slots__ = PROPERTY_NAMES 31 32 DEFAULT_TMPL_NAME = 'accessdenied.html' 33 DEFAULT_TMPL_DIR = path.join(path.dirname(__file__), 'templates') 34 35 MSG_TMPL = ( 36 "Access is forbidden for this resource:<br/><br/>" 37 "$pdpResponseMsg<br/><br/>" 38 "Please check with your site administrator that you have the required " 39 "access privileges." 40 ) 41 42 def __init__(self, app, global_conf, prefix='', **app_conf): 43 ''' 44 @type app: callable following WSGI interface 45 @param app: next middleware application in the chain 46 @type global_conf: dict 47 @param global_conf: PasteDeploy global configuration dictionary 48 @type prefix: basestring 49 @param prefix: prefix for configuration items 50 @type app_conf: dict 51 @param app_conf: PasteDeploy application specific configuration 52 dictionary 53 ''' 54 super(GenshiPEPResultHandlerMiddleware, self).__init__(app, 55 global_conf, 56 prefix=prefix, 57 **app_conf) 58 59 # Initialise attributes 60 for i in GenshiRendering.PROPERTY_NAMES: 61 setattr(self, i, '') 62 63 # Update from keywords 64 for i in app_conf: 65 setattr(self, i, app_conf[i]) 66 67 if not self.templateRootDir: 68 self.templateRootDir = \ 69 GenshiPEPResultHandlerMiddleware.DEFAULT_TMPL_DIR 70 71 if not self.messageTemplate: 72 self.messageTemplate = GenshiPEPResultHandlerMiddleware.MSG_TMPL 73 74 if not self.templateName: 75 self.templateName = \ 76 GenshiPEPResultHandlerMiddleware.DEFAULT_TMPL_NAME 77 78 self.__loader = TemplateLoader(self.templateRootDir, auto_reload=True) 79 80 @NDGSecurityMiddlewareBase.initCall 81 def __call__(self, environ, start_response): 82 83 if not self.isAuthenticated: 84 # sets 401 response to be trapped by authentication handler 85 log.warning("PEPResultHandlerMiddleware: user is not " 86 "authenticated - setting HTTP 401 response") 87 return self._setErrorResponse(code=UNAUTHORIZED) 88 else: 89 # Get response message from PDP recorded by PEP 90 pepCtx = self.session.get(PEPFilter.PEPCTX_SESSION_KEYNAME, {}) 91 pdpResponse = pepCtx.get(PEPFilter.PEPCTX_RESPONSE_SESSION_KEYNAME) 92 pdpResponseMsg = getattr(pdpResponse, 'message', '') or '' 93 94 msg = Template(self.messageTemplate).substitute( 95 pdpResponseMsg=pdpResponseMsg) 96 97 response = self._render(xml=msg) 98 start_response( 99 GenshiPEPResultHandlerMiddleware.getStatusMessage(FORBIDDEN), 100 [('Content-type', 'text/html'), 101 ('Content-Length', str(len(response)))]) 102 103 return response 104 105 def __setattr__(self, name, value): 106 """Apply some generic type checking""" 107 if name in GenshiRendering.PROPERTY_NAMES: 108 if not isinstance(value, basestring): 109 raise TypeError('Expecting string type for %r attribute; got ' 110 '%r' % (name, type(value))) 111 112 super(GenshiPEPResultHandlerMiddleware, self).__setattr__(name, value) 113 114 def _getLoader(self): 115 return self.__loader 116 117 def _setLoader(self, value): 118 if not isinstance(value, TemplateLoader): 119 raise TypeError('Expecting %r type for "loader"; got %r' % 120 (TemplateLoader, type(value))) 121 self.__loader = value 122 123 loader = property(_getLoader, _setLoader, 124 doc="Genshi TemplateLoader instance") 125 126 def _render(self, **kw): 127 '''Wrapper for Genshi template rendering 128 @type kw: dict 129 @param kw: keywords to pass to template 130 @rtype: string 131 @return: rendered template 132 ''' 133 tmpl = self.loader.load(self.templateName) 134 rendering = tmpl.generate(**kw).render('html', doctype='html') 135 136 return rendering -
TI12-security/trunk/NDGSecurity/python/ndg_security_server/ndg/security/server/wsgi/openid/provider/renderinginterface/genshi/__init__.py
r6245 r6265 84 84 @param *arg: RenderingInterface parent class arguments 85 85 @type **opt: dict 86 @param **opt: additional keywords to set-up Buffetrendering'''86 @param **opt: additional keywords to set-up Genshi rendering''' 87 87 super(GenshiRendering, self).__init__(*arg, **opt) 88 88 -
TI12-security/trunk/NDGSecurity/python/ndg_security_server/ndg/security/server/wsgi/session.py
r6264 r6265 14 14 log = logging.getLogger(__name__) 15 15 16 from ndg.security.server.wsgi import NDGSecurityMiddlewareBase 16 from ndg.security.server.wsgi import (NDGSecurityMiddlewareBase, 17 NDGSecurityMiddlewareError) 17 18 18 19 … … 36 37 37 38 38 class SessionHandlerMiddlewareError( AuthnException):39 class SessionHandlerMiddlewareError(NDGSecurityMiddlewareError): 39 40 """Base exception for SessionHandlerMiddleware""" 40 41
Note: See TracChangeset
for help on using the changeset viewer.