Skip to content

fediverse_pasture.data_provider

DataProvider dataclass

Provides the data about actors to various applications

Parameters:

Name Type Description Default
one_actor ActorData

User in one actor and as an application actor

required
possible_actors List[ActorData]

The list of actors used for verify_actor

required
Source code in fediverse_pasture/data_provider/__init__.py
@dataclass
class DataProvider:
    """Provides the data about actors to various applications"""

    one_actor: ActorData = field(
        metadata={"description": "User in one actor and as an application actor"}
    )
    possible_actors: List[ActorData] = field(
        metadata={"description": "The list of actors used for verify_actor"}
    )

    def save(self):
        data = {
            "one_actor": self.one_actor.model_dump(exclude_none=True),
            "possible_actors": [
                x.model_dump(exclude_none=True) for x in self.possible_actors
            ],
        }

        with open("data.toml", "wb") as fp:
            tomli_w.dump(data, fp)

    @staticmethod
    def load():
        with open("data.toml", "rb") as fp:
            data = tomllib.load(fp)

        one_actor = ActorData(**(data["one_actor"]))
        possible_actors = [ActorData(**x) for x in data["possible_actors"]]

        return DataProvider(one_actor=one_actor, possible_actors=possible_actors)

    @staticmethod
    def generate(with_possible_actors=True):
        one_actor = generate_one_actor()
        if with_possible_actors:
            possible_actors = generate_possible_actors()
        else:
            possible_actors = []
        return DataProvider(one_actor=one_actor, possible_actors=possible_actors)

    @staticmethod
    def generate_and_load(only_generate_config):
        if only_generate_config:
            dp = DataProvider.generate()
            dp.save()
            exit(0)

        try:
            dp = DataProvider.load()
        except Exception:
            dp = DataProvider.generate()
            dp.save()

        return dp

bovine_actor_for_actor_data(actor_id, data)

Builds the corresponding bovine actor and actor data

Parameters:

Name Type Description Default
actor_id str

The actor id to be used, e.g. https://pasture_one_actor/actor

required
data ActorData

The actor data

required

Returns:

Type Description
Tuple[BovineActor, Actor]

A tuple, where the first object is the Actor performing actions, and the second one the data object.

Source code in fediverse_pasture/data_provider/__init__.py
def bovine_actor_for_actor_data(
    actor_id: str, data: ActorData
) -> Tuple[BovineActor, Actor]:
    """Builds the corresponding bovine actor and actor data

    :param actor_id: The actor id to be used, e.g. `https://pasture_one_actor/actor`
    :param data: The actor data
    :return: A tuple, where the first object is the Actor performing actions, and the second one the data object.
    """
    key_pair = data.key_pairs[0]
    bovine_actor = BovineActor(
        actor_id=actor_id,
        public_key_url=f"{actor_id}#{key_pair.name}",
        secret=key_pair.private,
    )

    actor_object = Actor(
        id=actor_id,
        preferred_username=data.user_part,
        name="Test Actor",
        inbox=f"{actor_id}/inbox",
        outbox=f"{actor_id}/outbox",
        public_key=key_pair.public,
        public_key_name=key_pair.name,
        summary=data.summary,
    )

    return bovine_actor, actor_object

models

ActorData

Bases: BaseModel

Represents an Actor

Parameters:

Name Type Description Default
actor_name str

The name of the actor used in the actor_id

required
key_pairs List[ActorKeyPair]

List of keys

[]
user_part str | None

User as part of the acct-uri for webfinger, None means webfinger lookup is not possible

None
summary str

Summary part of actor profile

''
requires_signed_get_for_actor bool

If true, validates the signature on GET /actor

False
requires_signed_post_for_inbox bool

If true, validates the signature on POST /inbox

False
Source code in fediverse_pasture/data_provider/models.py
class ActorData(BaseModel):
    """Represents an Actor"""

    actor_name: str = Field(
        description="""The name of the actor used in the actor_id"""
    )
    key_pairs: List[ActorKeyPair] = Field([], description="""List of keys""")
    user_part: str | None = Field(
        None,
        description="""User as part of the acct-uri for webfinger, None means webfinger lookup is not possible""",
    )

    summary: str = Field("", description="""Summary part of actor profile""")

    requires_signed_get_for_actor: bool = Field(
        False, description="""If true, validates the signature on `GET /actor`"""
    )
    requires_signed_post_for_inbox: bool = Field(
        False, description="""If true, validates the signature on `POST /inbox`"""
    )

ActorKeyPair

Bases: BaseModel

Represents a key pair for the actor

Parameters:

Name Type Description Default
name str

Name of the key used in the key id in the form key_id = f"{actor_id}#{name}"

required
public str

The PEM encoded public key

required
private str

The PEM encoded private key

required
Source code in fediverse_pasture/data_provider/models.py
class ActorKeyPair(BaseModel):
    """Represents a key pair for the actor"""

    name: str = Field(
        description="""Name of the key used in the key id in the form `key_id = f"{actor_id}#{name}"`"""
    )
    public: str = Field(description="""The PEM encoded public key""")
    private: str = Field(description="""The PEM encoded private key""")