|
| 1 | +""" |
| 2 | +Password hashing helpers for any repository-managed credential flow. |
| 3 | +
|
| 4 | +The public app currently delegates user authentication to Supabase Auth. If a |
| 5 | +backend/admin flow ever stores local passwords, it must use these helpers |
| 6 | +rather than fast hashes such as MD5 or SHA variants. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import re |
| 12 | + |
| 13 | +import bcrypt |
| 14 | + |
| 15 | +BCRYPT_COST_FACTOR = 12 |
| 16 | +_BCRYPT_HASH_PATTERN = re.compile(r"^\$2[aby]\$(\d{2})\$[./A-Za-z0-9]{53}$") |
| 17 | + |
| 18 | + |
| 19 | +def _to_password_bytes(password: str) -> bytes: |
| 20 | + if not isinstance(password, str) or not password: |
| 21 | + raise ValueError("Password must be a non-empty string.") |
| 22 | + return password.encode("utf-8") |
| 23 | + |
| 24 | + |
| 25 | +def hash_password( |
| 26 | + password: str, |
| 27 | + *, |
| 28 | + cost_factor: int = BCRYPT_COST_FACTOR, |
| 29 | +) -> str: |
| 30 | + """ |
| 31 | + Hash a plaintext password with bcrypt. |
| 32 | +
|
| 33 | + bcrypt cost 12 is the project baseline. Higher values may be passed by |
| 34 | + callers after performance testing, but lower values are rejected. |
| 35 | + """ |
| 36 | + |
| 37 | + if cost_factor < BCRYPT_COST_FACTOR: |
| 38 | + raise ValueError( |
| 39 | + f"bcrypt cost factor must be at least {BCRYPT_COST_FACTOR}." |
| 40 | + ) |
| 41 | + |
| 42 | + password_bytes = _to_password_bytes(password) |
| 43 | + return bcrypt.hashpw( |
| 44 | + password_bytes, |
| 45 | + bcrypt.gensalt(rounds=cost_factor), |
| 46 | + ).decode("ascii") |
| 47 | + |
| 48 | + |
| 49 | +def verify_password(password: str, password_hash: str) -> bool: |
| 50 | + """ |
| 51 | + Verify a plaintext password against a bcrypt hash. |
| 52 | +
|
| 53 | + Malformed hashes, legacy MD5/SHA digests, and empty inputs fail closed. |
| 54 | + """ |
| 55 | + |
| 56 | + if not isinstance(password_hash, str) or not is_bcrypt_hash(password_hash): |
| 57 | + return False |
| 58 | + |
| 59 | + try: |
| 60 | + return bcrypt.checkpw( |
| 61 | + _to_password_bytes(password), |
| 62 | + password_hash.encode("ascii"), |
| 63 | + ) |
| 64 | + except (TypeError, ValueError): |
| 65 | + return False |
| 66 | + |
| 67 | + |
| 68 | +def is_bcrypt_hash(password_hash: str) -> bool: |
| 69 | + """Return whether a value has bcrypt's expected modular crypt format.""" |
| 70 | + |
| 71 | + return isinstance(password_hash, str) and bool( |
| 72 | + _BCRYPT_HASH_PATTERN.fullmatch(password_hash) |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +def password_hash_needs_rehash( |
| 77 | + password_hash: str, |
| 78 | + *, |
| 79 | + cost_factor: int = BCRYPT_COST_FACTOR, |
| 80 | +) -> bool: |
| 81 | + """Return true when a bcrypt hash is missing or uses a weaker cost.""" |
| 82 | + |
| 83 | + match = _BCRYPT_HASH_PATTERN.fullmatch(password_hash or "") |
| 84 | + if not match: |
| 85 | + return True |
| 86 | + |
| 87 | + return int(match.group(1)) < cost_factor |
0 commit comments