security
azure_bootstrap.security
¶
Constant-time comparison + FastAPI API-key helper.
The :func:compare_secrets helper centralizes the None/empty/bytes-coercion
dance so call sites can just call it without re-implementing the safe pattern.
The FastAPI API-key helper is fail-open-when-unset by default (matches the
v1 reference behavior); apps that want strict mode pass
fail_open_when_unset=False.
Functions:
| Name | Description |
|---|---|
compare_secrets |
Constant-time equality. Returns False on any None / empty input. |
verify_api_key_header |
FastAPI dependency. Raises |
compare_secrets
¶
Constant-time equality. Returns False on any None / empty input.
Coerces str to bytes via UTF-8. Bytes inputs pass through unchanged.
Source code in azure_bootstrap/security/__init__.py
verify_api_key_header
async
¶
verify_api_key_header(x_api_key: str | None, *, env_var: str = 'API_KEY', fail_open_when_unset: bool = True) -> None
FastAPI dependency. Raises HTTPException(401) on mismatch.
When fail_open_when_unset is True (default) and the env var is unset
or empty, the check passes — matches the v1 reference behavior. Strict
mode (env required) is opt-in via fail_open_when_unset=False.
Imports FastAPI lazily so this module is importable without the fastapi
extra; only callers that actually invoke the function pay the dep.