CherryPy user authentication tool

I'm trying to set up an easy way to style methods in my CherryPy controller classes so that the user is redirected to the login page if they have not been authenticated yet. I was about to make a basic Python decorator, but the answer here suggested using the custom CherryPy tool instead. Therefore, I am trying to do this, but I cannot get it to work. Here is what I 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 is a page that should be restricted to authenticated users, so I have the following:

 @cherrypy.expose @cherrypy.tools.authenticate def home(self, **kwargs): tmpl = TemplateDir.get_template('home.mako') return tmpl.render() 

However, I get this error when I try to run my website:

 Traceback (most recent call last): File ".\example.py", line 3, in <module> from 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: The 'authenticate' Tool does not accept positional arguments; you must use keyword arguments. 

Edit: OK, if I change my use of a custom tool to have parentheses, I get another error.

 @cherrypy.expose @cherrypy.tools.authenticate() # Magic parentheses... def home(self, **kwargs): ... 

Now I 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: I have sessions enabled:

 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 I first load a page using my tool decorator in a web method, I get this error:

AttributeError: object 'module' does not have attribute 'session'

Then, when I reload the page, I get this error:

AttributeError: '_Serving' object does not have the 'session' attribute

Edit: even trying to do this in my controller class, I still get the error message "The module node does not have an attribute":

 class Root(BaseModule): _cp_config = {'tools.sessions.on': True} sess = cherrypy.session # Error here ... 
+4
source share
2 answers

I used the wrong hook. The change:

 cherrypy.tools.authenticate = cherrypy.Tool('on_start_resource', authenticate) 

To:

 cherrypy.tools.authenticate = cherrypy.Tool('before_handler', authenticate) 

The problem is fixed. Apparently my authenticate method was called before sessions were turned on, so it was not able to access cherrypy.session . I do not need any session tools for inclusion in my controllers; all that was needed was on my server-starting 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 my controller using the restricted method:

 @cherrypy.expose @cherrypy.tools.authenticate() def home(self, **kwargs): ... 
+5
source

Most likely the sessions are not included. Here is an example configuration file on the wiki session page or see tutorial number 7 .

0
source

All Articles