python - CherryPy Custom Tool for user authentication -
i'm trying set simple way of decorating methods in cherrypy controller classes user redirected login page if haven't authenticated yet. going basic python decorator, an answer here suggested use cherrypy custom tool instead. i'm trying that, can't work. here's have:
def authenticate(): user = cherrypy.session.get('user', none) if not user: raise cherrypy.httpredirect('/?errmsg=please%20log%20in%20first') cherrypy.tools.authenticate = cherrypy.tool('on_start_resource', authenticate)
the /home
page page should restricted authenticated users, have this:
@cherrypy.expose @cherrypy.tools.authenticate def home(self, **kwargs): tmpl = templatedir.get_template('home.mako') return tmpl.render()
however, error when try start web site:
traceback (most recent call last): file ".\example.py", line 3, in <module> controller.main import root file "c:\...\controller\main.py", line 9, in <module> class root(basemodule): file "c:\...\controller\main.py", line 19, in root @cherrypy.tools.authenticate file "c:\python26\lib\site-packages\cherrypy\_cptools.py", line 119, in __call__ % self._name) typeerror: 'authenticate' tool not accept positional arguments; must use keyword arguments.
edit: okay, if change use of custom tool have parentheses, different error.
@cherrypy.expose @cherrypy.tools.authenticate() # magic parentheses... def home(self, **kwargs): ...
now get:
traceback (most recent call last): file "c:\python26\lib\site-packages\cherrypy\_cprequest.py", line 625, in respond self.hooks.run('on_start_resource') file "c:\python26\lib\site-packages\cherrypy\_cprequest.py", line 97, in run hook() file "c:\python26\lib\site-packages\cherrypy\_cprequest.py", line 57, in __call__ return self.callback(**self.kwargs) file ".\example.py", line 40, in authenticate user = cherrypy.session.get('user', none) attributeerror: 'module' object has no attribute 'session'
edit: have sessions turned on:
cherrypy.tools.sessions.storage_type = 'file' cherrypy.tools.sessions.storage_path = r'%s\sessions' % curdir cherrypy.tools.sessions.timeout = 60 cherrypy.tree.mount(root(), "/", config={ '/static': { 'tools.staticdir.on':true, 'tools.staticdir.dir':r'%s\static' % curdir, }, '/': { 'tools.sessions.on':true, } })
when first load page custom tool decorator on web method, error:
attributeerror: 'module' object has no attribute 'session'
then when reload page, error:
attributeerror: '_serving' object has no attribute 'session'
edit: trying in controller class, still 'module object has no attribute session' error:
class root(basemodule): _cp_config = {'tools.sessions.on': true} sess = cherrypy.session # error here ...
i using wrong hook. changing:
cherrypy.tools.authenticate = cherrypy.tool('on_start_resource', authenticate)
to:
cherrypy.tools.authenticate = cherrypy.tool('before_handler', authenticate)
fixed problem. apparently authenticate
method getting called before sessions had been turned on, couldn't access cherrypy.session
. didn't need session-turn-on stuff in controllers; necessary following in server-start script:
def authenticate(): ... cherrypy.tools.authenticate = cherrypy.tool('before_handler', authenticate) cherrypy.tree.mount(root(), "/", config={ "/": { 'tools.sessions.on':true, 'tools.sessions.storage_type':'file', 'tools.sessions.storage_path':r'%s\sessions' % curdir, 'tools.sessions.timeout':60 }, ... })
then, in controller on restricted method:
@cherrypy.expose @cherrypy.tools.authenticate() def home(self, **kwargs): ...
Comments
Post a Comment