fleche.security
Attributes
Exceptions
Exception raised when signature verification fails. |
Classes
Helper class to sign and verify serialized data using HMAC-SHA256. |
Functions
|
Retrieve the secret key(s) for signing cache entries. |
Module Contents
- exception fleche.security.SignatureError[source]
Bases:
ExceptionException raised when signature verification fails.
- fleche.security.get_secret_key() list[bytes][source]
Retrieve the secret key(s) for signing cache entries.
Only supports FLECHE_SECRET_KEY environment variable. If multiple keys are present, they should be colon-separated. Each key must be a hex-encoded string which is decoded to bytes. If no key is found, returns an empty list (security is disabled).
- Returns:
A list of secret keys as bytes.
- Return type:
list[bytes]
- class fleche.security.SignedBytes[source]
Helper class to sign and verify serialized data using HMAC-SHA256. Allows for key rotation by accepting a list of keys.
- Parameters:
keys (list[bytes]) – A list of secret keys. The first key is used for signing, and all keys are attempted during verification.
- _sign(data: bytes, key: bytes) bytes[source]
Generate HMAC-SHA256 hex signature for data using the specified key. Hex encoding ensures the signature string (0-9a-f) never contains the pickle STOP opcode byte (ASCII 46, .).
- Parameters:
data (bytes) – The data to sign.
key (bytes) – The secret key to use for signing.
- Returns:
The resulting 64-byte hex-encoded HMAC signature.
- Return type:
bytes
- dumps(content: bytes) bytes[source]
Signs the content using the first key in the list and appends the hex signature. If no keys are provided, returns the content unmodified.
- Parameters:
content (bytes) – The serialized data to sign.
- Returns:
The original data with the 64-byte hex signature appended (if keys exist).
- Return type:
bytes
- loads(content: bytes) bytes[source]
Verifies the signature of the content. Extracts the signature by searching for the pickle STOP opcode. Iterates through all provided keys for verification. Returns the original content if verification passes.
- Parameters:
content (bytes) – The payload containing the data and the appended signature.
- Returns:
The original serialized data, stripped of the signature.
- Return type:
bytes
- Raises:
SignatureError – If verification fails, if the data is corrupted, or if the STOP opcode is missing.