"""Argon2id password hashing and TOTP (REVIEW SEC-009, TODO 3.0). No database needed: these are pure functions. """ from __future__ import annotations import pytest from poorbird.passwords import ( MIN_PASSWORD_LENGTH, WeakPassword, hash_password, needs_rehash, validate_password_strength, verify_password, ) from poorbird.totp import DIGITS, new_secret, provisioning_uri, verify GOOD = "a-perfectly-fine-passphrase" # --- hashing ---------------------------------------------------------------- def test_a_password_verifies_against_its_own_hash(): assert verify_password(GOOD, hash_password(GOOD)) is True def test_a_wrong_password_does_not_verify(): assert verify_password("something else entirely", hash_password(GOOD)) is False def test_the_hash_is_argon2id_and_not_something_faster(): """SPEC 3.1 says Argon2id specifically. A SHA-256 here would pass every other test in this file while leaving the passwords brute-forceable, so the algorithm is asserted directly. """ assert hash_password(GOOD).startswith("$argon2id$") def test_the_same_password_hashes_differently_every_time(): """Per-hash salt. Equal hashes would mean equal passwords are visible.""" assert hash_password(GOOD) != hash_password(GOOD) def test_the_plaintext_never_appears_in_the_hash(): assert GOOD not in hash_password(GOOD) @pytest.mark.parametrize("stored", [None, "", "not-a-hash", "$argon2id$truncated"]) def test_an_unusable_stored_hash_is_a_no_not_a_crash(stored): """A user row that has never had a password set must not be loggable-in. It must also not take the login route down: NULL is the Phase 1 state of every seeded row, and a 500 on the login endpoint is worse than a 401. """ assert verify_password(GOOD, stored) is False def test_a_current_hash_does_not_need_rehashing(): assert needs_rehash(hash_password(GOOD)) is False def test_a_weaker_hash_is_flagged_for_rehash(): """Login is the only moment the plaintext exists, so it is the only moment a hash can be upgraded. This is what makes raising the cost possible.""" from argon2 import PasswordHasher weak = PasswordHasher(time_cost=1, memory_cost=8, parallelism=1).hash(GOOD) assert needs_rehash(weak) is True # ...and it must still verify, or raising the cost would lock everyone out. assert verify_password(GOOD, weak) is True # --- strength floor --------------------------------------------------------- def test_a_long_enough_password_is_accepted(): validate_password_strength("x" * MIN_PASSWORD_LENGTH) def test_a_short_password_is_rejected(): with pytest.raises(WeakPassword, match="at least"): validate_password_strength("x" * (MIN_PASSWORD_LENGTH - 1)) def test_whitespace_is_not_a_password(): with pytest.raises(WeakPassword, match="whitespace"): validate_password_strength(" " * (MIN_PASSWORD_LENGTH + 4)) def test_an_absurdly_long_password_is_rejected_rather_than_hashed(): """Argon2 is deliberately expensive; an unbounded body should not become a way to spend the server's CPU.""" with pytest.raises(WeakPassword, match="at most"): validate_password_strength("x" * 5000) # --- totp ------------------------------------------------------------------- def test_a_generated_code_verifies(): import pyotp secret = new_secret() assert verify(secret, pyotp.TOTP(secret).now()) is True def test_a_code_for_a_different_secret_does_not_verify(): import pyotp assert verify(new_secret(), pyotp.TOTP(new_secret()).now()) is False @pytest.mark.parametrize("code", [None, "", "abcdef", "12345", "1234567", "12 34 56 78"]) def test_a_malformed_code_is_rejected_without_raising(code): assert verify(new_secret(), code) is False def test_a_user_without_a_secret_can_never_pass_totp(): """The check has to fail closed. If a missing secret verified, enabling 2FA for one user would effectively disable it for everyone else.""" import pyotp assert verify(None, pyotp.TOTP(new_secret()).now()) is False def test_spaces_in_a_code_are_tolerated(): """Authenticator apps display "123 456"; people paste what they see.""" import pyotp secret = new_secret() code = pyotp.TOTP(secret).now() assert verify(secret, f"{code[:3]} {code[3:]}") is True def test_the_provisioning_uri_carries_the_issuer_and_account(): uri = provisioning_uri(new_secret(), "alice") assert uri.startswith("otpauth://totp/") assert "issuer=poorBird" in uri assert "alice" in uri def test_codes_are_six_digits(): """Changing this silently would break every already-enrolled authenticator.""" assert DIGITS == 6