Created
November 18, 2013 17:49
-
-
Save andreadipersio/7532143 to your computer and use it in GitHub Desktop.
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
import tornado.ioloop | |
import tornado.web | |
import tornado.auth | |
import tornado.gen | |
from tornado.options import parse_command_line, define, options | |
define("port", default=8888) | |
define("debug", default=False) | |
class AuthHandler(tornado.web.RequestHandler, tornado.auth.GoogleMixin): | |
@tornado.web.asynchronous | |
@tornado.gen.coroutine | |
def get(self): | |
if self.get_argument("openid.mode", None): | |
user = yield self.get_authenticated_user() | |
self.set_secure_cookie("oauth_user", user.get("email", "")) | |
else: | |
yield self.authenticate_redirect() | |
class MainHandler(tornado.web.RequestHandler): | |
def get_current_user(self): | |
return self.get_secure_cookie("oauth_user") | |
def get(self): | |
user = self.get_current_user() | |
if not user: | |
raise tornado.web.HTTPError(401, "Access denied") | |
self.write("Welcome back, {}".format(user)) | |
handlers = [ | |
(r"/auth", AuthHandler), | |
(r"/", MainHandler), | |
] | |
if __name__ == "__main__": | |
parse_command_line() | |
opts = { | |
"debug": options.debug, | |
"cookie_secret": "II*^WvQiOkv)QihQwQZ<JH!YY/q)v%TY" | |
} | |
application = tornado.web.Application(handlers, **opts) | |
application.listen(options.port) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment