Changeset 60

Show
Ignore:
Timestamp:
03/12/07 21:56:33
Author:
thejimmyg
Message:

Some more changes, need further testing

Files:

Legend:

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

    r59 r60  
    720720    else: 
    721721        raise NotImplementedError('No %r method has been implemented'%method) 
    722              
     722            
     723from authkit.authorize import authorize 
     724 
    723725def test_app(environ, start_response): 
    724726    """ 
     
    736738    The path ``/`` always displays the environment. 
    737739    """ 
    738     if environ['PATH_INFO']=='/private' and not environ.has_key('REMOTE_USER')
    739         start_response('401 Not signed in', []
    740     elif environ['PATH_INFO'] == '/signout': 
     740    if environ['PATH_INFO']=='/private'
     741        authorize(environ, RemoteUser()
     742    if environ['PATH_INFO'] == '/signout': 
    741743        start_response('200 OK', [('Content-type', 'text/plain')]) 
    742744        if environ.has_key('REMOTE_USER'): 
     
    749751    else: 
    750752        start_response('200 OK', [('Content-type', 'text/plain')]) 
    751     return ['%s: %s\n'%(k,v) for k,v in environ.items()]     
    752  
     753    return ['You Have Access To This Page.\n\nHere is the environment...\n\n%s: %s\n'%(k,v) for k,v in environ.items()] 
     754 
  • AuthKit/branches/0.4/authkit/authorize.py

    r59 r60  
    2525 
    2626from paste import httpexceptions 
     27from authkit.permissions import PermissionSetupError 
     28from authkit.authorize import NotAuthenticatedError, NotAuthorizedError 
    2729 
    2830# 
     
    6668    """ 
    6769    pass 
     70 
     71class _PermissionStartResponse: 
     72    def __init__(self, status, headers, exc_info=None): 
     73        pass 
     74 
     75class _PermissionList(list): 
     76    def __iter__(self): 
     77        raise FiddledWith('Fiddled with response') 
     78     
     79class _FiddledWith(Exception): 
     80    pass 
     81 
     82 
    6883 
    6984# 
     
    129144    return decorate 
    130145 
     146def authorize_request(environ, permission): 
     147    """ 
     148    This function can be used within a controller action to ensure that no code  
     149    after the function call is executed if the user doesn't pass the permission 
     150    check specified by ``permission``. 
     151 
     152    .. Note :: 
     153 
     154        Unlike the ``authorize()`` decorator or 
     155        ``authkit.authorize.middleware`` middleware, this function has no 
     156        access to the WSGI response so cannot be used to check response-based 
     157        permissions.  Since almost all AuthKit permissions are request-based 
     158        this shouldn't be a big problem unless you are defining your own  
     159        advanced permission checks. 
     160    """ 
     161    error = PermissionSetupError( 
     162        'The permissions being authorized require access to a response ' 
     163        'and so cannot be used to authorize based on a request alone. ' 
     164        'Try using the authkit.authorize.middleware or the authorize decorator.' 
     165    ) 
     166    if permission.require_response: 
     167        raise error 
     168    else: 
     169        try: 
     170            def dummy_app(environ, start_response): 
     171                if not start_response == _PermissionStartResponse: 
     172                    raise _FiddledWith('Fiddled with start_response %r'%start_response) 
     173                start_response( 
     174                    '1000 Test Response For Permissions Check',  
     175                    [('Content-type','text/plain')] 
     176                ) 
     177                return _PermissionList('''Dummy response from permission check.''') 
     178             
     179            if not isinstance( 
     180                permission.check( 
     181                    dummy_app,  
     182                    environ,  
     183                    _PermissionStartResponse 
     184                ),  
     185                _PermissionList 
     186            ): 
     187                raise _FiddledWith('Fiddled with response') 
     188        except _FiddledWith: 
     189            raise error 
     190 
     191def authorized(environ, permission): 
     192    try: 
     193        authorize_request(environ, permission) 
     194    except (NotAuthorizedError, NotAuthenticatedError): 
     195        return False 
     196    else: 
     197        return True 
     198 
  • AuthKit/branches/0.4/authkit/permissions.py

    r59 r60  
    6161    The base class for all request-based permissions 
    6262    """ 
    63     # XXX Is this line needed? 
    64     require_response = False 
     63    pass 
    6564 
    6665class _TestBadlyLabelledResponseBasedPermission(RequestPermission): 
     
    150149    ``REMOTE_USER``. 
    151150     
    152     If ``accept_empty`` is ``False`` (the default) then the value of the 
    153     ``REMOTE_USER`` must evaluate to ``True`` in Python. 
     151    If ``accept_empty`` is ``False`` (the default) then an empty ``REMOTE_USER`` 
     152    will not be accepted and the value of ``REMOTE_USER`` must evaluate to  
     153    ``True`` in Python. 
    154154    """ 
    155155 
  • AuthKit/branches/0.4/authkit/pylons_adaptors.py

    r59 r60  
    1919from authkit.permissions import PermissionSetupError 
    2020from authkit.authorize import NotAuthenticatedError, NotAuthorizedError 
     21from authkit.authorize import authorize_request as authkit_authorize_request 
    2122 
    2223def authorize(permission): 
     
    3132        return permission.check(app, request.environ, self.start_response) 
    3233    return decorator(validate) 
    33  
    34 class _PermissionStartResponse: 
    35     def __init__(self, status, headers, exc_info=None): 
    36         pass 
    37  
    38 class _PermissionList(list): 
    39     def __iter__(self): 
    40         raise FiddledWith('Fiddled with response') 
    41      
    42 class _FiddledWith(Exception): 
    43     pass 
    4434 
    4535def authorize_request(permission): 
     
    5848        advanced permission checks. 
    5949    """ 
    60     error = PermissionSetupError( 
    61         'The permissions being authorized require access to a response ' 
    62         'and so cannot be used to authorize based on a request alone. ' 
    63         'Try using the authkit.authorize.middleware or the authorize decorator.' 
    64     ) 
    65     if permission.require_response: 
    66         raise error 
    67     else: 
    68         try: 
    69             def dummy_app(environ, start_response): 
    70                 if not start_response == _PermissionStartResponse: 
    71                     raise _FiddledWith('Fiddled with start_response %r'%start_response) 
    72                 start_response( 
    73                     '1000 Test Response For Permissions Check',  
    74                     [('Content-type','text/plain')] 
    75                 ) 
    76                 return _PermissionList('''Dummy response from permission check.''') 
    77              
    78             if not isinstance( 
    79                 permission.check( 
    80                     dummy_app,  
    81                     request.environ,  
    82                     _PermissionStartResponse 
    83                 ),  
    84                 _PermissionList 
    85             ): 
    86                 raise _FiddledWith('Fiddled with response') 
    87         except _FiddledWith: 
    88             raise error 
     50    authkit_authorize_request(request.environ, permission) 
    8951 
    9052def authorized(permission): 
  • AuthKit/branches/0.4/test/test.py

    r59 r60  
     1from authkit.authenticate import middleware, test_app 
    12from paste.fixture import * 
    2 from paste.httpserver import serve 
    3 from authkit.authenticate import middleware, test_app 
    43 
    5 def test3(): 
     4def simple_app(environ, start_response): 
     5    return test_app(environ, start_response) 
    66 
    7     def valid(environ, username, password): 
    8         return username==password 
     7def test_ok(): 
     8    app = TestApp(simple_app) 
     9    res = app.get('') 
     10    assert res.header('content-type') == 'text/plain' 
     11    assert res.full_status == '200 OK' 
     12    assert 'signed in' in res 
    913 
    10     app = middleware( 
    11         test_app, 
    12         method='basic', 
    13         users_valid=valid, 
    14     ) 
    15     serve(app, host="0.0.0.0", port=8000) 
    16  
    17 def test4(): 
    18     app = middleware( 
    19         test_app, 
    20         config_file="config:test.ini", 
    21     ) 
    22     serve(app, host="0.0.0.0", port=8000) 
    23