<s> import logging <EOL> import requests <EOL> import hashlib <EOL> import hmac <EOL> from allauth . socialaccount . models import ( SocialLogin , <EOL> SocialToken ) <EOL> from allauth . socialaccount . helpers import complete_social_login <EOL> from allauth . socialaccount . helpers import render_authentication_error <EOL> from allauth . socialaccount import providers <EOL> from allauth . socialaccount . providers . oauth2 . views import ( OAuth2Adapter , <EOL> OAuth2LoginView , <EOL> OAuth2CallbackView ) <EOL> from . forms import FacebookConnectForm <EOL> from . provider import FacebookProvider , GRAPH_API_URL <EOL> logger = logging . getLogger ( __name__ ) <EOL> def compute_appsecret_proof ( app , token ) : <EOL> msg = token . token . encode ( '<STR_LIT:utf-8>' ) <EOL> key = app . secret . encode ( '<STR_LIT:utf-8>' ) <EOL> appsecret_proof = hmac . new ( <EOL> key , <EOL> msg , <EOL> digestmod = hashlib . sha256 ) . hexdigest ( ) <EOL> return appsecret_proof <EOL> def fb_complete_login ( request , app , token ) : <EOL> provider = providers . registry . by_id ( FacebookProvider . id ) <EOL> resp = requests . get ( <EOL> GRAPH_API_URL + '<STR_LIT>' , <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT:U+002C>' . join ( provider . get_fields ( ) ) , <EOL> '<STR_LIT>' : token . token , <EOL> '<STR_LIT>' : compute_appsecret_proof ( app , token ) <EOL> } ) <EOL> resp . raise_for_status ( ) <EOL> extra_data = resp . json ( ) <EOL> login = provider . sociallogin_from_response ( request , extra_data ) <EOL> return login <EOL> class FacebookOAuth2Adapter ( OAuth2Adapter ) : <EOL> provider_id = FacebookProvider . id <EOL> authorize_url = '<STR_LIT>' <EOL> access_token_url = GRAPH_API_URL + '<STR_LIT>' <EOL> expires_in_key = '<STR_LIT>' <EOL> def complete_login ( self , request , app , access_token , ** kwargs ) : <EOL> return fb_complete_login ( request , app , access_token ) <EOL> oauth2_login = OAuth2LoginView . adapter_view ( FacebookOAuth2Adapter ) <EOL> oauth2_callback = OAuth2CallbackView . adapter_view ( FacebookOAuth2Adapter ) <EOL> def login_by_token ( request ) : <EOL> ret = None <EOL> auth_exception = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = FacebookConnectForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> try : <EOL> provider = providers . registry . by_id ( FacebookProvider . id ) <EOL> login_options = provider . get_fb_login_options ( request ) <EOL> app = providers . registry . by_id ( FacebookProvider . id ) . get_app ( request ) <EOL> access_token = form . cleaned_data [ '<STR_LIT>' ] <EOL> if login_options . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> info = requests . get ( <EOL> GRAPH_API_URL + '<STR_LIT>' , <EOL> params = { '<STR_LIT>' : app . client_id , <EOL> '<STR_LIT>' : access_token } ) . json ( ) <EOL> nonce = provider . get_nonce ( request , pop = True ) <EOL> ok = nonce and nonce == info . get ( '<STR_LIT>' ) <EOL> else : <EOL> ok = True <EOL> if ok and provider . get_settings ( ) . get ( '<STR_LIT>' ) : <EOL> resp = requests . get ( <EOL> GRAPH_API_URL + '<STR_LIT>' , <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : app . client_id , <EOL> '<STR_LIT>' : app . secret , <EOL> '<STR_LIT>' : access_token } ) . json ( ) <EOL> access_token = resp [ '<STR_LIT>' ] <EOL> if ok : <EOL> token = SocialToken ( app = app , <EOL> token = access_token ) <EOL> login = fb_complete_login ( request , app , token ) <EOL> login . token = token <EOL> login . state = SocialLogin . state_from_request ( request ) <EOL> ret = complete_social_login ( request , login ) <EOL> except requests . RequestException as e : <EOL> logger . exception ( '<STR_LIT>' ) <EOL> auth_exception = e <EOL> if not ret : <EOL> ret = render_authentication_error ( request , <EOL> FacebookProvider . id , <EOL> exception = auth_exception ) <EOL> return ret </s>
