Skip to content

Authentication

Authentication

Bases: ABC

Base class for authentication methods.

Source code in src/signalbot/auth.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
18
19
20
21
22
23
24
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.

Attributes:

Name Type Description
username str

The username for the authentication.

password str

The password used for authentication.

Source code in src/signalbot/auth.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@dataclass
class BasicAuthentication(Authentication):
    """
    Username and password based authentication.

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

    username: str
    password: str

    @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}"

BearerAuthentication dataclass

Bases: Authentication

Token based authentication.

Attributes:

Name Type Description
token str

The token used for authentication.

Source code in src/signalbot/auth.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@dataclass
class BearerAuthentication(Authentication):
    """
    Token based authentication.

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

    token: str

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