add github login

This commit is contained in:
Son NK 2019-07-07 22:46:03 +02:00
parent 4883ace2ed
commit aae76f21f6
6 changed files with 218 additions and 70 deletions

View File

@ -6,4 +6,5 @@ from .views import (
resend_activation,
reset_password,
forgot_password,
github,
)

View File

@ -1,60 +1,86 @@
{% 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 %}
{% if show_resend_activation %}
<div class="text-center text-muted small mb-4">
You haven't received the activation email?
<a href="{{ url_for('auth.resend_activation') }}">Resend</a>
</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?
<a href="{{ url_for('auth.resend_activation') }}">Resend</a>
</div>
{% endif %}
<form class="card" method="post">
{{ form.csrf_token }}
<div class="card-body p-6">
<div class="card-title">Login to your account</div>
<form class="card" method="post">
{{ form.csrf_token }}
<div class="card-body p-6">
<div class="card-title">Login to your account</div>
<div class="form-group">
<label class="form-label">Email address</label>
{{ form.email(class="form-control", type="email") }}
{{ render_field_errors(form.email) }}
<div class="form-group">
<label class="form-label">Email address</label>
{{ form.email(class="form-control", type="email") }}
{{ render_field_errors(form.email) }}
</div>
<div class="form-group">
<label class="form-label">
Password
<a href="{{ url_for('auth.forgot_password') }}" class="float-right small">
I forgot my password
</a>
</label>
{{ form.password(class="form-control", type="password") }}
{{ render_field_errors(form.password) }}
</div>
<!-- TODO: add remember me
<div class="form-group">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input"/>
<span class="custom-control-label">Remember me</span>
</label>
</div>
-->
<div class="form-footer">
<button type="submit" class="btn btn-primary btn-block">Log in</button>
</div>
</div>
</form>
<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="form-group">
<label class="form-label">
Password
<a href="{{ url_for('auth.forgot_password') }}" class="float-right small">
I forgot my password
<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>
</label>
{{ form.password(class="form-control", type="password") }}
{{ render_field_errors(form.password) }}
</div>
<!-- TODO: add remember me
<div class="form-group">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input"/>
<span class="custom-control-label">Remember me</span>
</label>
</div>
-->
<div class="form-footer">
<button type="submit" class="btn btn-primary btn-block">Log in</button>
</div>
</div>
</form>
<div class="text-center text-muted">
Don't have an account yet? <a href="{{ url_for('auth.register') }}">Sign up</a>
</div>

View File

@ -4,47 +4,71 @@
Register
{% endblock %}
{% block head %}
<style>
.col-login {
max-width: 48rem;
}
</style>
{% endblock %}
{% block single_content %}
<form class="card" method="post">
{{ form.csrf_token }}
<div class="card-body p-6">
<div class="card-title">Create new account</div>
<div class="row">
<div class="col-md-6">
<form class="card" method="post">
{{ form.csrf_token }}
<div class="card-body p-6">
<div class="card-title">Create new account</div>
<div class="form-group">
<label class="form-label">How should we call you?</label>
{{ form.name(class="form-control") }}
{{ render_field_errors(form.name) }}
</div>
<div class="form-group">
<label class="form-label">How should we call you?</label>
{{ form.name(class="form-control") }}
{{ render_field_errors(form.name) }}
</div>
<div class="form-group">
<label class="form-label">Email address</label>
{{ form.email(class="form-control", type="email") }}
{{ render_field_errors(form.email) }}
</div>
<div class="form-group">
<label class="form-label">Email address</label>
{{ form.email(class="form-control", type="email") }}
{{ render_field_errors(form.email) }}
</div>
<div class="form-group">
<label class="form-label">Password</label>
{{ form.password(class="form-control", type="password") }}
{{ render_field_errors(form.password) }}
</div>
<div class="form-group">
<label class="form-label">Password</label>
{{ form.password(class="form-control", type="password") }}
{{ render_field_errors(form.password) }}
</div>
<!-- TODO: add terms
<div class="form-group">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input"/>
<span class="custom-control-label">Agree the <a href="terms.html">terms and policy</a></span>
</label>
</div>
-->
<!-- TODO: add terms
<div class="form-group">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input"/>
<span class="custom-control-label">Agree the <a href="terms.html">terms and policy</a></span>
</label>
</div>
-->
<div class="form-footer">
<button type="submit" class="btn btn-primary btn-block">Create new account</button>
<div class="form-footer">
<button type="submit" class="btn btn-primary btn-block">Create new account</button>
</div>
</div>
</form>
<div class="text-center text-muted">
Already have account? <a href="{{ url_for('auth.login') }}">Sign in</a>
</div>
</div>
</form>
<div class="text-center text-muted">
Already have account? <a href="{{ url_for('auth.login') }}">Sign in</a>
<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 %}

93
app/auth/views/github.py Normal file
View File

@ -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"))

View File

@ -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():

View File

@ -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 %}