Skip to content

fediverse_pasture.server.utils

actor_object_to_public_key(actor, key_id)

As public_key_owner_from_dict, but applies context normalization first, then checks without out.

Source code in fediverse_pasture/server/utils.py
def actor_object_to_public_key(
    actor: dict, key_id: str
) -> Tuple[str | None, str | None]:
    """As [public_key_owner_from_dict][fediverse_pasture.server.utils.public_key_owner_from_dict], but applies context normalization first, then checks without out."""
    public_key, owner = public_key_owner_from_dict(with_external_context(actor), key_id)

    if public_key and owner:
        return public_key, owner

    return public_key_owner_from_dict(actor, key_id)

find_with_item(dict_list, key_id)

Given a list of dictionaries, finds the dictionary with id = key_id

Source code in fediverse_pasture/server/utils.py
def find_with_item(dict_list, key_id):
    """Given a list of dictionaries, finds the dictionary with
    id = key_id"""
    for key in dict_list:
        if key.get("id") == key_id:
            return key
    return None

public_key_owner_from_dict(actor, key_id)

Given an actor and key_id returns the public_key and the owner. This method directly checks the key publicKey

Source code in fediverse_pasture/server/utils.py
def public_key_owner_from_dict(
    actor: dict, key_id: str
) -> Tuple[str | None, str | None]:
    """Given an actor and key_id returns the public_key and the owner. This method directly checks the key `publicKey`"""

    public_key_data = actor.get("publicKey", {})

    if isinstance(public_key_data, list):
        if len(public_key_data) == 1:
            public_key_data = public_key_data[0]
        else:
            public_key_data = find_with_item(public_key_data, key_id)

    if not public_key_data:
        return None, None

    public_key = public_key_data.get("publicKeyPem")
    owner = public_key_data.get("owner")

    return public_key, owner