Until now an admin created an account, the password appeared once on screen,
and that was the only copy. Gitea's Forgot password answers "Account recovery
is disabled because no email is set up", because this server has never been
able to send mail. Every route a member had to a password of their own was
closed, which made the members area unusable by anyone not standing next to the
owner.
Now: the admin enters a username and an email, the server creates the Gitea
account with a random password nobody ever sees — the admin included — and the
member is emailed a link to a Viena Latina page where they choose their own.
The same mechanism gives "¿Olvidaste tu contraseña?" that works, replacing
Gitea's dead page. Nobody leaves the site for either, which is possible because
PATCH /admin/users/{username} accepts a password.
A link is enough to take an account, so it is treated as a credential: single
use, short-lived, stored only as a SHA-256, and invalidated when a newer one is
issued so an older email stops working. SHA-256 rather than a password hash
because these are 32 random bytes, not something a person chose — there is no
dictionary to slow down. Recovery answers identically for a known and an
unknown address, or the form becomes a way to enumerate members one address at
a time, and stops after three tries so it cannot be used to mail-bomb somebody
using this server's reputation.
Rejecting a password deliberately does not spend the token. A typo must not
lock somebody out of an account they have never reached.
If the mail fails the admin is shown the link instead. The account exists
either way, so the difference is between a delayed invitation and a person who
simply never gets in.
Found while testing: the rate limit did not work at all. created_at is written
by SQLite as "2026-09-25 15:00:00" and I compared it against Python's
"2026-09-25T15:00:00+00:00"; a space sorts before T, so every row looked older
than any threshold and the limit silently never fired. It is now compared
inside SQLite, where the format and the clock are the same one.
Also drops two tabs from the sign-in page: OpenID, which nobody here will use,
and Register, which contradicted DISABLE_REGISTRATION and invited people to try
something the server then refused.
Verified: 126 checks. Tokens are hashed at rest, work once, expire, die when
reissued, and are refused for a suspended member; a short password and a Gitea
rejection both leave the link usable; recovery is indistinguishable for known
and unknown addresses and stops at three; creating a member emails them and
shows no password; a failed send surfaces the link.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NizVpJ2dwzCbjCrTLCjeHn
95 lines
2.8 KiB
Python
95 lines
2.8 KiB
Python
"""Shared fixtures.
|
|
|
|
Tests drive the app through Flask's test client rather than poking functions
|
|
directly, because most of what is worth checking here is a decision about who
|
|
may do what — and that decision is only real once it has survived routing,
|
|
the session and the CSRF hook.
|
|
|
|
Sign-in is faked by writing the session cookie: the OAuth round trip belongs to
|
|
Gitea, and the tests that care about it stub the two network calls instead.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
|
|
|
|
from apps.board.app import create_app # noqa: E402
|
|
from apps.board.db import connect # noqa: E402
|
|
|
|
|
|
@pytest.fixture
|
|
def app(tmp_path):
|
|
application = create_app({
|
|
"SECRET_KEY": "test-secret",
|
|
"DB_PATH": str(tmp_path / "board.db"),
|
|
"OWNER_LOGIN": "owner",
|
|
"SESSION_COOKIE_SECURE": False,
|
|
"COOLDOWN_SECONDS": 0,
|
|
"BASE_URL": "http://localhost",
|
|
"OAUTH_CLIENT_ID": "cid",
|
|
"OAUTH_CLIENT_SECRET": "secret",
|
|
"ADMIN_TOKEN": "admintoken",
|
|
"TESTING": True,
|
|
})
|
|
yield application
|
|
|
|
|
|
@pytest.fixture
|
|
def db(app):
|
|
conn = connect(app.config["DB_PATH"])
|
|
yield conn
|
|
conn.close()
|
|
|
|
|
|
@pytest.fixture
|
|
def client(app):
|
|
return app.test_client()
|
|
|
|
|
|
@pytest.fixture
|
|
def make_member(db):
|
|
"""Insert a member and return its id. The owner already exists, seeded."""
|
|
def _make(login, role="user", active=1):
|
|
cursor = db.execute(
|
|
"""INSERT INTO members (gitea_login, display_name, email, role, active)
|
|
VALUES (?, ?, ?, ?, ?)""",
|
|
(login, login.title(), f"{login}@example.com", role, active),
|
|
)
|
|
return cursor.lastrowid
|
|
return _make
|
|
|
|
|
|
@pytest.fixture
|
|
def owner_id(db):
|
|
return db.execute("SELECT id FROM members WHERE role = 'owner'").fetchone()["id"]
|
|
|
|
|
|
@pytest.fixture
|
|
def sign_in(client):
|
|
def _sign_in(member_id):
|
|
with client.session_transaction() as session:
|
|
session["member_id"] = member_id
|
|
session["csrf"] = "token-for-tests"
|
|
return _sign_in
|
|
|
|
|
|
@pytest.fixture
|
|
def post(client):
|
|
"""POST with a valid CSRF token, so tests exercise authorisation rather
|
|
than repeatedly rediscovering that the CSRF hook works."""
|
|
def _post(url, data=None, **kwargs):
|
|
# A real anonymous visitor gets a CSRF token when the form renders, so
|
|
# signed-out pages (invitations, password recovery) need one here too —
|
|
# otherwise every such test fails on the hook rather than on its subject.
|
|
with client.session_transaction() as session:
|
|
session.setdefault("csrf", "token-for-tests")
|
|
payload = dict(data or {})
|
|
payload.setdefault("csrf_token", "token-for-tests")
|
|
return client.post(url, data=payload, **kwargs)
|
|
return _post
|