remotivelabs.broker.auth

 1from __future__ import annotations
 2
 3from abc import ABC, abstractmethod
 4
 5
 6class AuthMethod(ABC):
 7    """Represents ways of authenticating to the Broker"""
 8
 9    @abstractmethod
10    def __str__(self) -> str:
11        """Return the auth value as a string"""
12
13    @property
14    @abstractmethod
15    def headers(self) -> list[tuple[str, str]] | None:
16        """Return HTTP authentication headers"""
17
18
19class NoAuth(AuthMethod):
20    """No authentication method needed"""
21
22    def __str__(self) -> str:
23        return ""
24
25    @property
26    def headers(self) -> None:
27        return None
28
29
30class ApiKeyAuth(AuthMethod):
31    """Api key authentication method (legacy)"""
32
33    def __init__(self, api_key: str):
34        self._api_key = api_key
35
36    def __str__(self) -> str:
37        return self._api_key
38
39    @property
40    def headers(self) -> list[tuple[str, str]]:
41        return [("x-api-key", str(self))]
42
43
44class TokenAuth(AuthMethod):
45    """Token based authentication method"""
46
47    def __init__(self, token: str):
48        self._token = token
49
50    def __str__(self) -> str:
51        return self._token
52
53    @property
54    def headers(self) -> list[tuple[str, str]]:
55        return [("authorization", f"Bearer {str(self)}"), ("x-api-key", str(self))]
class AuthMethod(abc.ABC):
 7class AuthMethod(ABC):
 8    """Represents ways of authenticating to the Broker"""
 9
10    @abstractmethod
11    def __str__(self) -> str:
12        """Return the auth value as a string"""
13
14    @property
15    @abstractmethod
16    def headers(self) -> list[tuple[str, str]] | None:
17        """Return HTTP authentication headers"""

Represents ways of authenticating to the Broker

headers: list[tuple[str, str]] | None
14    @property
15    @abstractmethod
16    def headers(self) -> list[tuple[str, str]] | None:
17        """Return HTTP authentication headers"""

Return HTTP authentication headers

class NoAuth(AuthMethod):
20class NoAuth(AuthMethod):
21    """No authentication method needed"""
22
23    def __str__(self) -> str:
24        return ""
25
26    @property
27    def headers(self) -> None:
28        return None

No authentication method needed

headers: None
26    @property
27    def headers(self) -> None:
28        return None

Return HTTP authentication headers

class ApiKeyAuth(AuthMethod):
31class ApiKeyAuth(AuthMethod):
32    """Api key authentication method (legacy)"""
33
34    def __init__(self, api_key: str):
35        self._api_key = api_key
36
37    def __str__(self) -> str:
38        return self._api_key
39
40    @property
41    def headers(self) -> list[tuple[str, str]]:
42        return [("x-api-key", str(self))]

Api key authentication method (legacy)

ApiKeyAuth(api_key: str)
34    def __init__(self, api_key: str):
35        self._api_key = api_key
headers: list[tuple[str, str]]
40    @property
41    def headers(self) -> list[tuple[str, str]]:
42        return [("x-api-key", str(self))]

Return HTTP authentication headers

class TokenAuth(AuthMethod):
45class TokenAuth(AuthMethod):
46    """Token based authentication method"""
47
48    def __init__(self, token: str):
49        self._token = token
50
51    def __str__(self) -> str:
52        return self._token
53
54    @property
55    def headers(self) -> list[tuple[str, str]]:
56        return [("authorization", f"Bearer {str(self)}"), ("x-api-key", str(self))]

Token based authentication method

TokenAuth(token: str)
48    def __init__(self, token: str):
49        self._token = token
headers: list[tuple[str, str]]
54    @property
55    def headers(self) -> list[tuple[str, str]]:
56        return [("authorization", f"Bearer {str(self)}"), ("x-api-key", str(self))]

Return HTTP authentication headers