<s> from flask import request , session , redirect , current_app , url_for <EOL> from flask . ext . security . utils import login_user <EOL> from . models import User , Connection <EOL> def clean_sessions ( ) : <EOL> for provider in current_app . config . get ( "<STR_LIT>" , { } ) : <EOL> session . pop ( '<STR_LIT>' % provider , None ) <EOL> session . pop ( '<STR_LIT>' % provider , None ) <EOL> def get_oauth_app ( provider ) : <EOL> provider_name = "<STR_LIT>" + provider <EOL> return getattr ( current_app , provider_name , None ) <EOL> def oauth_login ( provider ) : <EOL> oauth_app = get_oauth_app ( provider ) <EOL> clean_sessions ( ) <EOL> if provider == '<STR_LIT>' : <EOL> _next = None <EOL> else : <EOL> _next = request . args . get ( '<STR_LIT>' , request . referrer ) or None <EOL> return oauth_app . authorize ( <EOL> callback = url_for ( <EOL> '<STR_LIT>' . format ( provider ) , <EOL> _external = True , <EOL> next = _next <EOL> ) <EOL> ) <EOL> def make_oauth_handler ( provider ) : <EOL> def oauth_handler ( resp ) : <EOL> app = current_app <EOL> oauth_app = get_oauth_app ( provider ) <EOL> if not oauth_app : <EOL> return "<STR_LIT>" <EOL> oauth_app . tokengetter ( <EOL> lambda : session . get ( "<STR_LIT>" + provider + "<STR_LIT>" ) <EOL> ) <EOL> if resp is None : <EOL> return '<STR_LIT>' % ( <EOL> request . args [ '<STR_LIT>' ] , <EOL> request . args [ '<STR_LIT>' ] <EOL> ) <EOL> session [ "<STR_LIT>" + provider + "<STR_LIT>" ] = ( resp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> data = app . config . get ( "<STR_LIT>" , { } ) . get ( provider ) <EOL> me = oauth_app . get ( data . get ( '<STR_LIT>' ) ) <EOL> if not any ( [ me . data . get ( '<STR_LIT>' ) , <EOL> me . data . get ( '<STR_LIT>' ) ] ) : <EOL> return "<STR_LIT>" <EOL> email = me . data . get ( '<STR_LIT:email>' ) <EOL> name = me . data . get ( '<STR_LIT:name>' ) <EOL> provider_user_id = me . data . get ( '<STR_LIT:id>' ) <EOL> profile_url = me . data . get ( '<STR_LIT>' ) <EOL> access_token = resp [ '<STR_LIT>' ] <EOL> try : <EOL> user = User . objects . get ( email = email ) <EOL> except User . DoesNotExist : <EOL> user = User ( <EOL> name = name , <EOL> email = email , <EOL> username = User . generate_username ( email ) <EOL> ) <EOL> user . save ( ) <EOL> try : <EOL> connection = Connection . objects . get ( <EOL> user_id = str ( user . id ) , <EOL> provider_id = provider , <EOL> ) <EOL> connection . access_token = access_token <EOL> connection . save ( ) <EOL> except Connection . DoesNotExist : <EOL> connection = Connection ( <EOL> user_id = str ( user . id ) , <EOL> provider_id = provider , <EOL> provider_user_id = provider_user_id , <EOL> profile_url = profile_url , <EOL> access_token = access_token <EOL> ) <EOL> connection . save ( ) <EOL> login_user ( user ) <EOL> _next = request . args . get ( <EOL> '<STR_LIT>' , request . referrer <EOL> ) or session . get ( <EOL> '<STR_LIT>' <EOL> ) or app . config . get ( '<STR_LIT>' , "<STR_LIT:/>" ) <EOL> return redirect ( _next ) <EOL> return oauth_handler </s>
