2018-03-18 19:05:53 +01:00
|
|
|
from flask import *
|
|
|
|
from flask_user import *
|
|
|
|
from flask_login import login_user, logout_user
|
|
|
|
import flask_menu as menu
|
|
|
|
from flask_github import GitHub
|
|
|
|
from app import app, github
|
|
|
|
from app.models import *
|
|
|
|
|
|
|
|
|
2018-03-21 23:03:37 +01:00
|
|
|
@app.route("/user/github/start/")
|
2018-03-18 19:05:53 +01:00
|
|
|
def github_signin_page():
|
2018-03-21 23:05:32 +01:00
|
|
|
return github.authorize("")
|
2018-03-18 19:05:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _do_login_user(user, remember_me=False):
|
|
|
|
def _call_or_get(v):
|
|
|
|
if callable(v):
|
|
|
|
return v()
|
|
|
|
else:
|
|
|
|
return v
|
|
|
|
|
|
|
|
# User must have been authenticated
|
|
|
|
if not user:
|
|
|
|
return False
|
|
|
|
|
|
|
|
user.active = True
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# Check if user account has been disabled
|
|
|
|
if not _call_or_get(user.is_active):
|
2018-03-21 23:03:37 +01:00
|
|
|
flash("Your account has not been enabled.", "error")
|
2018-03-18 19:05:53 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
# Check if user has a confirmed email address
|
|
|
|
user_manager = current_app.user_manager
|
|
|
|
if user_manager.enable_email and user_manager.enable_confirm_email \
|
|
|
|
and not current_app.user_manager.enable_login_without_confirm_email \
|
|
|
|
and not user.has_confirmed_email():
|
2018-03-21 23:03:37 +01:00
|
|
|
url = url_for("user.resend_confirm_email")
|
|
|
|
flash("Your email address has not yet been confirmed", "error")
|
2018-03-18 19:05:53 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
# Use Flask-Login to sign in user
|
|
|
|
login_user(user, remember=remember_me)
|
|
|
|
signals.user_logged_in.send(current_app._get_current_object(), user=user)
|
|
|
|
|
2018-03-21 23:03:37 +01:00
|
|
|
flash("You have signed in successfully.", "success")
|
2018-03-18 19:05:53 +01:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _login_user(user):
|
|
|
|
user_mixin = None
|
|
|
|
if user_manager.enable_username:
|
|
|
|
user_mixin = user_manager.find_user_by_username(user.username)
|
|
|
|
|
|
|
|
return _do_login_user(user_mixin, False)
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-21 23:03:37 +01:00
|
|
|
@app.route("/user/github/callback/")
|
2018-03-18 19:05:53 +01:00
|
|
|
@github.authorized_handler
|
|
|
|
def github_authorized(oauth_token):
|
2018-03-21 23:03:37 +01:00
|
|
|
next_url = request.args.get("next")
|
2018-03-18 19:05:53 +01:00
|
|
|
if oauth_token is None:
|
|
|
|
flash("Authorization failed [err=gh-oauth-login-failed]", "danger")
|
|
|
|
return redirect(url_for("user.login"))
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
# Get Github username
|
|
|
|
url = "https://api.github.com/user"
|
|
|
|
r = requests.get(url, headers={"Authorization": "token " + oauth_token})
|
|
|
|
username = r.json()["login"]
|
|
|
|
|
|
|
|
# Get user by github username
|
|
|
|
userByGithub = User.query.filter_by(github_username=username).first()
|
|
|
|
|
|
|
|
# If logged in, connect
|
|
|
|
if current_user and current_user.is_authenticated:
|
|
|
|
if userByGithub is None:
|
|
|
|
current_user.github_username = username
|
|
|
|
db.session.add(auth)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for("gitAccount", id=auth.id))
|
|
|
|
else:
|
|
|
|
flash("Github account is already associated with another user", "danger")
|
|
|
|
return redirect(url_for("home_page"))
|
|
|
|
|
|
|
|
# If not logged in, log in
|
|
|
|
else:
|
|
|
|
if userByGithub is None:
|
|
|
|
flash("Authorization failed [err=gh-no-such-account]", "danger")
|
|
|
|
return redirect(url_for("user.login"))
|
|
|
|
elif _login_user(userByGithub):
|
|
|
|
return redirect(next_url or url_for("home_page"))
|
|
|
|
else:
|
|
|
|
flash("Authorization failed [err=gh-login-failed]", "danger")
|
|
|
|
return redirect(url_for("user.login"))
|