contentdb/app/views/githublogin.py

70 lines
2.3 KiB
Python
Raw Normal View History

2018-05-17 16:18:20 +02:00
# Content DB
# Copyright (C) 2018 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
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-05-15 20:35:59 +02:00
from app.utils import loginUser
2018-03-18 19:05:53 +01:00
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():
return github.authorize("")
2018-03-18 19:05:53 +01:00
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:
2018-05-14 00:31:42 +02:00
flash("Unable to find an account for that Github user", "error")
return redirect(url_for("user_claim_page"))
2018-05-13 16:28:27 +02:00
elif loginUser(userByGithub):
2018-03-18 19:05:53 +01:00
return redirect(next_url or url_for("home_page"))
else:
flash("Authorization failed [err=gh-login-failed]", "danger")
return redirect(url_for("user.login"))