diff options
| author | Tom Harley | 2021-07-07 15:41:42 +0000 |
|---|---|---|
| committer | Tom Harley | 2021-07-07 15:41:42 +0000 |
| commit | 6e58d1e712068de46f68a1188328d68273605f5b (patch) | |
| tree | 9df3ed956ecc6fba0aac1b2f121188ef7b5b2811 /sw | |
| parent | Begin work on adding user interaction (diff) | |
| download | swap-6e58d1e712068de46f68a1188328d68273605f5b.tar.gz swap-6e58d1e712068de46f68a1188328d68273605f5b.zip | |
Add testing for authorisation decorators
Test addition commits always look big lmao
As mentioned in the previous commit, this commit adds a check for the correct user when accessing someone’s /inbox.
Diffstat (limited to 'sw')
| -rw-r--r-- | sw/auth.py | 13 | ||||
| -rw-r--r-- | sw/users.py | 4 |
2 files changed, 11 insertions, 6 deletions
@@ -1,6 +1,9 @@ import functools +import logging + from flask import ( + abort, Blueprint, flash, g, @@ -99,20 +102,20 @@ def login(): session.clear() session["user_id"] = user["id"] - return 200 + return "Login Successful", 200 @bp.route("/logout") def logout(): session.clear() - return 200 + return "Logout Successful", 200 def login_required(view): @functools.wraps(view) def wrapped_view(**kwargs): if g.user is None: - abort("Login required", 403) + abort(403, "Login required") return view(**kwargs) @@ -123,8 +126,8 @@ 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) + if (g.user is None) or (g.user["username"] != username): + abort(403, "Unauthorised") return view(**kwargs) diff --git a/sw/users.py b/sw/users.py index 724965e..dffae93 100644 --- a/sw/users.py +++ b/sw/users.py @@ -1,9 +1,10 @@ -from flask import Blueprint, jsonify, url_for, request +from flask import Blueprint, jsonify, url_for, request, abort import json from sw.db import get_db from sw.time import now +from sw.auth import specific_user_required bp = Blueprint("users", __name__, url_prefix="/u") @@ -36,6 +37,7 @@ def get_user_info(username): @bp.route("/<username>/inbox") +@specific_user_required def get_user_inbox(username): db = get_db() |
