Created
October 29, 2010 14:57
-
-
Save babo/653698 to your computer and use it in GitHub Desktop.
Example code to show that localhost:8888/test redirects to localhost:8888/ after authentication.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import datetime | |
import tornado.auth | |
import tornado.httpserver | |
import tornado.web | |
class LoginHandler(tornado.web.RequestHandler): | |
def get_current_user(self): | |
return self.get_secure_cookie("user") | |
class AuthHandler(LoginHandler, tornado.auth.GoogleMixin): | |
@tornado.web.asynchronous | |
def get(self): | |
if self.get_argument('openid.mode', None): | |
self.get_authenticated_user(self.async_callback(self._on_auth)) | |
else: | |
self.authenticate_redirect() | |
def _on_auth(self, user): | |
if not user: | |
raise tornado.web.HTTPError(500, "Google auth failed") | |
expires = datetime.datetime.utcnow() + datetime.timedelta(minutes=90) | |
self.set_secure_cookie("user", user['email'], expires=expires) | |
self.redirect(self.get_argument('next', '/')) | |
class TestHandler(LoginHandler): | |
@tornado.web.authenticated | |
def get(self): | |
self.clear() | |
self.write('Test') | |
self.finish() | |
class RootHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.clear() | |
self.write('Root') | |
self.finish() | |
class Application(tornado.web.Application): | |
def __init__(self): | |
handlers = [ | |
(r'/', RootHandler), | |
(r'/test', TestHandler), | |
(r'/auth/login', AuthHandler) | |
] | |
settings = dict( | |
cookie_secret='blablabla', | |
xsrf_cookies=True, | |
login_url='/auth/login', | |
debug=True | |
) | |
tornado.web.Application.__init__(self, handlers, **settings) | |
def main(): | |
http_server = tornado.httpserver.HTTPServer(Application()) | |
http_server.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment