def get_token(provider): if provider == 'facebook': provider_class = FbOAuth() payload = { 'grant_type': 'client_credentials', 'client_id': provider_class.get_client_id(), 'client_secret': provider_class.get_client_secret() } elif provider == 'google': provider_class = GoogleOAuth() payload = { 'client_id': provider_class.get_client_id(), 'client_secret': provider_class.get_client_secret() } elif provider == 'twitter': provider_class = TwitterOAuth() payload = { 'client_id': provider_class.get_client_id(), 'client_secret': provider_class.get_client_secret() } elif provider == 'instagram': provider_class = InstagramOAuth() payload = { 'client_id': provider_class.get_client_id(), 'client_secret': provider_class.get_client_secret() } else: return make_response( jsonify(message="No support for {}".format(provider)), 200) response = requests.post(provider_class.get_token_uri(), params=payload) return make_response(jsonify(token=response.json()), 200)
def login_user(provider): if provider == 'facebook': provider_class = FbOAuth() payload = { 'client_id': provider_class.get_client_id(), 'redirect_uri': provider_class.get_redirect_uri(), 'client_secret': provider_class.get_client_secret(), 'code': request.args.get('code') } if not payload['client_id'] or not payload['client_secret']: raise NotImplementedError({'source': ''}, 'Facebook Login Not Configured') access_token = requests.get( 'https://graph.facebook.com/v3.0/oauth/access_token', params=payload).json() payload_details = { 'input_token': access_token['access_token'], 'access_token': provider_class.get_client_id() + '|' + provider_class.get_client_secret() } details = requests.get('https://graph.facebook.com/debug_token', params=payload_details).json() user_details = requests.get('https://graph.facebook.com/v3.0/' + details['data']['user_id'], params={ 'access_token': access_token['access_token'], 'fields': 'first_name, last_name, email' }).json() if get_count( db.session.query(User).filter_by( email=user_details['email'])) > 0: user = db.session.query(User).filter_by( email=user_details['email']).one() if not user.facebook_id: user.facebook_id = user_details['id'] user.facebook_login_hash = random.getrandbits(128) save_to_db(user) return make_response( jsonify(user_id=user.id, email=user.email, oauth_hash=user.facebook_login_hash), 200) user = User() user.first_name = user_details['first_name'] user.last_name = user_details['last_name'] user.facebook_id = user_details['id'] user.facebook_login_hash = random.getrandbits(128) user.password = ''.join( random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(8)) if user_details['email']: user.email = user_details['email'] save_to_db(user) return make_response( jsonify(user_id=user.id, email=user.email, oauth_hash=user.facebook_login_hash), 200) elif provider == 'google': provider_class = GoogleOAuth() payload = { 'client_id': provider_class.get_client_id(), 'client_secret': provider_class.get_client_secret() } elif provider == 'twitter': provider_class = TwitterOAuth() payload = { 'client_id': provider_class.get_client_id(), 'client_secret': provider_class.get_client_secret() } elif provider == 'instagram': provider_class = InstagramOAuth() payload = { 'client_id': provider_class.get_client_id(), 'client_secret': provider_class.get_client_secret() } else: return make_response( jsonify(message="No support for {}".format(provider)), 200) response = requests.post(provider_class.get_token_uri(), params=payload) return make_response(jsonify(token=response.json()), 200)