Google Groups Home
Help | Sign in
Filters / Hooks
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Richard King  
View profile
 More options Jul 3, 7:17 pm
From: Richard King <rki...@gmail.com>
Date: Thu, 3 Jul 2008 16:17:43 -0700 (PDT)
Local: Thurs, Jul 3 2008 7:17 pm
Subject: Filters / Hooks
I would like to implement my own user authentication methods for an
application built with CherryPy.  To do this, I would like to know how
to run a method at the beginning of each request so that I can check
if the user has authenticated, and redirect otherwise.  I am new to
CherryPy and have seen references to Filters & Hooks to do such
things, but I cannot find any good examples of how to do so.  Please
help.  Thanks.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Brewer  
View profile
 More options Jul 3, 8:11 pm
From: "Robert Brewer" <fuman...@aminus.org>
Date: Thu, 3 Jul 2008 17:11:12 -0700
Local: Thurs, Jul 3 2008 8:11 pm
Subject: RE: [cherrypy-users] Filters / Hooks

Richard King wrote:
> I would like to implement my own user authentication methods for an
> application built with CherryPy.  To do this, I would like to know how
> to run a method at the beginning of each request so that I can check
> if the user has authenticated, and redirect otherwise.  I am new to
> CherryPy and have seen references to Filters & Hooks to do such
> things, but I cannot find any good examples of how to do so.  Please
> help.  Thanks.

http://www.cherrypy.org/wiki/CustomTools covers it pretty well. To run
something at the beginning of the request, use the 'on_start_resource'
hook just like the 'print_path' example does. To do user authentication,
you're probably going to be checking
cherrypy.request.headers['WWW-Authenticate'] and maybe raise
cherrypy.HTTPError(401) if they can't authenticate. See
cherrypy.lib.auth for some helper functions. Of course, if you just want
basic/digest auth, there are already builtin tools for that.

Robert Brewer
fuman...@aminus.org


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
arjuna  
View profile
 More options Jul 4, 1:27 am
From: arjuna <brahmafor...@gmail.com>
Date: Fri, 4 Jul 2008 10:57:49 +0530
Local: Fri, Jul 4 2008 1:27 am
Subject: Re: [cherrypy-users] Re: Filters / Hooks

Hi Robert,

I had hacked together a simple login a while ago, id like to replace it with
the library login that maybe more secure...Whats the best place to find the
user authentication library and related docs? Thanks...

> Of course, if you just want
>basic/digest auth, there are already builtin tools for that.

On 7/4/08, Robert Brewer <fuman...@aminus.org> wrote:

--
Best regards,
arjuna
http://www.brahmaforces.com

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard King  
View profile
 More options Jul 3, 11:35 pm
From: Richard King <rki...@gmail.com>
Date: Thu, 3 Jul 2008 20:35:40 -0700 (PDT)
Local: Thurs, Jul 3 2008 11:35 pm
Subject: Re: Filters / Hooks
Thank you for the quick response.  The CustomTools reference was
exactly what I needed.  One more question: what do I need to do to be
able to access the cherrypy.session variables from within my Custom
Tool?

On Jul 3, 6:11 pm, "Robert Brewer" <fuman...@aminus.org> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard King  
View profile
 More options Jul 4, 2:29 am
From: Richard King <rki...@gmail.com>
Date: Thu, 3 Jul 2008 23:29:59 -0700 (PDT)
Local: Fri, Jul 4 2008 2:29 am
Subject: Re: Filters / Hooks
Thanks Robert.  I appreciate the good reference and the quick
response.  As I have developed web apps in the past, using other
languages, I have generally handled user authentication as follows:

1) Write code which executes before each request to see if the user
has logged in.
2) On each request, check to see if a "user_id" session variable
exists, and if not redirect to the login page.
3) Submitting the login pages will execute code that checks the
provided username/password with the database
4) If the credentials were valid, save the "user_id" session variable
and redirect to the requested page; otherwise redirect to the login
page.

Is there a way that I can accomplish this type of logic in CherryPy,
or is there a better way?  It appears that in CherryPy I do not have
access to cherrypy.session from a 'on_start_resource' hook.  Thank you
for your help.

-Richard

On Jul 3, 6:11 pm, "Robert Brewer" <fuman...@aminus.org> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Brewer  
View profile
 More options Jul 4, 3:15 am
From: "Robert Brewer" <fuman...@aminus.org>
Date: Fri, 4 Jul 2008 00:15:34 -0700
Local: Fri, Jul 4 2008 3:15 am
Subject: RE: [cherrypy-users] Re: Filters / Hooks

Sessions are implemented with hooks and tools too, so you just have to
make sure your tool runs after session.init is called. Looks like that's
before_request_body, priority=50 by default. It can't really be any
earlier than that because it has to run after the request headers are
read and parsed. So run your tool after that; either
before_request_body, priority 75 or something, or before_handler.

You also need to lock the session while you read/write it. By default,
that happens before_handler. If you set sessions.locking = 'early' it'll
run before_request_body, priority=60. You can also set
sessions.locking='explicit' and call
cherrypy.serving.session.acquire_lock()/release_lock() on your own.

Robert Brewer
fuman...@aminus.org


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Brewer  
View profile
 More options Jul 4, 3:18 am
From: "Robert Brewer" <fuman...@aminus.org>
Date: Fri, 4 Jul 2008 00:18:12 -0700
Local: Fri, Jul 4 2008 3:18 am
Subject: RE: [cherrypy-users] Re: Filters / Hooks

> I had hacked together a simple login a while ago,
> id like to replace it with the library login that
> maybe more secure...Whats the best place to find
> the user authentication library and related docs?

http://www.cherrypy.org/wiki/BuiltinTools#tools.basic_auth and
http://www.cherrypy.org/wiki/BuiltinTools#tools.digest_auth are the two
I was talking about. Aside from those few paragraphs, the source code is
probably best...

Robert Brewer
fuman...@aminus.org


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google