Changeset 71

Show
Ignore:
Timestamp:
05/28/07 14:52:00
Author:
thejimmyg
Message:

Added sqlalchemy user API driver, as well as new permissions - IPFrom and BetweenTimes? and upgraded template code to use a function

Files:

Legend:

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

    r69 r71  
    4848from paste.httpexceptions import HTTPExceptionHandler 
    4949 
     50from authkit.authorize import authorize_request 
     51from authkit.permissions import RemoteUser, no_authkit_users_in_environ, AuthKitConfigError 
     52 
    5053# 
    5154# Setting up logging 
     
    5861# 
    5962 
    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 
    7365 
    7466# 
     
    314306    ``template_obj`` 
    315307        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 looked  
    318     for. This means the code can be used to load templates for various different 
    319     authentication methods with different config options names. 
    320  
    321308 
    322309    authkit.method.form.template.string =  
     
    351338            ) 
    352339    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']) 
    357341        if not template: 
    358342            raise AuthKitConfigError( 
     
    366350    if not template: 
    367351        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 
    368356    return template 
    369357     
     
    695683        raise NotImplementedError('No %r method has been implemented'%method) 
    696684            
    697 from authkit.authorize import authorize_request 
    698 from authkit.permissions import RemoteUser 
     685 
    699686 
    700687def sample_app(environ, start_response): 
  • AuthKit/branches/0.4/authkit/authenticate/form.py

    r69 r71  
    1212from authkit.authenticate.multi import MultiHandler, status_checker 
    1313 
    14 template = """\ 
     14def template(): 
     15    return """\ 
    1516<html> 
    1617  <head><title>Please Sign In</title></head> 
     
    6566                    return self.on_authorized(environ, start_response) 
    6667 
    67         content = self.template % construct_url(environ) 
     68        content = self.template() % construct_url(environ) 
    6869        start_response("401 Unauthorized",[('Content-Type', 'text/html'+self.charset), 
    6970                                 ('Content-Length', str(len(content)))]) 
  • AuthKit/branches/0.4/authkit/authenticate/open_id.py

    r69 r71  
    2424from authkit.authenticate.multi import MultiHandler, status_checker 
    2525 
    26 template = """\ 
     26def template(): 
     27    return """\ 
    2728<html> 
    2829  <head><title>Please Sign In</title></head> 
     
    4748def render(template, **p): 
    4849    if sys.version_info >= (2,4): 
    49         return string.Template(template).substitute( 
     50        return string.Template(template()).substitute( 
    5051            **p 
    5152        ) 
    5253    else: 
    5354        for k, v in p.items(): 
    54             template = template.replace('$'+k, v) 
     55            template = template().replace('$'+k, v) 
    5556        return template 
    5657 
  • AuthKit/branches/0.4/authkit/permissions.py

    r68 r71  
    2323from authkit.authorize import PermissionError, NotAuthenticatedError 
    2424from authkit.authorize import NotAuthorizedError, middleware 
    25 from authkit.authenticate import no_authkit_users_in_environ  
    26  
     25 
     26import datetime 
    2727import logging 
    2828log = logging.getLogger('authkit.permissions') 
     29 
     30class AuthKitConfigError(Exception):  
     31    """ 
     32    Raised when there is a problem with the 
     33    configuration options chosen for the authenticate middleware 
     34    """ 
     35    pass 
     36     
     37no_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) 
    2943 
    3044#  
    3145# Permission Classes 
    3246# 
    33  
    3447 
    3548class Permission(object): 
     
    190203        In this implementation role names are case insensitive. 
    191204        """ 
    192         if not environ.has_key('authkit.users'): 
     205         
     206        if not environ.get('authkit.users'): 
    193207            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: 
    196210                raise self.error 
    197211            raise NotAuthenticatedError('Not authenticated') 
     212         
    198213        users = environ['authkit.users'] 
    199214        if not users.user_exists(environ['REMOTE_USER']): 
     
    245260        In this implementation group names are case insensitive. 
    246261        """ 
    247         if not environ.has_key('authkit.users'): 
     262        if not environ.get('authkit.users'): 
    248263            raise no_authkit_users_in_environ 
    249         if not environ.has_key('REMOTE_USER'): 
     264        if not environ.get('REMOTE_USER'): 
    250265            if self.error:  
    251266                raise self.error 
     
    281296        if not environ.has_key('authkit.users'): 
    282297            raise no_authkit_users_in_environ 
    283         if not environ.has_key('REMOTE_USER'): 
     298        if not environ.get('REMOTE_USER'): 
    284299            raise NotAuthenticatedError('Not Authenticated') 
    285300        if not environ['authkit.users'].user_exists(environ['REMOTE_USER']): 
     
    289304        return app(environ, start_response) 
    290305 
     306class 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 
     326class 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  
    1616from authkit.authenticate import middleware 
    1717from authkit.authorize import authorize 
    18 from authkit.users.database import UsersFromDatabase 
     18from authkit.users.sqlalchemy_driver import UsersFromDatabase 
    1919from authkit.permissions import ValidAuthKitUser, HasAuthKitRole, HasAuthKitGroup 
    2020 
     
    7878    setup_method='form,cookie', 
    7979    cookie_secret='secret encryption string', 
    80     form_authenticate_user_type = "authkit.users.database.UsersFromDatabase", 
     80    form_authenticate_user_type = "authkit.users.sqlalchemy_driver:UsersFromDatabase", 
    8181    form_authenticate_user_data = "model", 
    8282    cookie_signoutpath = '/signout', 
  • AuthKit/branches/0.4/examples/user/database/create.py

    r67 r71  
    11import model 
    2 from authkit.users.database import UsersFromDatabase 
     2from authkit.users.sqlalchemy_driver import UsersFromDatabase 
    33 
    44users = UsersFromDatabase(model)