Skip to content Skip to sidebar Skip to footer

Python Decorator For Gae Web-service Security Check

In this post, Nick suggested a decoartor: Python/WebApp Google App Engine - testing for user/pass in the headers I'm writing an API to expose potentially dozens of methods as web-

Solution 1:

You need to write a method decorator, not a class decorator: As lost-theory points out, class decorators don't exist in Python 2.5, and they wouldn't work very well in any case, because the RequestHandler class isn't initialized with request data until after it's constructed. A method decorator also gives you more control - eg, you could allow GET requests unauthenticated, but still require authentication for POST requests.

Other than that, your decorator looks fine - just apply it to the relevant methods. The only change I would really suggest is replacing the .set_status() calls with .error() calls and remove the response.write calls; this allows you to override .error() on the RequestHandler class to output a nice error page for each possible status code.

Solution 2:

Class decorators were added in Python 2.6.

You'll have to manually wrap the class or think of another solution to work under 2.5. How about writing a decorator for the get method instead?

Post a Comment for "Python Decorator For Gae Web-service Security Check"