Exemplo n.º 1
0
def facebook_callback():
    # user clicks on cancel
    if "error" in request.args:
        flash("Please use another sign in method then", "warning")
        return redirect("/")

    facebook = OAuth2Session(
        FACEBOOK_CLIENT_ID,
        state=session["oauth_state"],
        scope=_scope,
        redirect_uri=_redirect_uri,
    )
    facebook = facebook_compliance_fix(facebook)
    facebook.fetch_token(
        _token_url,
        client_secret=FACEBOOK_CLIENT_SECRET,
        authorization_response=request.url,
    )

    # Fetch a protected resource, i.e. user profile
    # {
    #     "email": "*****@*****.**",
    #     "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()

    email = facebook_user_data.get("email")

    # 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"))

    email = email.strip().lower()
    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)
            file = create_file_from_url(user, picture_url)
            user.profile_picture_id = file.id
            db.session.commit()

    else:
        flash(
            "Sorry you cannot sign up via Facebook, please use email/password sign-up instead",
            "error",
        )
        return redirect(url_for("auth.register"))

    next_url = None
    # 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)

    if not SocialAuth.get_by(user_id=user.id, social="facebook"):
        SocialAuth.create(user_id=user.id, social="facebook")
        db.session.commit()

    return after_login(user, next_url)
Exemplo n.º 2
0
def facebook_callback():
    # user clicks on cancel
    if "error" in request.args:
        flash("Please use another sign in method then", "warning")
        return redirect("/")

    facebook = OAuth2Session(
        FACEBOOK_CLIENT_ID,
        state=session["oauth_state"],
        scope=_scope,
        redirect_uri=_redirect_uri,
    )
    facebook = facebook_compliance_fix(facebook)
    token = facebook.fetch_token(
        _token_url,
        client_secret=FACEBOOK_CLIENT_SECRET,
        authorization_response=request.url,
    )

    # Fetch a protected resource, i.e. user profile
    # {
    #     "email": "*****@*****.**",
    #     "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()

    email = facebook_user_data.get("email")

    # 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"))

    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)
            file = create_file_from_url(picture_url)
            user.profile_picture_id = file.id
            db.session.commit()

    # create user
    else:
        if DISABLE_REGISTRATION:
            flash("Registration is closed", "error")
            return redirect(url_for("auth.login"))

        if not can_be_used_as_personal_email(email):
            flash(
                f"You cannot use {email} as your personal inbox.",
                "error",
            )
            return redirect(url_for("auth.login"))

        LOG.d("create facebook user with %s", facebook_user_data)
        user = User.create(email=email.lower(),
                           name=facebook_user_data["name"],
                           activated=True)

        if picture_url:
            LOG.d("set user profile picture to %s", picture_url)
            file = create_file_from_url(picture_url)
            user.profile_picture_id = file.id

        db.session.commit()
        login_user(user)
        email_utils.send_welcome_email(user)

        flash(f"Welcome to SimpleLogin {user.name}!", "success")

    next_url = None
    # 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)

    return after_login(user, next_url)