Changeset 60
- Timestamp:
- 03/12/07 21:56:33
- Files:
-
- AuthKit/branches/0.4/authkit/authenticate/__init__.py (modified) (3 diffs)
- AuthKit/branches/0.4/authkit/authorize.py (modified) (3 diffs)
- AuthKit/branches/0.4/authkit/permissions.py (modified) (2 diffs)
- AuthKit/branches/0.4/authkit/pylons_adaptors.py (modified) (3 diffs)
- AuthKit/branches/0.4/test/test.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
AuthKit/branches/0.4/authkit/authenticate/__init__.py
r59 r60 720 720 else: 721 721 raise NotImplementedError('No %r method has been implemented'%method) 722 722 723 from authkit.authorize import authorize 724 723 725 def test_app(environ, start_response): 724 726 """ … … 736 738 The path ``/`` always displays the environment. 737 739 """ 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': 741 743 start_response('200 OK', [('Content-type', 'text/plain')]) 742 744 if environ.has_key('REMOTE_USER'): … … 749 751 else: 750 752 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 25 25 26 26 from paste import httpexceptions 27 from authkit.permissions import PermissionSetupError 28 from authkit.authorize import NotAuthenticatedError, NotAuthorizedError 27 29 28 30 # … … 66 68 """ 67 69 pass 70 71 class _PermissionStartResponse: 72 def __init__(self, status, headers, exc_info=None): 73 pass 74 75 class _PermissionList(list): 76 def __iter__(self): 77 raise FiddledWith('Fiddled with response') 78 79 class _FiddledWith(Exception): 80 pass 81 82 68 83 69 84 # … … 129 144 return decorate 130 145 146 def 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 191 def 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 61 61 The base class for all request-based permissions 62 62 """ 63 # XXX Is this line needed? 64 require_response = False 63 pass 65 64 66 65 class _TestBadlyLabelledResponseBasedPermission(RequestPermission): … … 150 149 ``REMOTE_USER``. 151 150 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. 154 154 """ 155 155 AuthKit/branches/0.4/authkit/pylons_adaptors.py
r59 r60 19 19 from authkit.permissions import PermissionSetupError 20 20 from authkit.authorize import NotAuthenticatedError, NotAuthorizedError 21 from authkit.authorize import authorize_request as authkit_authorize_request 21 22 22 23 def authorize(permission): … … 31 32 return permission.check(app, request.environ, self.start_response) 32 33 return decorator(validate) 33 34 class _PermissionStartResponse:35 def __init__(self, status, headers, exc_info=None):36 pass37 38 class _PermissionList(list):39 def __iter__(self):40 raise FiddledWith('Fiddled with response')41 42 class _FiddledWith(Exception):43 pass44 34 45 35 def authorize_request(permission): … … 58 48 advanced permission checks. 59 49 """ 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) 89 51 90 52 def authorized(permission): AuthKit/branches/0.4/test/test.py
r59 r60 1 from authkit.authenticate import middleware, test_app 1 2 from paste.fixture import * 2 from paste.httpserver import serve3 from authkit.authenticate import middleware, test_app4 3 5 def test3(): 4 def simple_app(environ, start_response): 5 return test_app(environ, start_response) 6 6 7 def valid(environ, username, password): 8 return username==password 7 def 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 9 13 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
