Skip to content

hololinked.client.security.BasicSecurity

Bases: BaseModel

Basic Security Scheme with username and password.

The credentials are added into the Authorization header. Normally, you can instantiate this indirectly through the ClientFactory by passing username and password parameters, if the protocol supports it.

client = ClientFactory.http(
    url="http://localhost:9000/my-thing/resources/wot-td",
    security=BasicSecurity(
        username=os.getenv("USERNAME", "admin"),
        password=os.getenv("PASSWORD", "adminpass"),
        base64_encoding=True
    )
)
Source code in repo/hololinked/hololinked/client/security.py
class BasicSecurity(BaseModel):
    """
    Basic Security Scheme with username and password.

    The credentials are added into the `Authorization` header. Normally, you can instantiate this indirectly through the
    `ClientFactory` by passing `username` and `password` parameters, if the protocol supports it.

    ```python
    client = ClientFactory.http(
        url="http://localhost:9000/my-thing/resources/wot-td",
        security=BasicSecurity(
            username=os.getenv("USERNAME", "admin"),
            password=os.getenv("PASSWORD", "adminpass"),
            base64_encoding=True
        )
    )
    ```
    """

    http_header_name: str = "Authorization"
    """
    Name of the HTTP header to use for authentication, default is `Authorization`.
    Override this if the server expects the credentials in a different header.
    """

    _credentials: str = PrivateAttr()

    def __init__(self, username: str, password: str, use_base64: bool = True) -> None:
        """
        Initialize BasicSecurity with username and password.

        Parameters
        ----------
        username: str
            The username for basic authentication
        password: str
            The password for basic authentication
        use_base64: bool
            Whether to encode the credentials in base64, by default True
        """
        super().__init__()
        credentials = f"{username}:{password}"
        if use_base64:
            credentials = base64.b64encode(credentials.encode("utf-8")).decode("utf-8")
        self._credentials = f"Basic {credentials}"

    @property
    def http_header(self) -> str:
        """
        Value for the Authorization header.

        Contains the credentials prefixed with `Basic `, if necessary with base64 encoding.
        """
        return self._credentials

Attributes

http_header property

http_header: str

Value for the Authorization header.

Contains the credentials prefixed with Basic, if necessary with base64 encoding.

Functions

__init__

__init__(username: str, password: str, use_base64: bool = True) -> None

Initialize BasicSecurity with username and password.

Parameters:

Name Type Description Default

username

str

The username for basic authentication

required

password

str

The password for basic authentication

required

use_base64

bool

Whether to encode the credentials in base64, by default True

True
Source code in repo/hololinked/hololinked/client/security.py
def __init__(self, username: str, password: str, use_base64: bool = True) -> None:
    """
    Initialize BasicSecurity with username and password.

    Parameters
    ----------
    username: str
        The username for basic authentication
    password: str
        The password for basic authentication
    use_base64: bool
        Whether to encode the credentials in base64, by default True
    """
    super().__init__()
    credentials = f"{username}:{password}"
    if use_base64:
        credentials = base64.b64encode(credentials.encode("utf-8")).decode("utf-8")
    self._credentials = f"Basic {credentials}"