Skip to content

Commit 813f89c

Browse files
committed
Convert auth demo to coroutines.
1 parent 088489a commit 813f89c

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

demos/auth/authdemo.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
import tornado.escape
1919
import tornado.httpserver
2020
import tornado.ioloop
21-
import tornado.options
2221
import tornado.web
2322

24-
from tornado.options import define, options
23+
from tornado import gen
24+
from tornado.options import define, options, parse_command_line
2525

2626
define("port", default=8888, help="run on the given port", type=int)
2727

@@ -57,18 +57,16 @@ def get(self):
5757

5858
class AuthHandler(BaseHandler, tornado.auth.GoogleMixin):
5959
@tornado.web.asynchronous
60+
@gen.coroutine
6061
def get(self):
6162
if self.get_argument("openid.mode", None):
62-
self.get_authenticated_user(self.async_callback(self._on_auth))
63+
user = yield self.get_authenticated_user()
64+
self.set_secure_cookie("authdemo_user",
65+
tornado.escape.json_encode(user))
66+
self.redirect("/")
6367
return
6468
self.authenticate_redirect()
6569

66-
def _on_auth(self, user):
67-
if not user:
68-
raise tornado.web.HTTPError(500, "Google auth failed")
69-
self.set_secure_cookie("authdemo_user", tornado.escape.json_encode(user))
70-
self.redirect("/")
71-
7270

7371
class LogoutHandler(BaseHandler):
7472
def get(self):
@@ -82,7 +80,7 @@ def get(self):
8280
'Click <a href="/">here</a> to log back in.')
8381

8482
def main():
85-
tornado.options.parse_command_line()
83+
parse_command_line()
8684
http_server = tornado.httpserver.HTTPServer(Application())
8785
http_server.listen(options.port)
8886
tornado.ioloop.IOLoop.instance().start()

0 commit comments

Comments
 (0)