2020-01-26 13:51:43 +01:00
|
|
|
from flask import request, session, redirect, flash, url_for
|
2019-07-07 22:46:03 +02:00
|
|
|
from flask_login import login_user
|
|
|
|
from requests_oauthlib import OAuth2Session
|
|
|
|
|
2019-11-16 23:03:37 +01:00
|
|
|
from app import email_utils
|
2019-07-07 22:46:03 +02:00
|
|
|
from app.auth.base import auth_bp
|
2020-04-09 22:22:26 +02:00
|
|
|
from app.auth.views.login_utils import after_login, get_referral
|
2020-01-28 05:50:25 +01:00
|
|
|
from app.config import GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, URL, DISABLE_REGISTRATION
|
2020-02-10 17:14:36 +01:00
|
|
|
from app.email_utils import can_be_used_as_personal_email, email_already_used
|
2019-07-07 22:46:03 +02:00
|
|
|
from app.extensions import db
|
|
|
|
from app.log import LOG
|
2020-02-27 16:16:12 +01:00
|
|
|
from app.models import User, SocialAuth
|
2019-08-30 22:12:31 +02:00
|
|
|
from app.utils import encode_url
|
2019-07-07 22:46:03 +02:00
|
|
|
|
2019-07-08 19:33:28 +02:00
|
|
|
_authorization_base_url = "https://github.com/login/oauth/authorize"
|
|
|
|
_token_url = "https://github.com/login/oauth/access_token"
|
2019-07-07 22:46:03 +02:00
|
|
|
|
2019-07-07 22:56:55 +02:00
|
|
|
# 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:33:28 +02:00
|
|
|
_redirect_uri = URL + "/auth/github/callback"
|
2019-07-07 22:56:55 +02:00
|
|
|
|
2019-07-07 22:46:03 +02:00
|
|
|
|
|
|
|
@auth_bp.route("/github/login")
|
|
|
|
def github_login():
|
2019-07-08 19:33:28 +02:00
|
|
|
next_url = request.args.get("next")
|
|
|
|
if next_url:
|
|
|
|
redirect_uri = _redirect_uri + "?next=" + encode_url(next_url)
|
|
|
|
else:
|
|
|
|
redirect_uri = _redirect_uri
|
|
|
|
|
2019-07-07 22:56:55 +02:00
|
|
|
github = OAuth2Session(
|
|
|
|
GITHUB_CLIENT_ID, scope=["user:email"], redirect_uri=redirect_uri
|
|
|
|
)
|
2019-07-08 19:33:28 +02:00
|
|
|
authorization_url, state = github.authorization_url(_authorization_base_url)
|
2019-07-07 22:46:03 +02:00
|
|
|
|
|
|
|
# State is used to prevent CSRF, keep this for later.
|
|
|
|
session["oauth_state"] = state
|
|
|
|
return redirect(authorization_url)
|
|
|
|
|
|
|
|
|
|
|
|
@auth_bp.route("/github/callback")
|
|
|
|
def github_callback():
|
2019-07-11 20:14:18 +02:00
|
|
|
# user clicks on cancel
|
|
|
|
if "error" in request.args:
|
2019-12-26 14:00:17 +01:00
|
|
|
flash("Please use another sign in method then", "warning")
|
2019-07-11 20:14:18 +02:00
|
|
|
return redirect("/")
|
|
|
|
|
2019-07-07 22:46:03 +02:00
|
|
|
github = OAuth2Session(
|
2019-07-07 22:56:55 +02:00
|
|
|
GITHUB_CLIENT_ID,
|
|
|
|
state=session["oauth_state"],
|
|
|
|
scope=["user:email"],
|
2019-07-08 19:33:28 +02:00
|
|
|
redirect_uri=_redirect_uri,
|
2019-07-07 22:46:03 +02:00
|
|
|
)
|
|
|
|
token = github.fetch_token(
|
2019-07-08 19:33:28 +02:00
|
|
|
_token_url,
|
2019-07-07 22:46:03 +02:00
|
|
|
client_secret=GITHUB_CLIENT_SECRET,
|
|
|
|
authorization_response=request.url,
|
|
|
|
)
|
|
|
|
|
|
|
|
# a dict with "name", "login"
|
|
|
|
github_user_data = github.get("https://api.github.com/user").json()
|
|
|
|
|
|
|
|
# return list of emails
|
|
|
|
# {
|
|
|
|
# 'email': 'abcd@gmail.com',
|
|
|
|
# 'primary': False,
|
|
|
|
# 'verified': True,
|
|
|
|
# 'visibility': None
|
|
|
|
# }
|
|
|
|
emails = github.get("https://api.github.com/user/emails").json()
|
|
|
|
|
|
|
|
# only take the primary email
|
|
|
|
email = None
|
|
|
|
|
|
|
|
for e in emails:
|
2020-03-05 16:45:49 +01:00
|
|
|
if e.get("verified") and e.get("primary"):
|
2019-07-07 22:46:03 +02:00
|
|
|
email = e.get("email")
|
|
|
|
break
|
|
|
|
|
|
|
|
if not email:
|
2020-03-05 16:46:02 +01:00
|
|
|
LOG.error(f"cannot get email for github user {github_user_data} {emails}")
|
|
|
|
flash(
|
|
|
|
"Cannot get a valid email from Github, please another way to login/sign up",
|
|
|
|
"error",
|
|
|
|
)
|
|
|
|
return redirect(url_for("auth.login"))
|
2019-07-07 22:46:03 +02:00
|
|
|
|
2020-03-05 10:58:58 +01:00
|
|
|
email = email.lower()
|
2019-07-07 22:46:03 +02:00
|
|
|
user = User.get_by(email=email)
|
|
|
|
|
|
|
|
# create user
|
2019-12-27 15:35:22 +01:00
|
|
|
if not user:
|
2020-01-28 05:50:25 +01:00
|
|
|
if DISABLE_REGISTRATION:
|
|
|
|
flash("Registration is closed", "error")
|
|
|
|
return redirect(url_for("auth.login"))
|
|
|
|
|
2020-02-10 17:14:36 +01:00
|
|
|
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")
|
2020-01-26 13:51:43 +01:00
|
|
|
return redirect(url_for("auth.login"))
|
|
|
|
|
2019-07-07 22:46:03 +02:00
|
|
|
LOG.d("create github user")
|
2019-11-17 11:32:51 +01:00
|
|
|
user = User.create(
|
2020-04-09 22:22:26 +02:00
|
|
|
email=email.lower(),
|
|
|
|
name=github_user_data.get("name") or "",
|
|
|
|
activated=True,
|
|
|
|
referral=get_referral(),
|
2019-11-17 11:32:51 +01:00
|
|
|
)
|
2019-07-07 22:46:03 +02:00
|
|
|
db.session.commit()
|
|
|
|
login_user(user)
|
2020-01-30 07:54:33 +01:00
|
|
|
email_utils.send_welcome_email(user)
|
2019-07-07 22:46:03 +02:00
|
|
|
|
|
|
|
flash(f"Welcome to SimpleLogin {user.name}!", "success")
|
|
|
|
|
2020-02-27 16:16:12 +01:00
|
|
|
if not SocialAuth.get_by(user_id=user.id, social="github"):
|
|
|
|
SocialAuth.create(user_id=user.id, social="github")
|
|
|
|
db.session.commit()
|
|
|
|
|
2019-07-07 22:46:03 +02:00
|
|
|
# The activation link contains the original page, for ex authorize page
|
2019-12-27 15:35:22 +01:00
|
|
|
next_url = request.args.get("next") if request.args else None
|
|
|
|
|
|
|
|
return after_login(user, next_url)
|