loom.core.identity.wire

Serialisation of the caller identity for job envelopes.

A job crosses a process boundary, where no context variable follows it. The identity therefore travels inside the envelope, as an explicit part of the wire contract, encoded with plain JSON types so any broker serializer can carry it.

Decoding treats its input as untrusted: anything that cannot be read as an identity becomes None, and a use case declaring Caller() then fails closed instead of running as a partially decoded caller.

Functions

_decode_attributes(value)

Keep the string-valued attributes, dropping anything else.

_decode_roles(value)

Keep the non-empty string roles, dropping anything else.

decode_identity(payload)

Rebuild the caller from a job envelope, refusing anything unreadable.

encode_identity(identity)

Encode identity for transport inside a job envelope.

loom.core.identity.wire.encode_identity(identity)[source]

Encode identity for transport inside a job envelope.

Parameters:

identity (Identity | None) – Caller to propagate, or None.

Returns:

A JSON-serialisable mapping, or None when there is no authenticated caller to propagate.

Return type:

dict[str, Any] | None

Example:

envelope = {"payload": payload, "identity": encode_identity(caller)}
loom.core.identity.wire.decode_identity(payload)[source]

Rebuild the caller from a job envelope, refusing anything unreadable.

Parameters:

payload (Any) – Value found under the envelope’s identity key. Any type is accepted because the envelope may predate this contract or come from a tampered broker message.

Returns:

The decoded identity, or None when the envelope carries none or carries something that is not one.

Return type:

Identity | None