TokenFactory

Bases: Protocol

Token Factory interface for creating and validating authentication tokens.

Source code in src/alpha/interfaces/token_factory.py
@runtime_checkable
class TokenFactory(Protocol):
    """Token Factory interface for creating and validating authentication
    tokens.
    """

    def create(
        self,
        subject: str,
        payload: dict[str, str],
        not_before: datetime | None = None,
    ) -> str:
        """Create an authentication token for a subject.

        Parameters
        ----------
        subject
            The unique identifier for the subject.
        payload
            A dictionary containing payload data, such as an object containing
            user information.
        not_before
            An optional datetime indicating when the token becomes valid.

        Returns
        -------
        str
            The generated authentication token as a string.
        """
        ...

    def validate(
        self, token: str, options: dict[str, Any] | None = None
    ) -> bool:
        """Validate an authentication token.

        Parameters
        ----------
        token
            The authentication token to be validated.

        Returns
        -------
        bool
            True if the token is valid, False otherwise.
        """
        ...

    def get_payload(
        self, token: str, options: dict[str, bool] | None = None
    ) -> dict[str, str]:
        """Retrieve the payload from an authentication token.

        Parameters
        ----------
        token
            The authentication token from which to extract the payload.
        options
            A dictionary of options to customize the decoding behavior, such as
            enabling or disabling signature verification.

        Returns
        -------
        dict[str, str]
            A dictionary containing the payload data extracted from the
            token.
        """
        ...

Methods:

create

create(subject, payload, not_before=None)

Create an authentication token for a subject.

Parameters:
  • subject (str) –

    The unique identifier for the subject.

  • payload (dict[str, str]) –

    A dictionary containing payload data, such as an object containing user information.

  • not_before (datetime | None, default: None ) –

    An optional datetime indicating when the token becomes valid.

Returns:
  • str

    The generated authentication token as a string.

Source code in src/alpha/interfaces/token_factory.py
def create(
    self,
    subject: str,
    payload: dict[str, str],
    not_before: datetime | None = None,
) -> str:
    """Create an authentication token for a subject.

    Parameters
    ----------
    subject
        The unique identifier for the subject.
    payload
        A dictionary containing payload data, such as an object containing
        user information.
    not_before
        An optional datetime indicating when the token becomes valid.

    Returns
    -------
    str
        The generated authentication token as a string.
    """
    ...

validate

validate(token, options=None)

Validate an authentication token.

Parameters:
  • token (str) –

    The authentication token to be validated.

Returns:
  • bool

    True if the token is valid, False otherwise.

Source code in src/alpha/interfaces/token_factory.py
def validate(
    self, token: str, options: dict[str, Any] | None = None
) -> bool:
    """Validate an authentication token.

    Parameters
    ----------
    token
        The authentication token to be validated.

    Returns
    -------
    bool
        True if the token is valid, False otherwise.
    """
    ...

get_payload

get_payload(token, options=None)

Retrieve the payload from an authentication token.

Parameters:
  • token (str) –

    The authentication token from which to extract the payload.

  • options (dict[str, bool] | None, default: None ) –

    A dictionary of options to customize the decoding behavior, such as enabling or disabling signature verification.

Returns:
  • dict[str, str]

    A dictionary containing the payload data extracted from the token.

Source code in src/alpha/interfaces/token_factory.py
def get_payload(
    self, token: str, options: dict[str, bool] | None = None
) -> dict[str, str]:
    """Retrieve the payload from an authentication token.

    Parameters
    ----------
    token
        The authentication token from which to extract the payload.
    options
        A dictionary of options to customize the decoding behavior, such as
        enabling or disabling signature verification.

    Returns
    -------
    dict[str, str]
        A dictionary containing the payload data extracted from the
        token.
    """
    ...