2019-07-09 16:13:34 +00:00
|
|
|
from flask import request, session, redirect, url_for, flash
|
|
|
|
from flask_login import login_user
|
|
|
|
from requests_oauthlib import OAuth2Session
|
|
|
|
from requests_oauthlib.compliance_fixes import facebook_compliance_fix
|
|
|
|
|
2019-11-16 22:03:37 +00:00
|
|
|
from app import email_utils
|
2019-07-09 16:13:34 +00:00
|
|
|
from app.auth.base import auth_bp
|
|
|
|
from app.auth.views.google import create_file_from_url
|
2020-01-28 04:50:25 +00:00
|
|
|
from app.config import (
|
|
|
|
URL,
|
|
|
|
FACEBOOK_CLIENT_ID,
|
|
|
|
FACEBOOK_CLIENT_SECRET,
|
|
|
|
DISABLE_REGISTRATION,
|
|
|
|
)
|
2019-07-09 16:13:34 +00:00
|
|
|
from app.extensions import db
|
|
|
|
from app.log import LOG
|
2020-02-27 15:16:12 +00:00
|
|
|
from app.models import User, SocialAuth
|
2020-04-09 20:22:26 +00:00
|
|
|
from .login_utils import after_login, get_referral
|
2020-02-10 16:14:36 +00:00
|
|
|
from ...email_utils import can_be_used_as_personal_email, email_already_used
|
2019-07-09 16:13:34 +00:00
|
|
|
|
|
|
|
_authorization_base_url = "https://www.facebook.com/dialog/oauth"
|
|
|
|
_token_url = "https://graph.facebook.com/oauth/access_token"
|
|
|
|
|
|
|
|
_scope = ["email"]
|
|
|
|
|
|
|
|
# need to set explicitly redirect_uri instead of leaving the lib to pre-fill redirect_uri
|
|
|
|
# when served behind nginx, the redirect_uri is localhost... and not the real url
|
|
|
|
_redirect_uri = URL + "/auth/facebook/callback"
|
|
|
|
|
|
|
|
|
|
|
|
@auth_bp.route("/facebook/login")
|
|
|
|
def facebook_login():
|
|
|
|
# to avoid flask-login displaying the login error message
|
|
|
|
session.pop("_flashes", None)
|
|
|
|
|
|
|
|
next_url = request.args.get("next")
|
|
|
|
|
|
|
|
# Facebook does not allow to append param to redirect_uri
|
|
|
|
# we need to pass the next url by session
|
|
|
|
if next_url:
|
|
|
|
session["facebook_next_url"] = next_url
|
|
|
|
|
|
|
|
facebook = OAuth2Session(
|
|
|
|
FACEBOOK_CLIENT_ID, scope=_scope, redirect_uri=_redirect_uri
|
|
|
|
)
|
|
|
|
facebook = facebook_compliance_fix(facebook)
|
|
|
|
authorization_url, state = facebook.authorization_url(_authorization_base_url)
|
|
|
|
|
|
|
|
# State is used to prevent CSRF, keep this for later.
|
|
|
|
session["oauth_state"] = state
|
|
|
|
return redirect(authorization_url)
|
|
|
|
|
|
|
|
|
|
|
|
@auth_bp.route("/facebook/callback")
|
|
|
|
def facebook_callback():
|
2019-07-11 18:14:18 +00:00
|
|
|
# user clicks on cancel
|
|
|
|
if "error" in request.args:
|
2019-12-26 13:00:17 +00:00
|
|
|
flash("Please use another sign in method then", "warning")
|
2019-07-11 18:14:18 +00:00
|
|
|
return redirect("/")
|
|
|
|
|
2019-07-09 16:13:34 +00:00
|
|
|
facebook = OAuth2Session(
|
|
|
|
FACEBOOK_CLIENT_ID,
|
|
|
|
state=session["oauth_state"],
|
|
|
|
scope=_scope,
|
|
|
|
redirect_uri=_redirect_uri,
|
|
|
|
)
|
|
|
|
facebook = facebook_compliance_fix(facebook)
|
2020-05-07 20:01:14 +00:00
|
|
|
facebook.fetch_token(
|
2019-07-09 16:13:34 +00:00
|
|
|
_token_url,
|
|
|
|
client_secret=FACEBOOK_CLIENT_SECRET,
|
|
|
|
authorization_response=request.url,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Fetch a protected resource, i.e. user profile
|
|
|
|
# {
|
|
|
|
# "email": "abcd@gmail.com",
|
|
|
|
# "id": "1234",
|
|
|
|
# "name": "First Last",
|
|
|
|
# "picture": {
|
|
|
|
# "data": {
|
|
|
|
# "url": "long_url"
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
facebook_user_data = facebook.get(
|
|
|
|
"https://graph.facebook.com/me?fields=id,name,email,picture{url}"
|
|
|
|
).json()
|
|
|
|
|
2020-04-24 07:17:21 +00:00
|
|
|
email = facebook_user_data.get("email")
|
2019-12-19 20:19:39 +00:00
|
|
|
|
|
|
|
# user choose to not share email, cannot continue
|
|
|
|
if not email:
|
|
|
|
flash(
|
|
|
|
"In order to use SimpleLogin, you need to give us a valid email", "warning"
|
|
|
|
)
|
|
|
|
return redirect(url_for("auth.register"))
|
|
|
|
|
2020-04-24 07:17:21 +00:00
|
|
|
email = email.strip().lower()
|
2019-07-09 16:13:34 +00:00
|
|
|
user = User.get_by(email=email)
|
|
|
|
|
|
|
|
picture_url = facebook_user_data.get("picture", {}).get("data", {}).get("url")
|
|
|
|
|
|
|
|
if user:
|
|
|
|
if picture_url and not user.profile_picture_id:
|
|
|
|
LOG.d("set user profile picture to %s", picture_url)
|
2020-03-20 08:52:00 +00:00
|
|
|
file = create_file_from_url(user, picture_url)
|
2019-07-09 16:13:34 +00:00
|
|
|
user.profile_picture_id = file.id
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
else:
|
2020-05-07 20:01:14 +00:00
|
|
|
flash(
|
|
|
|
"Sorry you cannot sign up via Facebook, please use email/password sign-up instead",
|
|
|
|
"error",
|
2020-01-04 09:24:01 +00:00
|
|
|
)
|
2020-05-07 20:01:14 +00:00
|
|
|
return redirect(url_for("auth.register"))
|
2019-07-09 16:13:34 +00:00
|
|
|
|
2019-12-27 14:35:22 +00:00
|
|
|
next_url = None
|
2019-07-09 16:13:34 +00:00
|
|
|
# The activation link contains the original page, for ex authorize page
|
|
|
|
if "facebook_next_url" in session:
|
|
|
|
next_url = session["facebook_next_url"]
|
|
|
|
LOG.debug("redirect user to %s", next_url)
|
|
|
|
|
|
|
|
# reset the next_url to avoid user getting redirected at each login :)
|
|
|
|
session.pop("facebook_next_url", None)
|
|
|
|
|
2020-02-27 15:16:12 +00:00
|
|
|
if not SocialAuth.get_by(user_id=user.id, social="facebook"):
|
|
|
|
SocialAuth.create(user_id=user.id, social="facebook")
|
|
|
|
db.session.commit()
|
|
|
|
|
2019-12-27 14:35:22 +00:00
|
|
|
return after_login(user, next_url)
|