app-MAIL-temp/app/auth/views/google.py

150 lines
4.8 KiB
Python
Raw Normal View History

from flask import request, session, redirect, flash, url_for
2019-07-08 14:13:00 +02:00
from flask_login import login_user
from requests_oauthlib import OAuth2Session
from app import s3, email_utils
2019-07-08 14:13:00 +02:00
from app.auth.base import auth_bp
from app.config import URL, GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, DISABLE_REGISTRATION
2019-07-08 14:13:00 +02:00
from app.extensions import db
from app.log import LOG
from app.models import User, File, SocialAuth
from app.utils import random_string
2020-04-09 22:22:26 +02:00
from .login_utils import after_login, get_referral
from ...email_utils import can_be_used_as_personal_email, email_already_used
2019-07-08 14:13:00 +02:00
2019-07-08 19:44:11 +02:00
_authorization_base_url = "https://accounts.google.com/o/oauth2/v2/auth"
_token_url = "https://www.googleapis.com/oauth2/v4/token"
2019-07-08 14:13:00 +02:00
2019-07-08 19:44:11 +02:00
_scope = [
2019-07-08 14:13:00 +02:00
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
"openid",
]
# 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
2019-07-08 19:44:11 +02:00
_redirect_uri = URL + "/auth/google/callback"
2019-07-08 14:13:00 +02:00
@auth_bp.route("/google/login")
def google_login():
# to avoid flask-login displaying the login error message
session.pop("_flashes", None)
2019-07-08 19:45:24 +02:00
next_url = request.args.get("next")
# Google does not allow to append param to redirect_url
# we need to pass the next url by session
2019-07-08 19:45:24 +02:00
if next_url:
session["google_next_url"] = next_url
2019-07-08 19:45:24 +02:00
google = OAuth2Session(GOOGLE_CLIENT_ID, scope=_scope, redirect_uri=_redirect_uri)
2019-07-08 19:44:11 +02:00
authorization_url, state = google.authorization_url(_authorization_base_url)
2019-07-08 14:13:00 +02:00
# State is used to prevent CSRF, keep this for later.
session["oauth_state"] = state
return redirect(authorization_url)
@auth_bp.route("/google/callback")
def google_callback():
2019-07-11 20:14:18 +02:00
# user clicks on cancel
if "error" in request.args:
flash("please use another sign in method then", "warning")
return redirect("/")
2019-07-08 14:13:00 +02:00
google = OAuth2Session(
GOOGLE_CLIENT_ID,
state=session["oauth_state"],
2019-07-08 19:44:11 +02:00
scope=_scope,
redirect_uri=_redirect_uri,
2019-07-08 14:13:00 +02:00
)
token = google.fetch_token(
2019-07-08 19:44:11 +02:00
_token_url,
2019-07-08 14:13:00 +02:00
client_secret=GOOGLE_CLIENT_SECRET,
authorization_response=request.url,
)
# Fetch a protected resource, i.e. user profile
# {
# "email": "abcd@gmail.com",
# "family_name": "First name",
# "given_name": "Last name",
# "id": "1234",
# "locale": "en",
# "name": "First Last",
# "picture": "http://profile.jpg",
# "verified_email": true
# }
google_user_data = google.get(
"https://www.googleapis.com/oauth2/v1/userinfo"
).json()
email = google_user_data["email"]
user = User.get_by(email=email)
picture_url = google_user_data.get("picture")
if user:
if picture_url and not user.profile_picture_id:
LOG.d("set user profile picture to %s", picture_url)
2020-03-20 09:52:00 +01:00
file = create_file_from_url(user, picture_url)
2019-07-08 14:13:00 +02:00
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) or email_already_used(email):
2020-03-05 11:00:58 +01:00
flash(f"You cannot use {email} as your personal inbox.", "error")
return redirect(url_for("auth.login"))
2019-07-08 14:13:00 +02:00
LOG.d("create google user with %s", google_user_data)
user = User.create(
2020-04-09 22:22:26 +02:00
email=email.lower(),
name=google_user_data["name"],
activated=True,
referral=get_referral(),
)
2020-03-20 09:52:00 +01:00
db.session.flush()
2019-07-08 14:13:00 +02:00
if picture_url:
LOG.d("set user profile picture to %s", picture_url)
2020-03-20 09:52:00 +01:00
file = create_file_from_url(user, picture_url)
2019-07-08 14:13:00 +02:00
user.profile_picture_id = file.id
db.session.commit()
login_user(user)
email_utils.send_welcome_email(user)
2019-07-08 14:13:00 +02:00
flash(f"Welcome to SimpleLogin {user.name}!", "success")
next_url = None
2019-07-08 14:13:00 +02:00
# The activation link contains the original page, for ex authorize page
if "google_next_url" in session:
next_url = session["google_next_url"]
2019-07-08 14:13:00 +02:00
LOG.debug("redirect user to %s", next_url)
# reset the next_url to avoid user getting redirected at each login :)
session.pop("google_next_url", None)
2020-02-27 16:57:37 +01:00
if not SocialAuth.get_by(user_id=user.id, social="google"):
SocialAuth.create(user_id=user.id, social="google")
db.session.commit()
return after_login(user, next_url)
2019-07-08 14:13:00 +02:00
2020-03-20 09:52:00 +01:00
def create_file_from_url(user, url) -> File:
2019-07-08 14:13:00 +02:00
file_path = random_string(30)
2020-03-20 09:52:00 +01:00
file = File.create(path=file_path, user_id=user.id)
2019-07-08 14:13:00 +02:00
s3.upload_from_url(url, file_path)
db.session.flush()
LOG.d("upload file %s to s3", file)
return file