Serialization
serialization ¶
Serialization layer for encrypting session state and event data.
Converts Python dictionaries and JSON strings into self-describing encrypted envelopes using any EncryptionBackend-conformant backend. Each envelope carries a version byte and backend identifier prefix to support future backend migrations. The BACKEND_REGISTRY maps recognized backend IDs to human-readable names and is used by _parse_envelope() for validation. Registered backends: Fernet (0x01) and AES-GCM (0x02).
The layer is stateless — four async module-level functions, no classes. The encryption backend is passed per call.
Examples:
Encrypt and decrypt a session state dictionary:
from adk_secure_sessions.serialization import (
encrypt_session,
decrypt_session,
BACKEND_FERNET,
)
envelope = await encrypt_session(state, backend, BACKEND_FERNET)
restored = await decrypt_session(envelope, backend)
See Also
adk_secure_sessions.protocols: Encryption backend protocol that backends must conform to.
ENVELOPE_VERSION_1 module-attribute ¶
Current envelope format version byte.
BACKEND_AES_GCM module-attribute ¶
Backend identifier for AES-256-GCM encryption.
BACKEND_REGISTRY module-attribute ¶
BACKEND_REGISTRY: dict[int, str] = {
BACKEND_FERNET: "Fernet",
BACKEND_AES_GCM: "AES-GCM",
}
Mapping of supported backend IDs to human-readable names.
Used by _parse_envelope() to validate incoming envelopes and generate error messages for unrecognized backends.
encrypt_session async ¶
encrypt_session(
data: dict[str, Any],
backend: EncryptionBackend,
backend_id: int,
) -> bytes
Serialize a session state dict to an encrypted envelope.
| PARAMETER | DESCRIPTION |
|---|---|
data | JSON-serializable Python dictionary. TYPE: |
backend | Any TYPE: |
backend_id | Integer identifying the backend. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
bytes | Encrypted envelope bytes: |
| RAISES | DESCRIPTION |
|---|---|
SerializationError | If data cannot be serialized to JSON. |
Examples:
Source code in src/adk_secure_sessions/serialization.py
decrypt_session async ¶
decrypt_session(
envelope: bytes, backend: EncryptionBackend
) -> dict[str, Any]
Decrypt an encrypted envelope back to a session state dict.
| PARAMETER | DESCRIPTION |
|---|---|
envelope | Encrypted envelope bytes (>= 3 bytes). TYPE: |
backend | Any TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | Original Python dictionary. |
| RAISES | DESCRIPTION |
|---|---|
DecryptionError | If envelope is invalid, tampered, or backend fails. |
SerializationError | If decrypted bytes are not valid JSON. |
Examples:
Source code in src/adk_secure_sessions/serialization.py
encrypt_json async ¶
encrypt_json(
json_str: str,
backend: EncryptionBackend,
backend_id: int,
) -> bytes
Encrypt a pre-serialized JSON string into an encrypted envelope.
| PARAMETER | DESCRIPTION |
|---|---|
json_str | Valid JSON string (e.g., from TYPE: |
backend | Any TYPE: |
backend_id | Integer identifying the backend. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
bytes | Encrypted envelope bytes: |
Examples:
Source code in src/adk_secure_sessions/serialization.py
decrypt_json async ¶
decrypt_json(
envelope: bytes, backend: EncryptionBackend
) -> str
Decrypt an encrypted envelope back to a JSON string.
| PARAMETER | DESCRIPTION |
|---|---|
envelope | Encrypted envelope bytes (>= 3 bytes). TYPE: |
backend | Any TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | Original JSON string. |
| RAISES | DESCRIPTION |
|---|---|
DecryptionError | If envelope is invalid, tampered, backend fails, or decrypted bytes are not valid UTF-8. |
Examples: