3
# Copyright 2009 Facebook
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
19
import tornado.httpserver
21
import tornado.options
24
from tornado.options import define, options
26
define("port", default=8888, help="run on the given port", type=int)
29
class Application(tornado.web.Application):
33
(r"/auth/login", AuthHandler),
36
cookie_secret="32oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
37
login_url="/auth/login",
39
tornado.web.Application.__init__(self, handlers, **settings)
42
class BaseHandler(tornado.web.RequestHandler):
43
def get_current_user(self):
44
user_json = self.get_secure_cookie("user")
45
if not user_json: return None
46
return tornado.escape.json_decode(user_json)
49
class MainHandler(BaseHandler):
50
@tornado.web.authenticated
52
name = tornado.escape.xhtml_escape(self.current_user["name"])
53
self.write("Hello, " + name)
56
class AuthHandler(BaseHandler, tornado.auth.GoogleMixin):
57
@tornado.web.asynchronous
59
if self.get_argument("openid.mode", None):
60
self.get_authenticated_user(self.async_callback(self._on_auth))
62
self.authenticate_redirect()
64
def _on_auth(self, user):
66
raise tornado.web.HTTPError(500, "Google auth failed")
67
self.set_secure_cookie("user", tornado.escape.json_encode(user))
72
tornado.options.parse_command_line()
73
http_server = tornado.httpserver.HTTPServer(Application())
74
http_server.listen(options.port)
75
tornado.ioloop.IOLoop.instance().start()
78
if __name__ == "__main__":