<s> """<STR_LIT>""" <EOL> import tornado . web <EOL> import tornado . httpclient <EOL> import tornado . auth <EOL> from urllib import urlencode <EOL> from urlparse import parse_qs <EOL> import config <EOL> import json <EOL> from iheartgit . handlers import BaseHandler <EOL> from iheartgit . models . user import User <EOL> import logging <EOL> class GitHubLoginHandler ( BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . current_user != None : <EOL> self . redirect ( '<STR_LIT:/>' ) <EOL> else : <EOL> self . redirect ( '<STR_LIT>' % ( config . GITHUB_ID , config . GITHUB_CALLBACK ) ) <EOL> class GitHubCallbackHandler ( tornado . web . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def on_profile_response ( self , response ) : <EOL> """<STR_LIT>""" <EOL> if response . error : <EOL> self . send_error ( ) <EOL> try : <EOL> github_user = json . loads ( response . body ) <EOL> except : <EOL> logging . exception ( '<STR_LIT>' ) <EOL> self . send_error ( ) <EOL> user_id = None <EOL> current_user = User . objects ( service = '<STR_LIT>' , nick = github_user [ '<STR_LIT>' ] ) . first ( ) <EOL> if current_user != None : <EOL> user_id = unicode ( current_user . id ) <EOL> logging . info ( '<STR_LIT>' % ( github_user [ '<STR_LIT>' ] , ) ) <EOL> else : <EOL> user = User ( service = '<STR_LIT>' , nick = github_user [ '<STR_LIT>' ] , url = github_user [ '<STR_LIT>' ] , avatar_url = github_user [ '<STR_LIT>' ] ) <EOL> logging . info ( '<STR_LIT>' % ( github_user [ '<STR_LIT>' ] , ) ) <EOL> try : <EOL> user . save ( ) <EOL> except : <EOL> logging . exception ( '<STR_LIT>' ) <EOL> self . send_error ( ) <EOL> user_id = unicode ( user . id ) <EOL> self . set_secure_cookie ( '<STR_LIT>' , user_id , expires_days = <NUM_LIT> ) <EOL> self . redirect ( '<STR_LIT>' ) <EOL> self . finish ( ) <EOL> def on_callback_response ( self , response ) : <EOL> """<STR_LIT>""" <EOL> if response . error : <EOL> self . send_error ( ) <EOL> try : <EOL> response_data = parse_qs ( response . body ) <EOL> except : <EOL> self . send_error ( ) <EOL> request = tornado . httpclient . HTTPRequest ( '<STR_LIT>' + response_data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , method = '<STR_LIT:GET>' ) <EOL> httpclient = tornado . httpclient . AsyncHTTPClient ( ) <EOL> httpclient . fetch ( request , self . on_profile_response ) <EOL> @ tornado . web . asynchronous <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> body = { <EOL> '<STR_LIT>' : config . GITHUB_ID , <EOL> '<STR_LIT>' : config . GITHUB_SECRET , <EOL> '<STR_LIT:code>' : self . get_argument ( '<STR_LIT:code>' ) , <EOL> '<STR_LIT>' : config . GITHUB_CALLBACK <EOL> } <EOL> request = tornado . httpclient . HTTPRequest ( '<STR_LIT>' , method = '<STR_LIT:POST>' ) <EOL> request . body = urlencode ( body ) <EOL> request . headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> httpclient = tornado . httpclient . AsyncHTTPClient ( ) <EOL> httpclient . fetch ( request , self . on_callback_response ) <EOL> class TwitterHandler ( BaseHandler , tornado . auth . TwitterMixin ) : <EOL> """<STR_LIT>""" <EOL> def on_profile_response ( self , twitter_user ) : <EOL> """<STR_LIT>""" <EOL> user_id = None <EOL> current_user = User . objects ( service = '<STR_LIT>' , nick = twitter_user [ '<STR_LIT:username>' ] ) . first ( ) <EOL> if current_user != None : <EOL> user_id = unicode ( current_user . id ) <EOL> logging . info ( '<STR_LIT>' % ( twitter_user [ '<STR_LIT:username>' ] , ) ) <EOL> if len ( current_user . auth ) == <NUM_LIT:0> or current_user . auth [ '<STR_LIT:key>' ] != twitter_user [ '<STR_LIT>' ] [ '<STR_LIT:key>' ] : <EOL> current_user . auth = twitter_user [ '<STR_LIT>' ] <EOL> try : <EOL> current_user . save ( ) <EOL> except : <EOL> logging . exception ( '<STR_LIT>' ) <EOL> self . send_error <EOL> else : <EOL> user = User ( service = '<STR_LIT>' , nick = twitter_user [ '<STR_LIT:username>' ] , url = '<STR_LIT>' + twitter_user [ '<STR_LIT:username>' ] , avatar_url = twitter_user [ '<STR_LIT>' ] ) <EOL> user . auth = twitter_user [ '<STR_LIT>' ] <EOL> logging . info ( '<STR_LIT>' % ( twitter_user [ '<STR_LIT:username>' ] , ) ) <EOL> try : <EOL> user . save ( ) <EOL> except : <EOL> logging . exception ( '<STR_LIT>' ) <EOL> self . send_error ( ) <EOL> user_id = unicode ( user . id ) <EOL> self . set_secure_cookie ( '<STR_LIT>' , user_id , expires_days = <NUM_LIT> ) <EOL> self . redirect ( '<STR_LIT>' ) <EOL> @ tornado . web . asynchronous <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . current_user != None : <EOL> self . redirect ( '<STR_LIT:/>' ) <EOL> self . finish ( ) <EOL> else : <EOL> if self . get_argument ( '<STR_LIT>' , None ) : <EOL> self . get_authenticated_user ( self . async_callback ( self . on_profile_response ) ) <EOL> return <EOL> self . authenticate_redirect ( ) </s>
