s365b.auth.passwords

Password and recovery-code hashing (Argon2id) and password policy.

Attributes

Exceptions

PasswordPolicyError

Raised when a password does not meet the policy.

Functions

check_policy(password, username) → None

Check a new password against the policy.

hash_password(password) → str

Hash a password or recovery code.

verify_password(stored_hash, password) → bool

Check a password against a stored hash in constant-ish time.

generate_recovery_codes() → tuple[list[str], list[str]]

Create one-time recovery codes for a break-glass account.

use_recovery_code(hashes, code) → list[str] | None

Check a recovery code and consume it.

hash_password_async(password) → str

hash_password() in a worker thread (see HASH_SLOTS).

verify_password_async(stored_hash, password) → bool

verify_password() in a worker thread (see HASH_SLOTS).

generate_recovery_codes_async() → tuple[list[str], list[str]]

generate_recovery_codes() in a worker thread.

use_recovery_code_async(hashes, code) → list[str] | None

use_recovery_code() in a worker thread.

Module Contents

s365b.auth.passwords.MIN_PASSWORD_LENGTH = 14[source]
s365b.auth.passwords.RECOVERY_CODE_COUNT = 10[source]
exception s365b.auth.passwords.PasswordPolicyError[source]

Bases: ValueError

Raised when a password does not meet the policy.

s365b.auth.passwords.check_policy(password: str, username: str) → None[source]

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.

Parameters:
  • password – Proposed password.

  • username – The account’s username.

Raises:

PasswordPolicyError – With a message suitable for the user.

s365b.auth.passwords.hash_password(password: str) → str[source]

Hash a password or recovery code.

Parameters:

password – Plaintext.

Returns:

Argon2id hash string.

s365b.auth.passwords.verify_password(stored_hash: str | None, password: str) → bool[source]

Check a password against a stored hash in constant-ish time.

Parameters:
  • stored_hash – Hash from the database; None if the user has none (a dummy hash is checked so timing does not differ).

  • password – Plaintext to check.

Returns:

True if it matches.

s365b.auth.passwords.generate_recovery_codes() → tuple[list[str], list[str]][source]

Create one-time recovery codes for a break-glass account.

Returns:

codes to show the user once, and hashes to store.

Return type:

(codes, hashes)

s365b.auth.passwords.use_recovery_code(hashes: list[str], code: str) → list[str] | None[source]

Check a recovery code and consume it.

Parameters:
  • hashes – Stored hashes of unused codes.

  • code – Code entered by the user.

Returns:

The remaining hashes if the code was valid, else None.

s365b.auth.passwords.HASH_SLOTS = 2[source]
async s365b.auth.passwords.hash_password_async(password: str) → str[source]

hash_password() in a worker thread (see HASH_SLOTS).

Parameters:

password – Plaintext.

Returns:

Argon2id hash string.

async s365b.auth.passwords.verify_password_async(stored_hash: str | None, password: str) → bool[source]

verify_password() in a worker thread (see HASH_SLOTS).

Parameters:
  • stored_hash – Hash from the database, or None.

  • password – Plaintext to check.

Returns:

True if it matches.

async s365b.auth.passwords.generate_recovery_codes_async() → tuple[list[str], list[str]][source]

generate_recovery_codes() in a worker thread.

Returns:

(codes, hashes).

async s365b.auth.passwords.use_recovery_code_async(hashes: list[str], code: str) → list[str] | None[source]

use_recovery_code() in a worker thread.

Parameters:
  • hashes – Stored hashes of unused codes.

  • code – Code entered by the user.

Returns:

The remaining hashes if the code was valid, else None.