Convert auth module code samples to use gen.coroutine.
This commit is contained in:
parent
950d7d3837
commit
d577bc518d
114
tornado/auth.py
114
tornado/auth.py
|
@ -34,18 +34,16 @@ See the individual service classes below for complete documentation.
|
||||||
|
|
||||||
Example usage for Google OpenID::
|
Example usage for Google OpenID::
|
||||||
|
|
||||||
class GoogleHandler(tornado.web.RequestHandler, tornado.auth.GoogleMixin):
|
class GoogleLoginHandler(tornado.web.RequestHandler,
|
||||||
|
tornado.auth.GoogleMixin):
|
||||||
@tornado.web.asynchronous
|
@tornado.web.asynchronous
|
||||||
|
@tornado.gen.coroutine
|
||||||
def get(self):
|
def get(self):
|
||||||
if self.get_argument("openid.mode", None):
|
if self.get_argument("openid.mode", None):
|
||||||
self.get_authenticated_user(self.async_callback(self._on_auth))
|
user = yield self.get_authenticated_user()
|
||||||
return
|
# Save the user with e.g. set_secure_cookie()
|
||||||
self.authenticate_redirect()
|
else:
|
||||||
|
self.authenticate_redirect()
|
||||||
def _on_auth(self, user):
|
|
||||||
if not user:
|
|
||||||
raise tornado.web.HTTPError(500, "Google auth failed")
|
|
||||||
# Save the user with, e.g., set_secure_cookie()
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import absolute_import, division, print_function, with_statement
|
from __future__ import absolute_import, division, print_function, with_statement
|
||||||
|
@ -573,16 +571,13 @@ class TwitterMixin(OAuthMixin):
|
||||||
class TwitterLoginHandler(tornado.web.RequestHandler,
|
class TwitterLoginHandler(tornado.web.RequestHandler,
|
||||||
tornado.auth.TwitterMixin):
|
tornado.auth.TwitterMixin):
|
||||||
@tornado.web.asynchronous
|
@tornado.web.asynchronous
|
||||||
|
@tornado.gen.coroutine
|
||||||
def get(self):
|
def get(self):
|
||||||
if self.get_argument("oauth_token", None):
|
if self.get_argument("oauth_token", None):
|
||||||
self.get_authenticated_user(self.async_callback(self._on_auth))
|
user = yield self.get_authenticated_user()
|
||||||
return
|
# Save the user using e.g. set_secure_cookie()
|
||||||
self.authorize_redirect()
|
else:
|
||||||
|
self.authorize_redirect()
|
||||||
def _on_auth(self, user):
|
|
||||||
if not user:
|
|
||||||
raise tornado.web.HTTPError(500, "Twitter auth failed")
|
|
||||||
# Save the user using, e.g., set_secure_cookie()
|
|
||||||
|
|
||||||
The user object returned by `get_authenticated_user()` includes the
|
The user object returned by `get_authenticated_user()` includes the
|
||||||
attributes ``username``, ``name``, ``access_token``, and all of the
|
attributes ``username``, ``name``, ``access_token``, and all of the
|
||||||
|
@ -631,14 +626,12 @@ class TwitterMixin(OAuthMixin):
|
||||||
tornado.auth.TwitterMixin):
|
tornado.auth.TwitterMixin):
|
||||||
@tornado.web.authenticated
|
@tornado.web.authenticated
|
||||||
@tornado.web.asynchronous
|
@tornado.web.asynchronous
|
||||||
|
@tornado.gen.coroutine
|
||||||
def get(self):
|
def get(self):
|
||||||
self.twitter_request(
|
new_entry = yield self.twitter_request(
|
||||||
"/statuses/update",
|
"/statuses/update",
|
||||||
post_args={"status": "Testing Tornado Web Server"},
|
post_args={"status": "Testing Tornado Web Server"},
|
||||||
access_token=user["access_token"],
|
access_token=self.current_user["access_token"])
|
||||||
callback=self.async_callback(self._on_post))
|
|
||||||
|
|
||||||
def _on_post(self, new_entry):
|
|
||||||
if not new_entry:
|
if not new_entry:
|
||||||
# Call failed; perhaps missing permission?
|
# Call failed; perhaps missing permission?
|
||||||
self.authorize_redirect()
|
self.authorize_redirect()
|
||||||
|
@ -709,19 +702,16 @@ class FriendFeedMixin(OAuthMixin):
|
||||||
When your application is set up, you can use this mixin like this
|
When your application is set up, you can use this mixin like this
|
||||||
to authenticate the user with FriendFeed and get access to their feed::
|
to authenticate the user with FriendFeed and get access to their feed::
|
||||||
|
|
||||||
class FriendFeedHandler(tornado.web.RequestHandler,
|
class FriendFeedLoginHandler(tornado.web.RequestHandler,
|
||||||
tornado.auth.FriendFeedMixin):
|
tornado.auth.FriendFeedMixin):
|
||||||
@tornado.web.asynchronous
|
@tornado.web.asynchronous
|
||||||
|
@tornado.gen.coroutine
|
||||||
def get(self):
|
def get(self):
|
||||||
if self.get_argument("oauth_token", None):
|
if self.get_argument("oauth_token", None):
|
||||||
self.get_authenticated_user(self.async_callback(self._on_auth))
|
user = yield self.get_authenticated_user()
|
||||||
return
|
# Save the user using e.g. set_secure_cookie()
|
||||||
self.authorize_redirect()
|
else:
|
||||||
|
self.authorize_redirect()
|
||||||
def _on_auth(self, user):
|
|
||||||
if not user:
|
|
||||||
raise tornado.web.HTTPError(500, "FriendFeed auth failed")
|
|
||||||
# Save the user using, e.g., set_secure_cookie()
|
|
||||||
|
|
||||||
The user object returned by `~OAuthMixin.get_authenticated_user()` includes the
|
The user object returned by `~OAuthMixin.get_authenticated_user()` includes the
|
||||||
attributes ``username``, ``name``, and ``description`` in addition to
|
attributes ``username``, ``name``, and ``description`` in addition to
|
||||||
|
@ -760,14 +750,13 @@ class FriendFeedMixin(OAuthMixin):
|
||||||
tornado.auth.FriendFeedMixin):
|
tornado.auth.FriendFeedMixin):
|
||||||
@tornado.web.authenticated
|
@tornado.web.authenticated
|
||||||
@tornado.web.asynchronous
|
@tornado.web.asynchronous
|
||||||
|
@tornado.gen.coroutine
|
||||||
def get(self):
|
def get(self):
|
||||||
self.friendfeed_request(
|
new_entry = yield self.friendfeed_request(
|
||||||
"/entry",
|
"/entry",
|
||||||
post_args={"body": "Testing Tornado Web Server"},
|
post_args={"body": "Testing Tornado Web Server"},
|
||||||
access_token=self.current_user["access_token"],
|
access_token=self.current_user["access_token"])
|
||||||
callback=self.async_callback(self._on_post))
|
|
||||||
|
|
||||||
def _on_post(self, new_entry):
|
|
||||||
if not new_entry:
|
if not new_entry:
|
||||||
# Call failed; perhaps missing permission?
|
# Call failed; perhaps missing permission?
|
||||||
self.authorize_redirect()
|
self.authorize_redirect()
|
||||||
|
@ -842,19 +831,16 @@ class GoogleMixin(OpenIdMixin, OAuthMixin):
|
||||||
|
|
||||||
Example usage::
|
Example usage::
|
||||||
|
|
||||||
class GoogleHandler(tornado.web.RequestHandler, tornado.auth.GoogleMixin):
|
class GoogleLoginHandler(tornado.web.RequestHandler,
|
||||||
|
tornado.auth.GoogleMixin):
|
||||||
@tornado.web.asynchronous
|
@tornado.web.asynchronous
|
||||||
|
@tornado.gen.coroutine
|
||||||
def get(self):
|
def get(self):
|
||||||
if self.get_argument("openid.mode", None):
|
if self.get_argument("openid.mode", None):
|
||||||
self.get_authenticated_user(self.async_callback(self._on_auth))
|
user = yield self.get_authenticated_user()
|
||||||
return
|
# Save the user with e.g. set_secure_cookie()
|
||||||
self.authenticate_redirect()
|
else:
|
||||||
|
self.authenticate_redirect()
|
||||||
def _on_auth(self, user):
|
|
||||||
if not user:
|
|
||||||
raise tornado.web.HTTPError(500, "Google auth failed")
|
|
||||||
# Save the user with, e.g., set_secure_cookie()
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
_OPENID_ENDPOINT = "https://www.google.com/accounts/o8/ud"
|
_OPENID_ENDPOINT = "https://www.google.com/accounts/o8/ud"
|
||||||
_OAUTH_ACCESS_TOKEN_URL = "https://www.google.com/accounts/OAuthGetAccessToken"
|
_OAUTH_ACCESS_TOKEN_URL = "https://www.google.com/accounts/OAuthGetAccessToken"
|
||||||
|
@ -1117,24 +1103,20 @@ class FacebookGraphMixin(OAuth2Mixin):
|
||||||
|
|
||||||
class FacebookGraphLoginHandler(LoginHandler, tornado.auth.FacebookGraphMixin):
|
class FacebookGraphLoginHandler(LoginHandler, tornado.auth.FacebookGraphMixin):
|
||||||
@tornado.web.asynchronous
|
@tornado.web.asynchronous
|
||||||
|
@tornado.gen.coroutine
|
||||||
def get(self):
|
def get(self):
|
||||||
if self.get_argument("code", False):
|
if self.get_argument("code", False):
|
||||||
self.get_authenticated_user(
|
user = yield self.get_authenticated_user(
|
||||||
redirect_uri='/auth/facebookgraph/',
|
redirect_uri='/auth/facebookgraph/',
|
||||||
client_id=self.settings["facebook_api_key"],
|
client_id=self.settings["facebook_api_key"],
|
||||||
client_secret=self.settings["facebook_secret"],
|
client_secret=self.settings["facebook_secret"],
|
||||||
code=self.get_argument("code"),
|
code=self.get_argument("code"))
|
||||||
callback=self.async_callback(
|
# Save the user with e.g. set_secure_cookie
|
||||||
self._on_login))
|
else:
|
||||||
return
|
self.authorize_redirect(
|
||||||
self.authorize_redirect(redirect_uri='/auth/facebookgraph/',
|
redirect_uri='/auth/facebookgraph/',
|
||||||
client_id=self.settings["facebook_api_key"],
|
client_id=self.settings["facebook_api_key"],
|
||||||
extra_params={"scope": "read_stream,offline_access"})
|
extra_params={"scope": "read_stream,offline_access"})
|
||||||
|
|
||||||
def _on_login(self, user):
|
|
||||||
logging.error(user)
|
|
||||||
self.finish()
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
http = self.get_auth_http_client()
|
http = self.get_auth_http_client()
|
||||||
args = {
|
args = {
|
||||||
|
@ -1208,20 +1190,18 @@ class FacebookGraphMixin(OAuth2Mixin):
|
||||||
tornado.auth.FacebookGraphMixin):
|
tornado.auth.FacebookGraphMixin):
|
||||||
@tornado.web.authenticated
|
@tornado.web.authenticated
|
||||||
@tornado.web.asynchronous
|
@tornado.web.asynchronous
|
||||||
|
@tornado.gen.coroutine
|
||||||
def get(self):
|
def get(self):
|
||||||
self.facebook_request(
|
new_entry = yield self.facebook_request(
|
||||||
"/me/feed",
|
"/me/feed",
|
||||||
post_args={"message": "I am posting from my Tornado application!"},
|
post_args={"message": "I am posting from my Tornado application!"},
|
||||||
access_token=self.current_user["access_token"],
|
access_token=self.current_user["access_token"])
|
||||||
callback=self.async_callback(self._on_post))
|
|
||||||
|
|
||||||
def _on_post(self, new_entry):
|
|
||||||
if not new_entry:
|
if not new_entry:
|
||||||
# Call failed; perhaps missing permission?
|
# Call failed; perhaps missing permission?
|
||||||
self.authorize_redirect()
|
self.authorize_redirect()
|
||||||
return
|
return
|
||||||
self.finish("Posted a message!")
|
self.finish("Posted a message!")
|
||||||
|
|
||||||
"""
|
"""
|
||||||
url = "https://graph.facebook.com" + path
|
url = "https://graph.facebook.com" + path
|
||||||
all_args = {}
|
all_args = {}
|
||||||
|
|
Loading…
Reference in New Issue