summaryrefslogtreecommitdiff
path: root/sw
diff options
context:
space:
mode:
authorTom Harley2021-07-03 00:18:13 +0000
committerTom Harley2021-07-03 00:18:13 +0000
commit7c5c6c52250ca57257ab4af2cc9cdf4425595ec0 (patch)
treed36019286125bf984e0c267645d53ff562533ebf /sw
downloadswap-7c5c6c52250ca57257ab4af2cc9cdf4425595ec0.tar.gz
swap-7c5c6c52250ca57257ab4af2cc9cdf4425595ec0.zip

Initial commit

Diffstat (limited to 'sw')
-rw-r--r--sw/__init__.py35
-rw-r--r--sw/ap.py9
-rw-r--r--sw/auth.py119
-rw-r--r--sw/db.py46
-rw-r--r--sw/schema.sql48
5 files changed, 257 insertions, 0 deletions
diff --git a/sw/__init__.py b/sw/__init__.py
new file mode 100644
index 0000000..2601940
--- /dev/null
+++ b/sw/__init__.py
@@ -0,0 +1,35 @@
+import os
+
+from flask import Flask
+
+from sw import auth, ap, db
+
+
+def create_app(test_config=None):
+ app = Flask(__name__, instance_relative_config=True)
+ app.config.from_mapping(
+ SECRET_KEY="dev", DATABASE=os.path.join(app.instance_path, "sw.sqlite"),
+ )
+
+ if test_config is None:
+ app.config.from_pyfile("config.py", silent=True)
+
+ else:
+ app.config.update(test_config)
+
+ try:
+ os.makedirs(app.instance_path)
+
+ except OSError:
+ pass
+
+ @app.route("/hello")
+ def hello():
+ return "Hello, World!"
+
+ db.init_app(app)
+
+ app.register_blueprint(auth.bp)
+ app.register_blueprint(ap.bp)
+
+ return app
diff --git a/sw/ap.py b/sw/ap.py
new file mode 100644
index 0000000..c3301f2
--- /dev/null
+++ b/sw/ap.py
@@ -0,0 +1,9 @@
+from flask import Blueprint
+
+bp = Blueprint("ap", __name__)
+
+bp.route("/u/<user>", methods=["GET"])
+
+
+def get_user(user):
+ return 500
diff --git a/sw/auth.py b/sw/auth.py
new file mode 100644
index 0000000..5429171
--- /dev/null
+++ b/sw/auth.py
@@ -0,0 +1,119 @@
+import functools
+
+from flask import (
+ Blueprint,
+ flash,
+ g,
+ redirect,
+ render_template,
+ request,
+ session,
+ url_for,
+ Response,
+)
+
+from werkzeug.security import generate_password_hash, check_password_hash
+
+from sw.db import get_db
+
+bp = Blueprint("auth", __name__, url_prefix="/auth")
+
+
+@bp.before_app_request
+def load_logged_in_user():
+ user_id = session.get("user_id")
+
+ if user_id is None:
+ g.user = None
+
+ else:
+ g.user = (
+ get_db().execute("select * from user where id=?", (user_id,)).fetchone()
+ )
+
+
+@bp.route("/register", methods=["POST"])
+def register():
+ if not request.is_json:
+ return "Request body is not JSON", 422
+
+ data = request.json
+
+ if "username" not in data:
+ return "Username required", 400
+
+ if "password" not in data:
+ return "Password required", 400
+
+ username = data["username"]
+ password = data["password"]
+
+ if username is None:
+ return "Username required", 400
+
+ if password is None:
+ return "Password required", 400
+
+ if username == "":
+ return "Username must not be empty", 400
+
+ if password == "":
+ return "Password must not be empty", 400
+
+ db = get_db()
+ existing_user = db.execute(
+ "select id from user where username=?", (username,)
+ ).fetchone()
+
+ if existing_user is not None:
+ return f"Username {username} is taken", 409
+
+ db.execute(
+ "insert into user (username, password) values (?, ?)",
+ (username, generate_password_hash(password)),
+ )
+ db.commit()
+
+ response = Response(status=201)
+ # response.location = url_for("ap.get_user", user=username)
+
+ return response
+
+
+@bp.route("/login", methods=["POST"])
+def login():
+ if not request.is_json:
+ return "Request body is not JSON", 422
+
+ username = request.json["username"]
+ password = request.json["password"]
+
+ db = get_db()
+ user = db.execute("select * from user where username=?", (username,)).fetchone()
+
+ if user is None:
+ return "Username or password incorrect", 403
+
+ if not check_password_hash(user["password"], password):
+ return "Username or password incorrect", 403
+
+ session.clear()
+ session["user_id"] = user["id"]
+ return 200
+
+
+@bp.route("/logout")
+def logout():
+ session.clear()
+ return 200
+
+
+def login_required(view):
+ @functools.wraps(view)
+ def wrapped_view(**kwargs):
+ if g.user is None:
+ return "Login required", 403
+
+ return view(**kwargs)
+
+ return wrapped_view
diff --git a/sw/db.py b/sw/db.py
new file mode 100644
index 0000000..372d7ae
--- /dev/null
+++ b/sw/db.py
@@ -0,0 +1,46 @@
+import sqlite3
+
+import click
+from flask import current_app, g
+from flask.cli import with_appcontext
+
+
+def get_db():
+ if "db" not in g:
+ g.db = sqlite3.connect(
+ current_app.config["DATABASE"], detect_types=sqlite3.PARSE_DECLTYPES,
+ )
+
+ g.db.row_factory = sqlite3.Row
+
+ return g.db
+
+
+def get_db_cursor():
+ return get_db().cursor()
+
+
+def close_db(e=None):
+ db = g.pop("db", None)
+
+ if db is not None:
+ db.close()
+
+
+def init_db():
+ db = get_db()
+
+ with current_app.open_resource("schema.sql") as f:
+ db.executescript(f.read().decode("utf8"))
+
+
+@click.command("init-db")
+@with_appcontext
+def init_db_command():
+ init_db()
+ click.echo("Initialised the database.")
+
+
+def init_app(app):
+ app.teardown_appcontext(close_db)
+ app.cli.add_command(init_db_command)
diff --git a/sw/schema.sql b/sw/schema.sql
new file mode 100644
index 0000000..d6ee27a
--- /dev/null
+++ b/sw/schema.sql
@@ -0,0 +1,48 @@
+drop table if exists inbox;
+
+create table inbox (
+ recipient text,
+ received int,
+
+ ap_attachment text,
+ ap_attributedTo text,
+ ap_audience text,
+ ap_content text,
+ ap_context text,
+ ap_name text,
+ ap_endTime text,
+ ap_generator text,
+ ap_icon text,
+ ap_image text,
+ ap_inReplyTo text,
+ ap_location text,
+ ap_preview text,
+ ap_published text,
+ ap_replies text,
+ ap_startTime text,
+ ap_summary text,
+ ap_tag text,
+ ap_updated text,
+ ap_url text,
+ ap_to text,
+ ap_bto text,
+ ap_cc text,
+ ap_bcc text,
+ ap_mediaType text,
+ ap_duration text,
+
+ ap_actor text,
+ ap_object text,
+ ap_target text,
+ ap_result text,
+ ap_origin text,
+ ap_instrument text
+);
+
+drop table if exists user;
+
+create table user (
+ id integer primary key autoincrement,
+ username text unique not null,
+ password text not null
+);