added login page links and redirect

This commit is contained in:
Lynne Megido 2018-11-06 20:30:48 +10:00
parent 7e66e47656
commit 3ab5364ef0
Signed by: lynnesbian
GPG key ID: FB7B970303ACE499
3 changed files with 24 additions and 9 deletions

View file

@ -37,7 +37,7 @@ input {
margin:20px; margin:20px;
font-size:1.2em; font-size:1.2em;
} }
button { button, .button {
font-size:1em; font-size:1em;
text-transform: uppercase; text-transform: uppercase;
color:#2b90d9; color:#2b90d9;
@ -47,8 +47,9 @@ button {
padding:10px 25px; padding:10px 25px;
transition:0.2s all ease-in; transition:0.2s all ease-in;
cursor: pointer; cursor: pointer;
text-decoration:none;
} }
button:hover{ button:hover, .button:hover{
background-color:#2b90d9; background-color:#2b90d9;
color:white; color:white;
} }

View file

@ -16,8 +16,10 @@
<form onsubmit='cont(); return false'> <form onsubmit='cont(); return false'>
<label for='instance'>Instance URL</label><br /> <label for='instance'>Instance URL</label><br />
<input name='instance' placeholder='mastodon.social' id='instance-input' /><br /> <input name='instance' placeholder='mastodon.social' id='instance-input' /><br />
<button class='loading'>Continue</button> <button class='loading'>Sign Up</button>
</form> </form>
<br /><br />
<a class='button' href='/login'>Log In</a>
<div id='footer'> <div id='footer'>
Note that Curious Greg requires first-party cookies to be enabled. You may safely delete the cookie upon completing the connection process.<br /> Note that Curious Greg requires first-party cookies to be enabled. You may safely delete the cookie upon completing the connection process.<br />
Curious Greg will not function without JavaScript. Please ensure you have JavaScript enabled. Curious Greg will not function without JavaScript. Please ensure you have JavaScript enabled.

24
web.py
View file

@ -7,13 +7,14 @@
import requests, sqlite3, json import requests, sqlite3, json
from mastodon import Mastodon from mastodon import Mastodon
from flask import Flask, render_template, request, session, redirect, url_for from flask import Flask, render_template, request, session, redirect, url_for
import bcrypt
import urllib import urllib
cfg = json.load(open("meta.json")) cfg = json.load(open("meta.json"))
db = sqlite3.connect("database.db") db = sqlite3.connect("database.db") #TODO: switch to mysql so concurrency is possible
c = db.cursor() c = db.cursor()
c.execute("CREATE TABLE IF NOT EXISTS `data` (secret TEXT NOT NULL, appid TEXT NOT NULL, appsecret TEXT NOT NULL, cc VARCHAR, latest_post VARCHAR)") c.execute("CREATE TABLE IF NOT EXISTS `data` (username TEXT NOT NULL, instance TEXT NOT NULL, secret TEXT NOT NULL, appid TEXT NOT NULL, appsecret TEXT NOT NULL, cc TEXT, latest_post TEXT, latest_timestamp TEXT, time_between_checks INT)")
app = Flask(cfg['name']) app = Flask(cfg['name'])
app.secret_key = cfg['flask_key'] app.secret_key = cfg['flask_key']
@ -42,7 +43,7 @@ def internal_auth_a():
session['client_id'], session['client_secret'] = Mastodon.create_app(cfg['name'], session['client_id'], session['client_secret'] = Mastodon.create_app(cfg['name'],
api_base_url=session['instance_url'], api_base_url=session['instance_url'],
scopes=["write:statuses"], scopes=["write:statuses", "read:accounts"],
website=cfg['website'], website=cfg['website'],
redirect_uris=['https://cg.lynnesbian.space/internal/auth_b'] redirect_uris=['https://cg.lynnesbian.space/internal/auth_b']
) )
@ -50,11 +51,11 @@ def internal_auth_a():
params = { params = {
"client_id": session['client_id'], "client_id": session['client_id'],
"client_secret":session['client_secret'], "client_secret":session['client_secret'],
"scope":"write:statuses", "scope":"write:statuses+read:accounts",
"redirect_uri": "https://cg.lynnesbian.space/internal/auth_b", "redirect_uri": "https://cg.lynnesbian.space/internal/auth_b",
"response_type":"code", "response_type":"code",
} }
url = "{}/oauth/authorize?{}".format(session['instance_url'], urllib.parse.urlencode(params)) url = "{}/oauth/authorize?{}".format(session['instance_url'], urllib.parse.urlencode(params))
return url return url
@ -63,11 +64,22 @@ def internal_auth_a():
def internal_auth_b(): def internal_auth_b():
session['secret'] = request.args.get('code') session['secret'] = request.args.get('code')
#write details to DB #write details to DB
c.execute("INSERT INTO data (secret, appid, appsecret) VALUES (?, ?, ?, ?)", (session['secret'], session['client_id'], session['client_secret'])) client = Mastodon(access_token = session['secret'], api_base_url=session['instance_url'])
session['username'] = client.account_verify_credentials()['username']
session['acct'] = "@{}@{}".format(session['username'], session['instance_url'].replace("https://", ""))
if c.execute("SELECT COUNT(*) FROM data WHERE username LIKE ? AND instance LIKE ?", (session['username'], session['instance_url'])).fetchone()[0] > 0:
#user already has an account with CG
return redirect(url_for('log_in'))
c.execute("INSERT INTO data (username, instance, secret, appid, appsecret) VALUES (?, ?, ?, ?, ?)", (session['username'], session['instance_url'], session['secret'], session['client_id'], session['client_secret']))
db.commit() db.commit()
return redirect(url_for('home'))
@app.route('/debug') @app.route('/debug')
def print_debug_info(): def print_debug_info():
return json.dumps(session._get_current_object()) return json.dumps(session._get_current_object())
@app.route('/login')
def log_in():
return render_template("login.html")
# return(json.dumps(client_info)) # return(json.dumps(client_info))