import tornado.ioloop import tornado.web import tornado.escape import tornado.options import tornado.httputil import jinja2 import pyjade.compiler import coffeescript import markdown import github class GithubLoginHandler(tornado.web.RequestHandler, github.GithubMixin): _OAUTH_REDIRECT_URL = 'http://localhost:8888/auth/github' @tornado.web.asynchronous def get(self): # we can append next to the redirect uri, so the user gets the # correct URL on login redirect_uri = tornado.httputil.url_concat( self._OAUTH_REDIRECT_URL, {'next': self.get_argument('next', '/')}) # if we have a code, we have been authorized so we can log in if self.get_argument("code", False): self.get_authenticated_user( redirect_uri=redirect_uri, client_id=self.settings["github_client_id"], client_secret=self.settings["github_secret"], code=self.get_argument("code"), callback=self.async_callback(self._on_login) ) return # otherwise we need to request an authorization code self.authorize_redirect( redirect_uri=redirect_uri, client_id=self.settings["github_client_id"], extra_params={"scope": self.settings['github_scope'], "foo":1}) def _on_login(self, user): """ This handles the user object from the login request """ if user: logging.info('logged in user from github: ' + str(user)) self.set_secure_cookie("user", tornado.escape.json_encode(user)) else: self.clear_cookie("user") self.redirect(self.get_argument("next","/")) class GistLister(BaseHandler, github.GithubMixin): @tornado.web.authenticated @tornado.web.asynchronous def get(self): self.github_request( '/gists', self._on_get_gists, access_token=self.current_user['access_token']) def _on_get_gists(self, gists): self.render('gists.jade', gists=gists)