add github login
This commit is contained in:
parent
4883ace2ed
commit
aae76f21f6
|
@ -6,4 +6,5 @@ from .views import (
|
|||
resend_activation,
|
||||
reset_password,
|
||||
forgot_password,
|
||||
github,
|
||||
)
|
||||
|
|
|
@ -1,14 +1,26 @@
|
|||
{% extends "single.html" %}
|
||||
|
||||
|
||||
{% block title %}
|
||||
Login
|
||||
{% endblock %}
|
||||
|
||||
|
||||
{% block head %}
|
||||
<style>
|
||||
.col-login {
|
||||
max-width: 48rem;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
{% block single_content %}
|
||||
{% if error %}
|
||||
<div class="text-danger text-center mb-4">{{ error }}</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
{% if show_resend_activation %}
|
||||
<div class="text-center text-muted small mb-4">
|
||||
You haven't received the activation email?
|
||||
|
@ -56,6 +68,20 @@
|
|||
<div class="text-center text-muted">
|
||||
Don't have an account yet? <a href="{{ url_for('auth.register') }}">Sign up</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-6">
|
||||
<div class="card">
|
||||
<div class="card-body p-6">
|
||||
<div class="card-title">Social sign in</div>
|
||||
|
||||
<a href="{{ url_for('auth.github_login') }}" class="btn btn-block btn-social btn-github">
|
||||
<i class="fa fa-github"></i> Sign in with Github
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
{% endblock %}
|
|
@ -4,7 +4,18 @@
|
|||
Register
|
||||
{% endblock %}
|
||||
|
||||
{% block head %}
|
||||
<style>
|
||||
.col-login {
|
||||
max-width: 48rem;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
|
||||
{% block single_content %}
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<form class="card" method="post">
|
||||
{{ form.csrf_token }}
|
||||
<div class="card-body p-6">
|
||||
|
@ -46,5 +57,18 @@
|
|||
<div class="text-center text-muted">
|
||||
Already have account? <a href="{{ url_for('auth.login') }}">Sign in</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-6">
|
||||
<div class="card">
|
||||
<div class="card-body p-6">
|
||||
<div class="card-title">Social sign up</div>
|
||||
|
||||
<a href="{{ url_for('auth.github_login') }}" class="btn btn-block btn-social btn-github">
|
||||
<i class="fa fa-github"></i> Sign up with Github
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
|
@ -0,0 +1,93 @@
|
|||
import arrow
|
||||
from flask import request, session, redirect, url_for, flash
|
||||
from flask_login import login_user
|
||||
from requests_oauthlib import OAuth2Session
|
||||
|
||||
from app.auth.base import auth_bp
|
||||
from app.config import GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET
|
||||
from app.email_utils import notify_admin
|
||||
from app.extensions import db
|
||||
from app.log import LOG
|
||||
from app.models import User
|
||||
from app.utils import random_string
|
||||
|
||||
authorization_base_url = "https://github.com/login/oauth/authorize"
|
||||
token_url = "https://github.com/login/oauth/access_token"
|
||||
|
||||
|
||||
@auth_bp.route("/github/login")
|
||||
def github_login():
|
||||
github = OAuth2Session(GITHUB_CLIENT_ID, scope=["user:email"])
|
||||
authorization_url, state = github.authorization_url(authorization_base_url)
|
||||
|
||||
# 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():
|
||||
github = OAuth2Session(
|
||||
GITHUB_CLIENT_ID, state=session["oauth_state"], scope=["user:email"]
|
||||
)
|
||||
token = github.fetch_token(
|
||||
token_url,
|
||||
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()
|
||||
LOG.d("user login with github %s", github_user_data)
|
||||
|
||||
# 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:
|
||||
if e.get("verified") and e.get("primary"):
|
||||
email = e.get("email")
|
||||
break
|
||||
|
||||
if not email:
|
||||
raise Exception("cannot get email for github user")
|
||||
|
||||
user = User.get_by(email=email)
|
||||
|
||||
if user:
|
||||
login_user(user)
|
||||
# create user
|
||||
else:
|
||||
LOG.d("create github user")
|
||||
user = User.create(email=email, name=github_user_data["name"])
|
||||
|
||||
# set a random password
|
||||
user.set_password(random_string(20))
|
||||
|
||||
user.activated = True
|
||||
|
||||
db.session.commit()
|
||||
login_user(user)
|
||||
|
||||
flash(f"Welcome to SimpleLogin {user.name}!", "success")
|
||||
|
||||
notify_admin(
|
||||
f"new user signs up {user.email}", f"{user.name} signs up at {arrow.now()}"
|
||||
)
|
||||
|
||||
# The activation link contains the original page, for ex authorize page
|
||||
if "next" in request.args:
|
||||
next_url = request.args.get("next")
|
||||
LOG.debug("redirect user to %s", next_url)
|
||||
return redirect(next_url)
|
||||
else:
|
||||
LOG.debug("redirect user to dashboard")
|
||||
return redirect(url_for("dashboard.index"))
|
|
@ -274,6 +274,8 @@ if __name__ == "__main__":
|
|||
# enable to print all queries generated by sqlalchemy
|
||||
# app.config["SQLALCHEMY_ECHO"] = True
|
||||
|
||||
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
|
||||
|
||||
if ENV == "local":
|
||||
LOG.d("reset db, add fake data")
|
||||
with app.app_context():
|
||||
|
|
|
@ -55,6 +55,8 @@
|
|||
<!-- Sentry -->
|
||||
<script src="https://browser.sentry-cdn.com/5.4.3/bundle.min.js" crossorigin="anonymous"></script>
|
||||
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-social/4.12.0/bootstrap-social.min.css">
|
||||
|
||||
<link rel="stylesheet" type="text/css" href="/static/style.css?v={{ VERSION }}">
|
||||
|
||||
{% if LYRA_ANALYTICS_ID %}
|
||||
|
|
Loading…
Reference in New Issue