Changeset 71
- Timestamp:
- 05/28/07 14:52:00
- Files:
-
- AuthKit/branches/0.4/authkit/authenticate/__init__.py (modified) (6 diffs)
- AuthKit/branches/0.4/authkit/authenticate/form.py (modified) (2 diffs)
- AuthKit/branches/0.4/authkit/authenticate/open_id.py (modified) (2 diffs)
- AuthKit/branches/0.4/authkit/permissions.py (modified) (5 diffs)
- AuthKit/branches/0.4/authkit/users/sqlalchemy_driver.py (added)
- AuthKit/branches/0.4/examples/user/database/app.py (modified) (2 diffs)
- AuthKit/branches/0.4/examples/user/database/create.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
AuthKit/branches/0.4/authkit/authenticate/__init__.py
r69 r71 48 48 from paste.httpexceptions import HTTPExceptionHandler 49 49 50 from authkit.authorize import authorize_request 51 from authkit.permissions import RemoteUser, no_authkit_users_in_environ, AuthKitConfigError 52 50 53 # 51 54 # Setting up logging … … 58 61 # 59 62 60 class AuthKitConfigError(Exception): 61 """ 62 Raised when there is a problem with the 63 configuration options chosen for the authenticate middleware 64 """ 65 pass 66 67 no_authkit_users_in_environ = AuthKitConfigError( 68 'No authkit.users object exists in the environment. You may have ' 69 'forgotton to specify a Users object or are using the the default ' 70 'valid_password() method in the authenticate middleware when you ' 71 'may have meant to specify your own.' 72 ) 63 64 73 65 74 66 # … … 314 306 ``template_obj`` 315 307 A paste eval_import string or callable which returns a string 316 317 ``base_part`` is added to the option name before the option is looked318 for. This means the code can be used to load templates for various different319 authentication methods with different config options names.320 321 308 322 309 authkit.method.form.template.string = … … 351 338 ) 352 339 elif template_conf.keys()[0] == 'obj': 353 template = eval_import(template_conf[base_part+'template_obj']) 354 if not isinstance(data, (str,unicode)): 355 # Call a function or class to generate the template too 356 template = template() 340 template = eval_import(template_conf['obj']) 357 341 if not template: 358 342 raise AuthKitConfigError( … … 366 350 if not template: 367 351 raise AuthKitConfigError("The template loaded did not contain any data") 352 if isinstance(template, (str, unicode)): 353 def render_template(): 354 return template 355 return render_template 368 356 return template 369 357 … … 695 683 raise NotImplementedError('No %r method has been implemented'%method) 696 684 697 from authkit.authorize import authorize_request 698 from authkit.permissions import RemoteUser 685 699 686 700 687 def sample_app(environ, start_response): AuthKit/branches/0.4/authkit/authenticate/form.py
r69 r71 12 12 from authkit.authenticate.multi import MultiHandler, status_checker 13 13 14 template = """\ 14 def template(): 15 return """\ 15 16 <html> 16 17 <head><title>Please Sign In</title></head> … … 65 66 return self.on_authorized(environ, start_response) 66 67 67 content = self.template % construct_url(environ)68 content = self.template() % construct_url(environ) 68 69 start_response("401 Unauthorized",[('Content-Type', 'text/html'+self.charset), 69 70 ('Content-Length', str(len(content)))]) AuthKit/branches/0.4/authkit/authenticate/open_id.py
r69 r71 24 24 from authkit.authenticate.multi import MultiHandler, status_checker 25 25 26 template = """\ 26 def template(): 27 return """\ 27 28 <html> 28 29 <head><title>Please Sign In</title></head> … … 47 48 def render(template, **p): 48 49 if sys.version_info >= (2,4): 49 return string.Template(template ).substitute(50 return string.Template(template()).substitute( 50 51 **p 51 52 ) 52 53 else: 53 54 for k, v in p.items(): 54 template = template .replace('$'+k, v)55 template = template().replace('$'+k, v) 55 56 return template 56 57 AuthKit/branches/0.4/authkit/permissions.py
r68 r71 23 23 from authkit.authorize import PermissionError, NotAuthenticatedError 24 24 from authkit.authorize import NotAuthorizedError, middleware 25 from authkit.authenticate import no_authkit_users_in_environ 26 25 26 import datetime 27 27 import logging 28 28 log = logging.getLogger('authkit.permissions') 29 30 class AuthKitConfigError(Exception): 31 """ 32 Raised when there is a problem with the 33 configuration options chosen for the authenticate middleware 34 """ 35 pass 36 37 no_authkit_users_in_environ = AuthKitConfigError( 38 'No authkit.users object exists in the environment. You may have ' 39 'forgotton to specify a Users object or are using the the default ' 40 'valid_password() method in the authenticate middleware when you ' 41 'may have meant to specify your own.' 42 ) 29 43 30 44 # 31 45 # Permission Classes 32 46 # 33 34 47 35 48 class Permission(object): … … 190 203 In this implementation role names are case insensitive. 191 204 """ 192 if not environ.has_key('authkit.users'): 205 206 if not environ.get('authkit.users'): 193 207 raise no_authkit_users_in_environ 194 if not environ. has_key('REMOTE_USER'):195 if self.error: 208 if not environ.get('REMOTE_USER'): 209 if self.error: 196 210 raise self.error 197 211 raise NotAuthenticatedError('Not authenticated') 212 198 213 users = environ['authkit.users'] 199 214 if not users.user_exists(environ['REMOTE_USER']): … … 245 260 In this implementation group names are case insensitive. 246 261 """ 247 if not environ. has_key('authkit.users'):262 if not environ.get('authkit.users'): 248 263 raise no_authkit_users_in_environ 249 if not environ. has_key('REMOTE_USER'):264 if not environ.get('REMOTE_USER'): 250 265 if self.error: 251 266 raise self.error … … 281 296 if not environ.has_key('authkit.users'): 282 297 raise no_authkit_users_in_environ 283 if not environ. has_key('REMOTE_USER'):298 if not environ.get('REMOTE_USER'): 284 299 raise NotAuthenticatedError('Not Authenticated') 285 300 if not environ['authkit.users'].user_exists(environ['REMOTE_USER']): … … 289 304 return app(environ, start_response) 290 305 306 class FromIP(RequestPermission): 307 """ 308 Checks that the remote host specified in the environment ``key`` is one 309 of the hosts specified in ``hosts``. 310 """ 311 def __init__(self, hosts, key='REMOTE_ADDR'): 312 self.hosts = hosts 313 if not isinstance(self.hosts, (list, tuple)): 314 self.hosts = [hosts] 315 self.key = key 316 317 def check(self, app, environ, start_response): 318 if not environ.has_key(self.key): 319 raise Exception( 320 "No such key %r in environ so cannot check the host"%self.key 321 ) 322 if not environ.get(self.key) in self.hosts: 323 raise NotAuthorizedError('Host %r not allowed'%environ.get(self.key)) 324 return app(environ, start_response) 325 326 class BetweenTimes(RequestPermission): 327 """ 328 Only grants access if the request is made on or after ``start`` and 329 before ``end``. Times should be specified as datetime.time objects. 330 """ 331 def __init__(self, start, end): 332 self.start = start 333 self.end = end 334 335 def check(self, app, environ, start_response): 336 today = datetime.datetime.now() 337 now = datetime.time(today.hour, today.minute, today.second, today.microsecond) 338 error = NotAuthorizedError("Not authorized at this time of day") 339 if self.end > self.start: 340 if now >= self.start and now < self.end: 341 return app(environ, start_response) 342 else: 343 raise error 344 else: 345 if now < datetime.time(23, 59, 59, 999999) and now >= self.start: 346 return app(environ, start_response) 347 elif now >= datetime.time(0) and now < self.end: 348 return app(environ, start_response) 349 else: 350 raise error AuthKit/branches/0.4/examples/user/database/app.py
r69 r71 16 16 from authkit.authenticate import middleware 17 17 from authkit.authorize import authorize 18 from authkit.users. databaseimport UsersFromDatabase18 from authkit.users.sqlalchemy_driver import UsersFromDatabase 19 19 from authkit.permissions import ValidAuthKitUser, HasAuthKitRole, HasAuthKitGroup 20 20 … … 78 78 setup_method='form,cookie', 79 79 cookie_secret='secret encryption string', 80 form_authenticate_user_type = "authkit.users. database.UsersFromDatabase",80 form_authenticate_user_type = "authkit.users.sqlalchemy_driver:UsersFromDatabase", 81 81 form_authenticate_user_data = "model", 82 82 cookie_signoutpath = '/signout', AuthKit/branches/0.4/examples/user/database/create.py
r67 r71 1 1 import model 2 from authkit.users. databaseimport UsersFromDatabase2 from authkit.users.sqlalchemy_driver import UsersFromDatabase 3 3 4 4 users = UsersFromDatabase(model)
