summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sw/__init__.py3
-rw-r--r--sw/auth.py22
-rw-r--r--sw/db.py16
-rw-r--r--sw/schema.sql36
-rw-r--r--sw/time.py9
-rw-r--r--sw/users.py58
-rw-r--r--tests/data.sql2
-rw-r--r--tests/test_auth.py2
8 files changed, 136 insertions, 12 deletions
diff --git a/sw/__init__.py b/sw/__init__.py
index 2601940..b4224fb 100644
--- a/sw/__init__.py
+++ b/sw/__init__.py
@@ -2,7 +2,7 @@ import os
from flask import Flask
-from sw import auth, ap, db
+from sw import auth, ap, db, users
def create_app(test_config=None):
@@ -31,5 +31,6 @@ def create_app(test_config=None):
app.register_blueprint(auth.bp)
app.register_blueprint(ap.bp)
+ app.register_blueprint(users.bp)
return app
diff --git a/sw/auth.py b/sw/auth.py
index 5429171..e1173c8 100644
--- a/sw/auth.py
+++ b/sw/auth.py
@@ -28,7 +28,7 @@ def load_logged_in_user():
else:
g.user = (
- get_db().execute("select * from user where id=?", (user_id,)).fetchone()
+ get_db().execute("select * from users where id=?", (user_id,)).fetchone()
)
@@ -62,14 +62,14 @@ def register():
db = get_db()
existing_user = db.execute(
- "select id from user where username=?", (username,)
+ "select id from users 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 (?, ?)",
+ "insert into users (username, password) values (?, ?)",
(username, generate_password_hash(password)),
)
db.commit()
@@ -89,7 +89,7 @@ def login():
password = request.json["password"]
db = get_db()
- user = db.execute("select * from user where username=?", (username,)).fetchone()
+ user = db.execute("select * from users where username=?", (username,)).fetchone()
if user is None:
return "Username or password incorrect", 403
@@ -112,7 +112,19 @@ def login_required(view):
@functools.wraps(view)
def wrapped_view(**kwargs):
if g.user is None:
- return "Login required", 403
+ abort("Login required", 403)
+
+ return view(**kwargs)
+
+ return wrapped_view
+
+
+def specific_user_required(view):
+ @functools.wraps(view)
+ def wrapped_view(**kwargs):
+ username = kwargs.get("username") or kwargs.get("user")
+ if g.user != username:
+ abort("Unauthorised", 403)
return view(**kwargs)
diff --git a/sw/db.py b/sw/db.py
index 372d7ae..dff8616 100644
--- a/sw/db.py
+++ b/sw/db.py
@@ -4,6 +4,8 @@ import click
from flask import current_app, g
from flask.cli import with_appcontext
+from werkzeug.security import generate_password_hash
+
def get_db():
if "db" not in g:
@@ -41,6 +43,20 @@ def init_db_command():
click.echo("Initialised the database.")
+@click.command("add-user")
+@click.argument("username")
+@click.argument("password")
+@with_appcontext
+def add_user_command(username, password):
+ db = get_db()
+ db.execute(
+ "insert into users (username, password) values (?, ?);",
+ (username, generate_password_hash(password)),
+ )
+ db.commit()
+
+
def init_app(app):
app.teardown_appcontext(close_db)
app.cli.add_command(init_db_command)
+ app.cli.add_command(add_user_command)
diff --git a/sw/schema.sql b/sw/schema.sql
index d6ee27a..a51a7b9 100644
--- a/sw/schema.sql
+++ b/sw/schema.sql
@@ -39,10 +39,38 @@ create table inbox (
ap_instrument text
);
-drop table if exists user;
+drop table if exists users;
-create table user (
- id integer primary key autoincrement,
+create table users (
+ id integer primary key,
username text unique not null,
- password text not null
+ password text not null,
+ inbox_size integer default 0,
+
+ 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
);
diff --git a/sw/time.py b/sw/time.py
new file mode 100644
index 0000000..8111232
--- /dev/null
+++ b/sw/time.py
@@ -0,0 +1,9 @@
+from datetime import datetime
+
+
+def time_formatted(t):
+ return t.replace(microsecond=0).isoformat()
+
+
+def now():
+ return time_formatted(datetime.now())
diff --git a/sw/users.py b/sw/users.py
new file mode 100644
index 0000000..724965e
--- /dev/null
+++ b/sw/users.py
@@ -0,0 +1,58 @@
+from flask import Blueprint, jsonify, url_for, request
+
+import json
+
+from sw.db import get_db
+from sw.time import now
+
+bp = Blueprint("users", __name__, url_prefix="/u")
+
+
+def ap(r):
+ def f(k):
+ return k.startswith("ap_") and r[k] is not None
+
+ res = {k: r[k] for k in filter(f, r.keys())}
+
+ res["id"] = url_for(".get_user_info", username=r["username"])
+ res["inbox"] = url_for(".get_user_inbox", username=r["username"])
+ # res["outbox"] = url_for(".get_user_outbox", username=r["username"])
+ # res["following"] = url_for(".get_user_following", username=r["username"])
+ # res["followers"] = url_for(".get_user_followers", username=r["username"])
+
+ return res
+
+
+@bp.route("/<username>")
+def get_user_info(username):
+ db = get_db()
+
+ user = db.execute("select * from users where username=?", (username,)).fetchone()
+
+ if user is None:
+ abort(404, "User not found")
+
+ return jsonify(ap(user))
+
+
+@bp.route("/<username>/inbox")
+def get_user_inbox(username):
+ db = get_db()
+
+ inbox_size = db.execute(
+ "select inbox_size from users where username=?", (username,)
+ ).fetchone()[0]
+
+ if inbox_size is None:
+ abort(404, "User not found")
+
+ request_time = request.date or now()
+
+ return jsonify(
+ {
+ "type": "OrderedCollection",
+ "totalItems": inbox_size,
+ "first": url_for(".get_user_inbox", username=username, before=request_time),
+ "current": url_for(".get_user_inbox", username=username),
+ }
+ )
diff --git a/tests/data.sql b/tests/data.sql
index 4579284..eb1f36e 100644
--- a/tests/data.sql
+++ b/tests/data.sql
@@ -1,4 +1,4 @@
-insert into user (username, password)
+insert into users (username, password)
values
('test', 'pbkdf2:sha256:50000$TCI4GzcX$0de171a4f4dac32e3364c7ddc7c14f3e2fa61f2d17574483f7ffbb431b4acb2f'),
('other', 'pbkdf2:sha256:50000$kJPKsz6N$d2d4784f1b030a9761f5ccaeeaca413f27f2ecb76d6168407af962ddce849f79');
diff --git a/tests/test_auth.py b/tests/test_auth.py
index 3fb2493..067746d 100644
--- a/tests/test_auth.py
+++ b/tests/test_auth.py
@@ -16,7 +16,7 @@ def test_register(client, app):
with app.app_context():
db_result = (
- get_db().execute("select * from user where username='a'",).fetchone()
+ get_db().execute("select * from users where username='a'",).fetchone()
)
assert db_result is not None