Compare commits

...

3 commits

6 changed files with 60 additions and 16 deletions

View file

@ -1,6 +1,6 @@
body { body {
font-family: sans-serif; font-family: sans-serif;
margin: 5%; margin: 2.5%;
text-align: center; text-align: center;
background-color: #282c37; background-color: #282c37;
color: white; color: white;
@ -19,8 +19,10 @@ h2 {
} }
#footer { #footer {
position:absolute; position:absolute;
bottom:5%; bottom:2.5%;
width:90%; width:95%;
color:#aaa;
font-size:0.8em;
} }
#logo-main { #logo-main {
height:300px; height:300px;
@ -37,7 +39,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 +49,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;
} }

5
templates/footer.html Normal file
View file

@ -0,0 +1,5 @@
<div id='footer'>
Note that Curious Greg uses a cookie to store your login state. Deleting the cookie used by Curious Greg will log you out of your account. Posting will still function as normal.<br />
Curious Greg will not function without JavaScript. Please ensure you have JavaScript enabled.<br />
Created by <a href='https://fedi.lynnesbian.space/@lynnesbian'>@lynnesbian@fedi.lynnesbian.space</a> (message her about any bugs you find). Source code is available <a href='https://git.lynnesbian.space/curious-greg'>here</a>, under the <a href='https://www.mozilla.org/en-US/MPL/2.0/'>Mozilla Public License Version 2.0</a>.
</div>

View file

@ -16,9 +16,6 @@
<div id='body'> <div id='body'>
You haven't posted to Curious Cat in a while, so we'll wait <strong>14 minutes</strong> until we check for new answers. You haven't posted to Curious Cat in a while, so we'll wait <strong>14 minutes</strong> until we check for new answers.
</div> </div>
<div id='footer'> {% include 'footer.html' %}
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.
</div>
</body> </body>
</html> </html>

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.

25
templates/login.html Normal file
View file

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>Curious Greg - Login</title>
<link rel='stylesheet' type='text/css' href="{{ url_for('static', filename='style.css') }}" />
<script type="text/javascript" src="{{ url_for('static', filename='script.js') }}"></script>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300" rel="stylesheet">
</head>
<body>
<h1>Log in</h1>
<h2>Log in to your Curious Greg account.</h2>
<noscript>
Curious Greg will not function without JavaScript. Please ensure you have JavaScript enabled.
</noscript>
<!-- <div id='logo-main'></div> -->
<form action='/internal/do_login' method='POST'>
<label for='acct'>Mastodon Account</label><br />
<input name='acct' placeholder='@you@instan.ce' required /><br />
<label for='pw'>Password</label><br />
<input type='password' placeholder='••••••••' name='pw' required /><br />
<button>Log In</button>
</form>
{% include 'footer.html' %}
</body>
</html>

22
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,7 +51,7 @@ 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",
} }
@ -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))