s365b.auth.passwords ==================== .. py:module:: s365b.auth.passwords .. autoapi-nested-parse:: Password and recovery-code hashing (Argon2id) and password policy. Attributes ---------- .. autoapisummary:: s365b.auth.passwords.MIN_PASSWORD_LENGTH s365b.auth.passwords.RECOVERY_CODE_COUNT s365b.auth.passwords.HASH_SLOTS Exceptions ---------- .. autoapisummary:: s365b.auth.passwords.PasswordPolicyError Functions --------- .. autoapisummary:: s365b.auth.passwords.check_policy s365b.auth.passwords.hash_password s365b.auth.passwords.verify_password s365b.auth.passwords.generate_recovery_codes s365b.auth.passwords.use_recovery_code s365b.auth.passwords.hash_password_async s365b.auth.passwords.verify_password_async s365b.auth.passwords.generate_recovery_codes_async s365b.auth.passwords.use_recovery_code_async Module Contents --------------- .. py:data:: MIN_PASSWORD_LENGTH :value: 14 .. py:data:: RECOVERY_CODE_COUNT :value: 10 .. py:exception:: PasswordPolicyError Bases: :py:obj:`ValueError` Raised when a password does not meet the policy. .. py:function:: check_policy(password: str, username: str) -> None Check a new password against the policy. Policy: at least 14 characters, not containing the username. Length is what matters for an account that is used rarely and written down in a safe; no composition rules. :param password: Proposed password. :param username: The account's username. :raises PasswordPolicyError: With a message suitable for the user. .. py:function:: hash_password(password: str) -> str Hash a password or recovery code. :param password: Plaintext. :returns: Argon2id hash string. .. py:function:: verify_password(stored_hash: str | None, password: str) -> bool Check a password against a stored hash in constant-ish time. :param stored_hash: Hash from the database; ``None`` if the user has none (a dummy hash is checked so timing does not differ). :param password: Plaintext to check. :returns: True if it matches. .. py:function:: generate_recovery_codes() -> tuple[list[str], list[str]] Create one-time recovery codes for a break-glass account. :returns: codes to show the user once, and hashes to store. :rtype: ``(codes, hashes)`` .. py:function:: use_recovery_code(hashes: list[str], code: str) -> list[str] | None Check a recovery code and consume it. :param hashes: Stored hashes of unused codes. :param code: Code entered by the user. :returns: The remaining hashes if the code was valid, else ``None``. .. py:data:: HASH_SLOTS :value: 2 .. py:function:: hash_password_async(password: str) -> str :async: :func:`hash_password` in a worker thread (see :data:`HASH_SLOTS`). :param password: Plaintext. :returns: Argon2id hash string. .. py:function:: verify_password_async(stored_hash: str | None, password: str) -> bool :async: :func:`verify_password` in a worker thread (see :data:`HASH_SLOTS`). :param stored_hash: Hash from the database, or ``None``. :param password: Plaintext to check. :returns: True if it matches. .. py:function:: generate_recovery_codes_async() -> tuple[list[str], list[str]] :async: :func:`generate_recovery_codes` in a worker thread. :returns: ``(codes, hashes)``. .. py:function:: use_recovery_code_async(hashes: list[str], code: str) -> list[str] | None :async: :func:`use_recovery_code` in a worker thread. :param hashes: Stored hashes of unused codes. :param code: Code entered by the user. :returns: The remaining hashes if the code was valid, else ``None``.