Účty, schvalování žáků v /admin a obnova hesla přes SMTP (#11)
Implementuje účty, role, schvalování žáků v `/admin` a obnovu hesla přes SMTP podle OpenSpec změny `ucty-schvalovani-reset-hesla`. - Registrace žáků (`/registrace`), přihlášení (`/prihlaseni`), odhlášení (`/odhlaseni`), profil (`/profil`) a chráněný třídní pohled (`/trida`). - Hashování hesel (`scrypt`), podepsaná session cookie (`HttpOnly`, `SameSite=Lax`, za HTTPS `Secure`), okamžité zneplatnění session při změně hesla či zablokování účtu, globální CSRF kontrola, in-memory rate limiting a volitelný `ProxyFix`. - Atomická inicializace prvního účtu učitelky z `POZNAVACKY_ADMIN_EMAIL` / `POZNAVACKY_ADMIN_PASSWORD` při startu nad prázdnou tabulkou `uzivatele`. - Administrace `/admin`, `/admin/tridy` a `/admin/zaci` (správa tříd, fronta schvalování, schválení, zamítnutí, přeřazení, deaktivace/aktivace s ochranou účtů učitelky). - Obnova zapomenutého hesla (`/obnova-hesla`, `/obnova-hesla/<token>`) s jednorázovým SHA-256 hashovaným tokenem (migrace `0002_token_hash_index.sql`), anti-enumerací a SMTP odesílačem (STARTTLS / `SMTP_SSL`). - Synchronizovány specifikace `user-auth`, `student-approval`, `password-reset` do `openspec/specs/` a změna archivována. - Ověřeno offline: `.venv/bin/pytest -W error` (52 passed) a `.venv/bin/ruff check .`. Closes #2 Reviewed-on: #11 Co-authored-by: David Spáčil <spacildavi@gmail.com>
This commit was merged in pull request #11.
This commit is contained in:
1 parent
c413e884d0
commit
5fcfe72272
36 files changed
+4921
-3
No files matched your search
@@ -0,0 +1,148 @@
|
||||
"""Jednotkové testy bezpečnostních primitiv v poznavacky.security."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
|
||||
from poznavacky.security import (
|
||||
CSRF_SESSION_KEY,
|
||||
RateLimiter,
|
||||
compute_auth_version,
|
||||
display_name,
|
||||
get_or_create_csrf_token,
|
||||
hash_password,
|
||||
normalize_email,
|
||||
rotate_csrf_token,
|
||||
validate_csrf_token,
|
||||
verify_auth_version,
|
||||
verify_password,
|
||||
)
|
||||
|
||||
|
||||
def test_scrypt_password_hashing_and_verification() -> None:
|
||||
hashed = hash_password("bezpecne-heslo-123")
|
||||
assert hashed.startswith("scrypt:")
|
||||
assert "bezpecne-heslo-123" not in hashed
|
||||
assert verify_password(hashed, "bezpecne-heslo-123") is True
|
||||
assert verify_password(hashed, "spatne-heslo") is False
|
||||
assert verify_password("", "bezpecne-heslo-123") is False
|
||||
|
||||
|
||||
def test_auth_version_changes_with_password_hash() -> None:
|
||||
hash_a = hash_password("prvni-heslo-123")
|
||||
hash_b = hash_password("druhe-heslo-456")
|
||||
|
||||
ver_a = compute_auth_version("tajny-klic", hash_a)
|
||||
assert len(ver_a) == 32
|
||||
assert verify_auth_version("tajny-klic", hash_a, ver_a) is True
|
||||
assert verify_auth_version("tajny-klic", hash_b, ver_a) is False
|
||||
assert verify_auth_version("jiny-klic", hash_a, ver_a) is False
|
||||
assert verify_auth_version("tajny-klic", hash_a, None) is False
|
||||
|
||||
|
||||
def test_csrf_token_lifecycle_and_validation() -> None:
|
||||
store: dict[str, str] = {}
|
||||
token = get_or_create_csrf_token(store)
|
||||
assert store[CSRF_SESSION_KEY] == token
|
||||
assert get_or_create_csrf_token(store) == token
|
||||
|
||||
assert validate_csrf_token(store, token) is True
|
||||
assert validate_csrf_token(store, "spatny-token") is False
|
||||
assert validate_csrf_token(store, None) is False
|
||||
assert validate_csrf_token({}, token) is False
|
||||
|
||||
rotated = rotate_csrf_token(store)
|
||||
assert rotated != token
|
||||
assert validate_csrf_token(store, token) is False
|
||||
assert validate_csrf_token(store, rotated) is True
|
||||
|
||||
|
||||
def test_rate_limiter_sliding_window_and_retry_after() -> None:
|
||||
now = [100.0]
|
||||
limiter = RateLimiter(clock=lambda: now[0])
|
||||
|
||||
for _ in range(3):
|
||||
decision = limiter.check_and_record(
|
||||
"login", "192.0.2.10", limit=3, window_seconds=60
|
||||
)
|
||||
assert decision.allowed is True
|
||||
assert decision.retry_after == 0
|
||||
|
||||
blocked = limiter.check_and_record(
|
||||
"login", "192.0.2.10", limit=3, window_seconds=60
|
||||
)
|
||||
assert blocked.allowed is False
|
||||
assert blocked.retry_after == 60
|
||||
|
||||
# Jiná IP adresa a jiný endpoint zůstávají nezávislé.
|
||||
assert (
|
||||
limiter.check_and_record(
|
||||
"login", "192.0.2.11", limit=3, window_seconds=60
|
||||
).allowed
|
||||
is True
|
||||
)
|
||||
assert (
|
||||
limiter.check_and_record(
|
||||
"register", "192.0.2.10", limit=3, window_seconds=60
|
||||
).allowed
|
||||
is True
|
||||
)
|
||||
|
||||
# Po posunu o 30 s stále blokováno, retry_after klesne na 30.
|
||||
now[0] = 130.0
|
||||
still_blocked = limiter.check_and_record(
|
||||
"login", "192.0.2.10", limit=3, window_seconds=60
|
||||
)
|
||||
assert still_blocked.allowed is False
|
||||
assert still_blocked.retry_after == 30
|
||||
|
||||
# Po uplynutí 60 s je pokus opět povolen.
|
||||
now[0] = 160.0
|
||||
unblocked = limiter.check_and_record(
|
||||
"login", "192.0.2.10", limit=3, window_seconds=60
|
||||
)
|
||||
assert unblocked.allowed is True
|
||||
|
||||
|
||||
def test_rate_limiter_thread_safety() -> None:
|
||||
limiter = RateLimiter(clock=lambda: 500.0)
|
||||
results: list[bool] = []
|
||||
lock = threading.Lock()
|
||||
|
||||
def worker() -> None:
|
||||
decision = limiter.check_and_record(
|
||||
"login", "192.0.2.99", limit=5, window_seconds=60
|
||||
)
|
||||
with lock:
|
||||
results.append(decision.allowed)
|
||||
|
||||
threads = [threading.Thread(target=worker) for _ in range(20)]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
for thread in threads:
|
||||
thread.join()
|
||||
|
||||
assert results.count(True) == 5
|
||||
assert results.count(False) == 15
|
||||
|
||||
|
||||
def test_display_name_and_email_normalization() -> None:
|
||||
assert (
|
||||
display_name(
|
||||
{"jmeno": "Petr", "prijmeni": "Ukazkovy", "prezdivka": "Sokolik"}
|
||||
)
|
||||
== "Sokolik"
|
||||
)
|
||||
assert (
|
||||
display_name(
|
||||
{"jmeno": "Anna", "prijmeni": "Novotna", "prezdivka": None}
|
||||
)
|
||||
== "Anna N."
|
||||
)
|
||||
assert (
|
||||
display_name(
|
||||
{"jmeno": " Anna ", "prijmeni": " Novotna ", "prezdivka": " "}
|
||||
)
|
||||
== "Anna N."
|
||||
)
|
||||
assert normalize_email(" Zak@Example.Test ") == "zak@example.test"
|
||||
Reference in new issue
Block a user