Changeset 147

Show
Ignore:
Timestamp:
03/10/08 18:06:26
Author:
thejimmyg
Message:

Added get support to forms

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • AuthKit/trunk/CHANGELOG.txt

    r146 r147  
    440.4.1 (**svn**) 
    55 
     6* Added a form.method option so you can choose GET authentication for 
     7  cases when another piece of middleware has already parsed the form  
     8  variables (eg with repoze?). 
    69* Fixed bug in form action generation for non-standard ports 
    710* Fixed bug reported by Sam Gentle where remote addr is obtained from  
  • AuthKit/trunk/authkit/authenticate/form.py

    r146 r147  
    3030log = logging.getLogger('authkit.authenticate.form') 
    3131 
    32 def template(): 
    33     return """\ 
     32def template(method=False): 
     33    t = """\ 
    3434<html> 
    3535  <head><title>Please Sign In</title></head> 
     
    4848</html> 
    4949""" 
     50    if method is not False: 
     51        t = t.replace('post', method) 
     52    return t 
    5053 
    5154class FormAuthHandler(AuthKitAuthHandler, AuthFormHandler): 
     
    5558        charset=None, 
    5659        status="200 OK", 
     60        method='post', 
    5761        **p 
    5862    ): 
     
    6367        else: 
    6468            self.charset = '; charset='+charset 
     69        self.method = method 
    6570     
    6671    def on_authorized(self, environ, start_response): 
     
    7277        # multi handler 
    7378        username = environ.get('REMOTE_USER','') 
    74         if 'POST' == environ['REQUEST_METHOD']: 
    75             formvars = parse_formvars(environ, include_get_vars=False) 
    76             username = formvars.get('username') 
    77             password = formvars.get('password') 
    78             if username and password: 
    79                 if self.authfunc(environ, username, password): 
    80                     environ['AUTH_TYPE'] = 'form' 
    81                     environ['REMOTE_USER'] = username 
    82                     environ['REQUEST_METHOD'] = 'GET' 
    83                     environ['CONTENT_LENGTH'] = '' 
    84                     environ['CONTENT_TYPE'] = '' 
    85                     del environ['paste.parsed_formvars'] 
    86                     return self.on_authorized(environ, start_response) 
     79        formvars = parse_formvars(environ, include_get_vars=True) 
     80        username = formvars.get('username') 
     81        password = formvars.get('password') 
     82        if username and password: 
     83            if self.authfunc(environ, username, password): 
     84                log.debug("Username and password authenticated successfully") 
     85                environ['AUTH_TYPE'] = 'form' 
     86                environ['REMOTE_USER'] = username 
     87                environ['REQUEST_METHOD'] = 'GET' 
     88                environ['CONTENT_LENGTH'] = '' 
     89                environ['CONTENT_TYPE'] = '' 
     90                del environ['paste.parsed_formvars'] 
     91                return self.on_authorized(environ, start_response) 
     92            else: 
     93                log.debug("Username and password authentication failed") 
     94        else: 
     95            log.debug("Either username or password missing") 
    8796        action =  construct_url(environ) 
    8897        log.debug("Form action is: %s", action) 
    89         content = self.template() % action 
     98        if self.method == 'post': 
     99            content = self.template() % action 
     100        else: 
     101            content = self.template(method=self.method) % (action) 
     102             
    90103        # @@@ Tell Pylons error documents middleware not to intercept the  
    91104        # response 
     
    180193    ) 
    181194    charset=auth_conf.get('charset') 
    182     return app, {'authfunc':authfunc, 'template':template_, 'charset':charset}, None 
     195    method =auth_conf.get('method', 'post') 
     196    if method.lower() not in ['get','post']: 
     197        raise Exception('Form method should be GET or POST, not %s'%method) 
     198    return app, {'authfunc':authfunc, 'template':template_, 'charset':charset, 'method':method}, None 
    183199 
    184200def make_form_handler( 
     
    202218        authfunc=auth_handler_params['authfunc'],  
    203219        template=auth_handler_params['template'],  
    204         charset=auth_handler_params['charset'] 
     220        charset=auth_handler_params['charset'], 
     221        method=auth_handler_params['method'], 
    205222    ) 
    206223    app.add_checker('form', status_checker) 
  • AuthKit/trunk/examples/docs/form2.py

    r146 r147  
    1717    form_charset='UTF-8', 
    1818    cookie_signoutpath = '/signout', 
     19    form_method='get', 
    1920) 
    2021