<s> from django . conf import settings <EOL> from django . contrib . auth import REDIRECT_FIELD_NAME , authenticate as django_authenticate , login as django_login <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect <EOL> import helper <EOL> import utils <EOL> REQUEST_TOKEN_FORMAT = '<STR_LIT>' <EOL> ACTION_FORMAT = '<STR_LIT>' <EOL> class OAuthMediator ( object ) : <EOL> def __init__ ( self , client , redirect_field_name = REDIRECT_FIELD_NAME ) : <EOL> self . client = client <EOL> self . view_functions = { } <EOL> self . redirect_field_name = redirect_field_name <EOL> def _redirect ( self , request , action ) : <EOL> callback_url = request . build_absolute_uri ( reverse ( self . callback ) ) <EOL> request_token = self . client . request_token ( ) <EOL> request . session . update ( { <EOL> REQUEST_TOKEN_FORMAT % self . client . base_url : request_token , <EOL> ACTION_FORMAT % self . client . base_url : action , <EOL> } ) <EOL> redirect_url = self . client . get_url ( action , token = request_token , callback_url = callback_url ) <EOL> return HttpResponseRedirect ( redirect_url ) <EOL> def callback ( self , request ) : <EOL> impersonate = request . session . pop ( settings . TWITTER_IMPERSONATE_SESSION_KEY , None ) <EOL> request_token = request . session . get ( REQUEST_TOKEN_FORMAT % self . client . base_url ) <EOL> action = request . session . get ( ACTION_FORMAT % self . client . base_url ) <EOL> view_function = self . view_functions . get ( action ) <EOL> if not ( request_token and view_function ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> oauth_verifier = request . GET . get ( '<STR_LIT>' ) <EOL> access_token = self . client . access_token ( request_token , verifier = oauth_verifier ) <EOL> if not request . user . is_authenticated ( ) : <EOL> user = django_authenticate ( client = self . client , access_token = access_token , impersonate = impersonate ) <EOL> if user : <EOL> django_login ( request , user ) <EOL> redirect_to = request . session . get ( '<STR_LIT>' ) or settings . LOGIN_REDIRECT_URL <EOL> return view_function ( request , access_token , redirect_to = redirect_to , impersonate = impersonate ) <EOL> def authorize ( self , view_function ) : <EOL> self . view_functions [ helper . AUTHORIZE ] = view_function <EOL> @ login_required <EOL> def _authorize ( request ) : <EOL> return self . _redirect ( request , helper . AUTHORIZE ) <EOL> return _authorize <EOL> def authenticate ( self , view_function ) : <EOL> self . view_functions [ helper . AUTHENTICATE ] = view_function <EOL> def _authenticate ( request ) : <EOL> impersonate = request . REQUEST . get ( settings . TWITTER_IMPERSONATE_SESSION_KEY ) <EOL> if impersonate : <EOL> request . session [ settings . TWITTER_IMPERSONATE_SESSION_KEY ] = impersonate <EOL> redirect_to = request . REQUEST . get ( self . redirect_field_name , '<STR_LIT>' ) <EOL> if not redirect_to or '<STR_LIT>' in redirect_to or '<STR_LIT:U+0020>' in redirect_to : <EOL> redirect_to = settings . LOGIN_REDIRECT_URL <EOL> if request . user . is_authenticated ( ) : <EOL> return HttpResponseRedirect ( redirect_to ) <EOL> request . session [ '<STR_LIT>' ] = redirect_to <EOL> return self . _redirect ( request , helper . AUTHENTICATE ) <EOL> return _authenticate </s>
