Changeset 91

Show
Ignore:
Timestamp:
07/10/07 00:24:46
Author:
bbangert
Message:

Added capability to use the auth cookie's when present to retain the remote user. Added ability to check REMOTE_USER and use path protection.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • AuthKit/branches/0.4/authkit/authenticate/sso/api.py

    r88 r91  
    2525    """ The exception raised if the verification fails """ 
    2626 
     27 
    2728class RedirectingAuthHandler(object): 
    2829    """Handles generating redirect to SSO system 
     
    3536        url = self.redirect_url(environ) 
    3637        return HTTPSeeOther(url).wsgi_application(environ, start_response) 
     38 
    3739 
    3840class RedirectingAuthMiddleware(object): 
     
    4951                def __init__(self, app, path, use_options=False): 
    5052                    self.app = app 
     53                    self.protect = [] 
    5154                    self.dispatch = { 
    5255                        '/verify':'verify', 
     
    6770    def __call__(self, environ, start_response): 
    6871        path = environ['PATH_INFO'] 
     72         
    6973        if path in self.dispatch: 
    7074            return getattr(self, self.path +  
    7175                           self.dispatch[path])(environ, start_response) 
    72         else: 
    73             return self.app(environ, start_response) 
     76         
     77        for route in self.protect: 
     78            if path.startswith(route) and 'REMOTE_USER' not in environ \ 
     79               and 'type=' + self.type in environ['QUERY_STRING']: 
     80                return self.verify(environ, start_response) 
     81         
     82        return self.app(environ, start_response) 
    7483     
    7584    def verify(self): 
     
    8089        """Construct the redirect URL""" 
    8190        raise NotImplemented() 
     91 
    8292 
    8393def find_multi_app(app): 
  • AuthKit/branches/0.4/authkit/authenticate/sso/cas.py

    r89 r91  
    2323    CAS  
    2424    """ 
    25     def __init__(self, app, authority, path='casauth', use_cas2=False): 
     25    def __init__(self, app, authority, path='', use_cas2=False, protect=None): 
    2626        self.app = app 
    2727        self.authority = authority 
     
    3030     
    3131    def redirect_url(self, environ): 
    32         kwargs = {'service': construct_url(environ, script_name='',  
    33                                            path_info='/' + self.path + '/verify')} 
     32        kwargs = {'service': construct_url(environ, querystring='type=cas')} 
    3433         
    3534        # XXX TODO: Store this for the middleware below, also look for a proxy 
     
    4948class AuthCASMiddleware(RedirectingAuthMiddleware): 
    5049    """CAS 1.0 and 2.0 Capable Authentication Handler""" 
    51     def __init__(self, app, authority, use_cas2=False): 
     50    def __init__(self, app, authority, use_cas2=False, path='', protect=None): 
    5251        self.app = app 
    5352        self.authority = authority 
     53        self.type = 'cas' 
    5454        self._cas2 = use_cas2 
    5555        if use_cas2: 
     
    5757        else: 
    5858            self._authtype = 'CAS 1.0' 
    59         self.dispatch = {'/verify':'verify'} 
     59        self.protect = protect or [] 
     60        self.dispatch = {path + '/verify':'verify'} 
    6061         
    6162    def verify(self, environ, start_response): 
     
    6667        ticket = req.GET['ticket'] 
    6768         
    68         service = construct_url(environ
     69        service = construct_url(environ, querystring='type=cas'
    6970        kwargs = {'service': service, 'ticket':ticket} 
    7071        if req.environ.get('authkit.sso.cas.renew'): 
    7172            kwargs['renew'] = 'true' 
    7273        args = urllib.urlencode(kwargs) 
    73          
     74                 
    7475        # XXX TODO: Store whether renew was used for this request to ensure 
    7576        #           that the validation asks for it as well 
     
    8586            results = {} 
    8687            if success: 
     88                user_kwargs = {} 
    8789                results['user'] = tree[0][0].text 
    8890                results['extra_environ'] = {} 
     
    9597                    if len(tree[0] > 2): 
    9698                        proxies = [x.text.strip() for x in tree[0][2]] 
    97                         results['authkit.cas.proxies'] = proxies 
     99                        results['authkit.cas.proxies'] = proxies                 
    98100            else: 
    99101                log.info('Authentication failed for auth: %s, ticket %s, ' 
     
    116118        environ['REMOTE_USER'] = results['user'] 
    117119         
     120        set_user = req.environ.get('paste.auth_tkt.set_user') 
     121        if set_user: 
     122            set_user(results['user']) 
     123         
    118124        # Add in optional environ data from the auth system 
    119125        if 'extra_environ' in results: 
     
    129135    if 'use_cas2' in auth_conf: 
    130136        kwargs['use_cas2'] = True 
     137    kwargs['path'] = auth_conf.get('path', '') 
    131138    if 'path' in auth_conf: 
    132139        kwargs['path'] = auth_conf['path'] 
     140    if 'protect' in auth_conf: 
     141        kwargs['protect'] = auth_conf['protect'].split(',') 
    133142     
    134143    multi_app, app = find_multi_app(app)