From 95da8038f03cfaa85cb2b268493e77799c4fd2c2 Mon Sep 17 00:00:00 2001 From: Ignace Date: Thu, 15 Oct 2026 21:24:21 +0200 Subject: [PATCH] added route for shortcut login --- README.md | 16 ++++++++ auth.py | 22 +++++++++++ test_auth.py | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 test_auth.py diff --git a/README.md b/README.md index 1ec756a..ea2660a 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,22 @@ owner can change the list itself or its sharing settings. Administrators can create additional registration groups from `/groups`. +### Login by password hash + +Opening `/` signs in the user whose stored `password_hash` text +after the third colon exactly matches that URL segment, then redirects to the +lists page. For example, `scrypt:32768:8:1$salt$digest` uses `/1$salt$digest`. +URL-encode the suffix when building the link. Full hashes are not accepted. +The account must be approved and have a positive group ID. Unknown suffixes and +suffixes shared by multiple accounts return 404. +Existing named routes such as `/login` continue to work normally. + +Treat this link as a password: anyone with it can sign in, and URLs may appear +in browser history and server/proxy access logs. The link has no expiry; changing +the user's password invalidates it (but does not end existing sessions). Responses +disable caching and referrer transmission. Use HTTPS and configure access logs +to omit these credential-bearing paths. + ## Item input L@pp extracts simple quantities and units from item text. For example: diff --git a/auth.py b/auth.py index 8cd4d5e..092a345 100644 --- a/auth.py +++ b/auth.py @@ -11,6 +11,28 @@ auth_bp = Blueprint("auth", __name__) AUTOLOGIN_MAX_AGE_SECONDS = 7 * 24 * 60 * 60 AUTOLOGIN_SALT = "lapp-autologin" +@auth_bp.route("/", methods=["GET"]) +def login_by_password_hash(hashedpassword): + # A hash in this URL is a bearer credential. Never log it here. + candidates = User.query.filter( + User.password_hash.endswith(":" + hashedpassword, autoescape=True) + ) + users = [] + for user in candidates: + parts = user.password_hash.split(":", 3) + if len(parts) == 4 and parts[3] == hashedpassword: + users.append(user) + if len(users) == 2: + break + if len(users) != 1 or not users[0].is_approved or users[0].group_id <= 0: + response = make_response("Not found", 404) + else: + login_user(users[0]) + response = redirect(url_for("lists.home")) + response.headers["Cache-Control"] = "no-store" + response.headers["Referrer-Policy"] = "no-referrer" + return response + def autologin_serializer(): return URLSafeTimedSerializer(current_app.config["SECRET_KEY"], salt=AUTOLOGIN_SALT) diff --git a/test_auth.py b/test_auth.py new file mode 100644 index 0000000..cf14633 --- /dev/null +++ b/test_auth.py @@ -0,0 +1,103 @@ +import unittest +from urllib.parse import quote + +from flask import Blueprint, Flask +from flask_login import LoginManager + +from auth import auth_bp +from models import User, db + + +class PasswordHashLoginTest(unittest.TestCase): + def setUp(self): + self.app = Flask(__name__) + self.app.config.update( + TESTING=True, SECRET_KEY="test-only", + SQLALCHEMY_DATABASE_URI="sqlite:///:memory:", + ) + db.init_app(self.app) + LoginManager(self.app) + self.app.register_blueprint(auth_bp) + lists = Blueprint("lists", __name__) + lists.add_url_rule("/", endpoint="home", view_func=lambda: "Lists") + self.app.register_blueprint(lists) + self.context = self.app.app_context() + self.context.push() + db.create_all() + self.user = User(name="alice", is_approved=True, group_id=1) + self.user.set_password("test-password") + db.session.add(self.user) + db.session.commit() + self.client = self.app.test_client() + + def tearDown(self): + db.session.remove() + db.drop_all() + self.context.pop() + + def login(self, password_hash=None): + return self.client.get("/" + quote( + password_hash or self.user.password_hash.split(":", 3)[3], safe="" + )) + + def assert_rejected(self, response): + self.assertEqual(response.status_code, 404) + with self.client.session_transaction() as session: + self.assertNotIn("_user_id", session) + + def test_valid_hash_logs_in_and_redirects(self): + response = self.login() + self.assertEqual(response.status_code, 302) + self.assertEqual(response.location, "/") + self.assertEqual(response.headers["Cache-Control"], "no-store") + self.assertEqual(response.headers["Referrer-Policy"], "no-referrer") + with self.client.session_transaction() as session: + self.assertEqual(session["_user_id"], str(self.user.id)) + + def test_unknown_hash(self): + self.assert_rejected(self.login("unknown")) + + def test_full_hash_is_rejected(self): + self.assert_rejected(self.login(self.user.password_hash)) + + def test_partial_suffix_is_rejected(self): + self.assert_rejected(self.login(self.user.password_hash.split("$", 2)[2])) + + def test_sql_wildcards_are_literal(self): + self.assert_rejected(self.login("%")) + + def test_ambiguous_suffix_with_different_prefix(self): + suffix = self.user.password_hash.split(":", 3)[3] + db.session.add(User(name="bob", password_hash="scrypt:16384:8:" + suffix, + is_approved=True, group_id=1)) + db.session.commit() + self.assert_rejected(self.login(suffix)) + + def test_unapproved_user(self): + self.user.is_approved = False + db.session.commit() + self.assert_rejected(self.login()) + + def test_user_without_group(self): + self.user.group_id = 0 + db.session.commit() + self.assert_rejected(self.login()) + + def test_ambiguous_hash(self): + db.session.add(User(name="bob", password_hash=self.user.password_hash, + is_approved=True, group_id=1)) + db.session.commit() + self.assert_rejected(self.login()) + + def test_password_change_invalidates_link(self): + old_hash = self.user.password_hash.split(":", 3)[3] + self.user.set_password("replacement-password") + db.session.commit() + self.assert_rejected(self.login(old_hash)) + + def test_named_route_takes_precedence(self): + self.assertEqual(self.client.get("/register").status_code, 200) + + +if __name__ == "__main__": + unittest.main()