Skip to content

Authentication

Authentication

Bases: ABC

Base class for authentication methods.

Source code in src/signalbot/auth.py
class Authentication(ABC):
    """
    Base class for authentication methods.
    """

    @property
    @abstractmethod
    def header(self) -> str:
        """The authorization header value."""

    def write_header(self, headers: dict[str, str]) -> None:
        """Adds the authorization header to the given headers.

        Args:
            headers: The dictionary to which the authorization header will be added.
        """
        headers["Authorization"] = self.header

header abstractmethod property

header: str

The authorization header value.

write_header

write_header(headers: dict[str, str]) -> None

Adds the authorization header to the given headers.

Parameters:

Name Type Description Default
headers dict[str, str]

The dictionary to which the authorization header will be added.

required
Source code in src/signalbot/auth.py
def write_header(self, headers: dict[str, str]) -> None:
    """Adds the authorization header to the given headers.

    Args:
        headers: The dictionary to which the authorization header will be added.
    """
    headers["Authorization"] = self.header

BasicAuthentication dataclass

Bases: Authentication

Username and password based authentication.

Source code in src/signalbot/auth.py
@dataclass
class BasicAuthentication(Authentication):
    """Username and password based authentication."""

    username: str
    """The username for the authentication."""
    password: str
    """The password used for authentication."""

    @property
    def header(self) -> str:
        credentials = f"{self.username}:{self.password}".encode()
        credential_string = base64.b64encode(credentials).decode("utf-8")
        return f"Basic {credential_string}"

password instance-attribute

password: str

The password used for authentication.

username instance-attribute

username: str

The username for the authentication.

BearerAuthentication dataclass

Bases: Authentication

Token based authentication.

Source code in src/signalbot/auth.py
@dataclass
class BearerAuthentication(Authentication):
    """Token based authentication."""

    token: str
    """The token used for authentication."""

    @property
    def header(self) -> str:
        return f"Bearer {self.token}"

token instance-attribute

token: str

The token used for authentication.