Last active
December 17, 2015 23:29
-
-
Save wsantos/5689075 to your computer and use it in GitHub Desktop.
Revisions
-
wsantos revised this gist
Jun 1, 2013 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -18,8 +18,6 @@ class GoogleOAuth2Mixin(tornado.auth.OAuth2Mixin): _OAUTH_USERINFO_URL = "https://www.googleapis.com/oauth2/v1/userinfo" _USER_INFO_URL = "https://www.googleapis.com/oauth2/v1/userinfo" @property def httpclient_instance(self): return httpclient.AsyncHTTPClient() -
wsantos revised this gist
Jun 1, 2013 . 1 changed file with 37 additions and 32 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,9 @@ from tornado.options import options from tornado import escape from tornado.concurrent import Future from tornado import gen import logging import urllib class GoogleOAuth2Mixin(tornado.auth.OAuth2Mixin): @@ -22,6 +24,12 @@ class GoogleOAuth2Mixin(tornado.auth.OAuth2Mixin): def httpclient_instance(self): return httpclient.AsyncHTTPClient() @property def authorization_header(self): return { "Authorization": "Bearer "+GoogleOAuth2Mixin.access_token } def authorize_redirect(self, scope, **kwargs): args = { @@ -33,7 +41,8 @@ def authorize_redirect(self, scope, **kwargs): if kwargs: args.update(kwargs) self.redirect(url_concat(self._OAUTH_AUTHENTICATE_URL, args)) @gen.coroutine def get_session(self, authorization_code): args = { "redirect_uri": options.google_redirect_uri, "client_id": options.google_consumer_key, @@ -48,47 +57,43 @@ def get_authenticated_user(self, authorization_code, callback=None): body=urllib.urlencode(args) ) response = yield self.httpclient_instance.fetch(request) if response.error: #Todo: Parse json body logging.warning('Google auth error: %s' % str(response)) raise response.error try: session = escape.json_decode(response.body) GoogleOAuth2Mixin.access_token = session['access_token'] except: pass #Todo: Work with error raise gen.Return(session) @gen.coroutine def validate_token(self, session): response = yield self.httpclient_instance.fetch( self._OAUTH_TOKEN_VALIDATION_URL+"?access_token="+session['access_token'], ) raise gen.Return(False if response.error else True) @gen.coroutine def get_authenticated_user(self, authorization_code, callback=None): session = yield self.get_session(authorization_code) valid_token = yield self.validate_token(session) if valid_token: response = yield self.httpclient_instance.fetch( self._OAUTH_USERINFO_URL+"?access_token="+session['access_token'], headers=self.authorization_header ) if response.error: raise response.error else: raise gen.Return(escape.json_decode(response.body)) else: print "False" -
wsantos created this gist
Jun 1, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,94 @@ import tornado.auth from tornado import httpclient from tornado import httputil from tornado.httputil import url_concat from tornado.options import options from tornado import escape from tornado.concurrent import Future import logging class GoogleOAuth2Mixin(tornado.auth.OAuth2Mixin): access_token = "" _OAUTH_AUTHENTICATE_URL = "https://accounts.google.com/o/oauth2/auth" _OAUTH_ACCESS_TOKEN_URL = "https://accounts.google.com/o/oauth2/token" _OAUTH_TOKEN_VALIDATION_URL = "https://www.googleapis.com/oauth2/v1/tokeninfo" _OAUTH_USERINFO_URL = "https://www.googleapis.com/oauth2/v1/userinfo" _USER_INFO_URL = "https://www.googleapis.com/oauth2/v1/userinfo" future = Future() @property def httpclient_instance(self): return httpclient.AsyncHTTPClient() def authorize_redirect(self, scope, **kwargs): args = { "redirect_uri": options.google_redirect_uri, "client_id": options.google_consumer_key, "response_type": "code", "scope": scope } if kwargs: args.update(kwargs) self.redirect(url_concat(self._OAUTH_AUTHENTICATE_URL, args)) def get_authenticated_user(self, authorization_code, callback=None): args = { "redirect_uri": options.google_redirect_uri, "client_id": options.google_consumer_key, "code": authorization_code, "client_secret": options.google_consumer_secret, "grant_type": "authorization_code" } request = httpclient.HTTPRequest( self._OAUTH_ACCESS_TOKEN_URL, method="POST", body=urllib.urlencode(args) ) http_future = self.httpclient_instance.fetch(request) http_future.add_done_callback(self._on_access_token) return self.future def _on_access_token(self, future): response = future.result() if response.error: #Todo: Parse json body logging.warning('Google auth error: %s' % str(response)) callback(None) return session = escape.json_decode(response.body) GoogleOAuth2Mixin.access_token = session['access_token'] self.validate_token(session) def validate_token(self, session): future = self.httpclient_instance.fetch( self._OAUTH_TOKEN_VALIDATION_URL+"?access_token="+session['access_token'], ) future.add_done_callback(self.get_user_info) def _on_validate_token(self, future): additional_headers = { "Authorization": "Bearer "+GoogleOAuth2Mixin.access_token } g_future = self.httpclient_instance.fetch( self._OAUTH_USERINFO_URL+"?access_token="+session['access_token'], ) g_future.add_done_callback(get_user_info) def get_user_info(self, future): response = future.result() if response.error: self.future.set_exception(response.error) else: try: self.future.set_result(escape.json_decode(response.body)) except Exception as ex: self.future.set_exception(ex)