Ignore:
Timestamp:
30/11/10 22:26:05 (10 years ago)
Author:
pjkersha
Message:

Incomplete - task 16: NDG Security 2.x.x - incl. updated Paster templates

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TI12-security/trunk/NDGSecurity/python/ndg_security_server/ndg/security/server/wsgi/authn.py

    r7077 r7756  
    196196        raise NotImplementedError() 
    197197 
    198          
     198import webob 
     199 
     200class AuthenticationEnforcementFilter(object): 
     201    """Simple filter raises HTTP 401 response code if the requested URI matches 
     202    a fixed regular expression set in the start-up configuration.  If however, 
     203    REMOTE_USER is set in environ, the request is passed through to the next 
     204    middleware or terminating app 
     205    """ 
     206    REMOTE_USER_ENVVAR_NAME = 'REMOTE_USER' 
     207    INTERCEPT_URI_PAT_OPTNAME = 'interceptUriPat' 
     208    DEFAULT_INTERCEPT_URI_PAT = re.compile(".*") 
     209    RE_PAT_TYPE = type(DEFAULT_INTERCEPT_URI_PAT) 
     210     
     211    __slots__ = ('_app', '__interceptUriPat') 
     212     
     213    def __init__(self, app): 
     214        """Create attributes, initialising intercept URI to match all incoming 
     215        requests 
     216        """ 
     217        self.__interceptUriPat = self.__class__.DEFAULT_INTERCEPT_URI_PAT 
     218        self._app = app 
     219         
     220    @property 
     221    def interceptUriPat(self): 
     222        return self.__interceptUriPat 
     223     
     224    @interceptUriPat.setter 
     225    def interceptUriPat(self, value): 
     226        if isinstance(value, basestring): 
     227            self.__interceptUriPat = re.compile(value) 
     228             
     229        elif isinstance(value, self.__class__.RE_PAT_TYPE): 
     230            self.__interceptUriPat = value 
     231             
     232        else: 
     233            raise TypeError('Expecting string or RE pattern type for "' 
     234                            'RE_PAT_TYPE" attribute') 
     235     
     236    @classmethod 
     237    def filter_app_factory(cls, app, global_conf, **app_conf): 
     238        filter = cls(app) 
     239        if cls.INTERCEPT_URI_PAT_OPTNAME in app_conf: 
     240            filter.interceptUriPat = app_conf[cls.INTERCEPT_URI_PAT_OPTNAME] 
     241             
     242        return filter 
     243     
     244    def __call__(self, environ, start_response): 
     245        request = webob.Request(environ) 
     246        if not self.interceptUriPat.match(request.url): 
     247            return self._app(environ, start_response) 
     248         
     249        if self.__class__.REMOTE_USER_ENVVAR_NAME in environ: 
     250            return self._app(environ, start_response) 
     251        else: 
     252            response = webob.Response(body="401 Unauthorized", status=401) 
     253            return response(environ, start_response) 
     254         
     255                 
    199256class AuthnRedirectMiddleware(SessionMiddlewareBase): 
    200257    """Base class for Authentication HTTP redirect initiator and redirect 
Note: See TracChangeset for help on using the changeset viewer.