From be33ea3747e599a582832cf390328e3af2003d9d Mon Sep 17 00:00:00 2001 From: Daniel Grams Date: Sun, 20 Dec 2020 13:43:01 +0100 Subject: [PATCH 1/2] #44 Remove Login via Google --- README.md | 7 -- project/__init__.py | 9 --- project/oauth.py | 67 ------------------ project/templates/_macros.html | 4 -- project/templates/security/login_user.html | 5 +- project/templates/security/register_user.html | 6 +- .../translations/de/LC_MESSAGES/messages.mo | Bin 18886 -> 18822 bytes .../translations/de/LC_MESSAGES/messages.po | 17 +++-- requirements.txt | 2 +- 9 files changed, 11 insertions(+), 106 deletions(-) delete mode 100644 project/oauth.py diff --git a/README.md b/README.md index 39f9b6b..992365d 100644 --- a/README.md +++ b/README.md @@ -68,13 +68,6 @@ Create `.env` file in the root directory or pass as environment variables. | MAIL_SERVER | " | | MAIL_USERNAME | " | -### Login with Google via OAuth - -| Variable | Function | -| --- | --- | -| GOOGLE_OAUTH_CLIENT_ID | Client Id | -| GOOGLE_OAUTH_CLIENT_SECRET | Secret | - ### Resolve addresses with Google Maps | Variable | Function | diff --git a/project/__init__.py b/project/__init__.py index a84680b..53f2751 100644 --- a/project/__init__.py +++ b/project/__init__.py @@ -23,10 +23,6 @@ app.config["SECURITY_RECOVERABLE"] = True app.config["SECURITY_CHANGEABLE"] = True app.config["SECURITY_EMAIL_SENDER"] = os.getenv("MAIL_DEFAULT_SENDER") app.config["LANGUAGES"] = ["en", "de"] -app.config["GOOGLE_OAUTH_CLIENT_ID"] = os.getenv("GOOGLE_OAUTH_CLIENT_ID") -app.config["GOOGLE_OAUTH_CLIENT_SECRET"] = os.getenv("GOOGLE_OAUTH_CLIENT_SECRET") -app.config["OAUTHLIB_INSECURE_TRANSPORT"] = True -app.config["OAUTHLIB_RELAX_TOKEN_SCOPE"] = True # Generate a nice key using secrets.token_urlsafe() app.config["SECRET_KEY"] = os.environ.get( @@ -90,11 +86,6 @@ from project.models import User, Role user_datastore = SQLAlchemySessionUserDatastore(db.session, User, Role) security = Security(app, user_datastore) -# OAuth -from project.oauth import blueprint - -app.register_blueprint(blueprint, url_prefix="/login") - from project import i10n from project import jinja_filters from project import init_data diff --git a/project/oauth.py b/project/oauth.py deleted file mode 100644 index e12fc59..0000000 --- a/project/oauth.py +++ /dev/null @@ -1,67 +0,0 @@ -from flask import flash -from flask_security import current_user, login_user -from flask_dance.contrib.google import make_google_blueprint -from flask_dance.consumer import oauth_authorized, oauth_error -from flask_dance.consumer.storage.sqla import SQLAlchemyStorage -from project.models import OAuth -from project import db, user_datastore -from flask_babelex import gettext - -blueprint = make_google_blueprint( - scope=["profile", "email"], - storage=SQLAlchemyStorage(OAuth, db.session, user=current_user), -) - - -# create/login local user on successful OAuth login -@oauth_authorized.connect_via(blueprint) -def google_logged_in(blueprint, token): # pragma: no cover - if not token: - flash("Failed to log in.", category="error") - return False - - resp = blueprint.session.get("/oauth2/v1/userinfo") - if not resp.ok: - msg = "Failed to fetch user info." - flash(msg, category="error") - return False - - info = resp.json() - user_id = info["id"] - - # Find this OAuth token in the database, or create it - oauth = OAuth.query.filter_by( - provider=blueprint.name, provider_user_id=user_id - ).first() - if oauth is None: - oauth = OAuth(provider=blueprint.name, provider_user_id=user_id, token=token) - - if oauth.user: - login_user(oauth.user, authn_via=["google"]) - user_datastore.commit() - flash(gettext("Successfully signed in."), "success") - - else: - # Create a new local user account for this user - user = user_datastore.create_user(email=info["email"]) - # Associate the new local user account with the OAuth token - oauth.user = user - # Save and commit our database models - db.session.add_all([user, oauth]) - db.session.commit() - # Log in the new local user account - login_user(user, authn_via=["google"]) - user_datastore.commit() - flash(gettext("Successfully signed in."), "success") - - # Disable Flask-Dance's default behavior for saving the OAuth token - return False - - -# notify on OAuth provider error -@oauth_error.connect_via(blueprint) -def google_error(blueprint, message, response): # pragma: no cover - msg = "OAuth error from {name}! message={message} response={response}".format( - name=blueprint.name, message=message, response=response - ) - flash(msg, category="error") diff --git a/project/templates/_macros.html b/project/templates/_macros.html index 59c82be..4dfe7f7 100644 --- a/project/templates/_macros.html +++ b/project/templates/_macros.html @@ -438,10 +438,6 @@ {% endmacro %} -{% macro render_google_sign_in_button() %} - {{ _('Sign in with Google') }} -{% endmacro %} - {% macro render_google_place_autocomplete_header(location_only = False) %}