|
6 | 6 |
|
7 | 7 |
|
8 | 8 | class Authenticator(ABC): |
9 | | - """ |
10 | | - Abstract base class for authenticators. |
11 | | -
|
12 | | - This class defines the basic structure for any authenticator by requiring the implementation |
13 | | - of a method to retrieve authentication headers, and provides a way to store and retrieve the host. |
14 | | - """ |
15 | | - |
16 | | - def __init__(self, host: str): |
17 | 9 | """ |
18 | | - Initializes the Authenticator with the specified host. |
| 10 | + Abstract base class for authenticators. |
19 | 11 |
|
20 | | - :param host: The base URL or endpoint for the service. |
| 12 | + This class defines the basic structure for any authenticator by requiring the implementation |
| 13 | + of a method to retrieve authentication headers, and provides a way to store and retrieve the host. |
21 | 14 | """ |
22 | | - self.host = host |
23 | | - |
24 | | - @abstractmethod |
25 | | - def get_auth_headers(self) -> Dict[str, str]: |
26 | | - """ |
27 | | - Retrieves the authentication headers to be sent with requests. |
28 | | -
|
29 | | - Subclasses must override this method to return the appropriate headers. |
30 | 15 |
|
31 | | - :return: A dictionary mapping header names to their values. |
32 | | - """ |
33 | | - pass |
| 16 | + def __init__(self, host: str): |
| 17 | + """ |
| 18 | + Initializes the Authenticator with the specified host. |
34 | 19 |
|
35 | | - def get_host(self) -> str: |
36 | | - """ |
37 | | - Returns the stored host. |
| 20 | + :param host: The base URL or endpoint for the service. |
| 21 | + """ |
| 22 | + self.host = host |
38 | 23 |
|
39 | | - :return: The host as a string. |
40 | | - """ |
41 | | - return self.host |
| 24 | + @abstractmethod |
| 25 | + def get_auth_headers(self) -> Dict[str, str]: |
| 26 | + """ |
| 27 | + Retrieves the authentication headers to be sent with requests. |
42 | 28 |
|
| 29 | + Subclasses must override this method to return the appropriate headers. |
43 | 30 |
|
44 | | -class Token: |
45 | | - def __init__(self, access_token: str, expires_at: datetime): |
46 | | - """ |
47 | | - Initializes a new Token instance. |
| 31 | + :return: A dictionary mapping header names to their values. |
| 32 | + """ |
| 33 | + pass |
48 | 34 |
|
49 | | - Parameters: |
50 | | - - access_token (str): The JWT or OAuth token. |
51 | | - - expires_at (datetime): The expiration time of the token. It should be timezone-aware. |
52 | | - If a naive datetime is provided, it will be converted to an aware datetime in UTC. |
53 | | - """ |
54 | | - self.access_token = access_token |
| 35 | + def get_host(self) -> str: |
| 36 | + """ |
| 37 | + Returns the stored host. |
55 | 38 |
|
56 | | - # Ensure expires_at is timezone-aware. If naive, assume UTC. |
57 | | - if expires_at.tzinfo is None: |
58 | | - self.expires_at = expires_at.replace(tzinfo=timezone.utc) |
59 | | - else: |
60 | | - self.expires_at = expires_at |
| 39 | + :return: The host as a string. |
| 40 | + """ |
| 41 | + return self.host |
61 | 42 |
|
62 | | - def is_expired(self) -> bool: |
63 | | - """ |
64 | | - Checks if the token is expired by comparing the current UTC time |
65 | | - with the token's expiration time. |
66 | 43 |
|
67 | | - Returns: |
68 | | - - bool: True if expired, False otherwise. |
69 | | - """ |
70 | | - return datetime.now(timezone.utc) >= self.expires_at |
| 44 | +class Token: |
| 45 | + def __init__(self, access_token: str, expires_at: datetime): |
| 46 | + """ |
| 47 | + Initializes a new Token instance. |
| 48 | +
|
| 49 | + Parameters: |
| 50 | + - access_token (str): The JWT or OAuth token. |
| 51 | + - expires_at (datetime): The expiration time of the token. It should be timezone-aware. |
| 52 | + If a naive datetime is provided, it will be converted to an aware datetime in UTC. |
| 53 | + """ |
| 54 | + self.access_token = access_token |
| 55 | + |
| 56 | + # Ensure expires_at is timezone-aware. If naive, assume UTC. |
| 57 | + if expires_at.tzinfo is None: |
| 58 | + self.expires_at = expires_at.replace(tzinfo=timezone.utc) |
| 59 | + else: |
| 60 | + self.expires_at = expires_at |
| 61 | + |
| 62 | + def is_expired(self) -> bool: |
| 63 | + """ |
| 64 | + Checks if the token is expired by comparing the current UTC time |
| 65 | + with the token's expiration time. |
| 66 | +
|
| 67 | + Returns: |
| 68 | + - bool: True if expired, False otherwise. |
| 69 | + """ |
| 70 | + return datetime.now(timezone.utc) >= self.expires_at |
71 | 71 |
|
72 | 72 |
|
73 | 73 | T = TypeVar("T", bound="OAuthAuthenticatorBuilder") |
74 | 74 |
|
75 | 75 |
|
76 | 76 | class OAuthAuthenticatorBuilder(ABC, Generic[T]): |
77 | | - """ |
78 | | - Abstract builder class for constructing OAuth authenticator instances. |
79 | | -
|
80 | | - This builder provides common configuration options such as the OpenId instance and authentication scopes. |
81 | | - """ |
82 | | - |
83 | | - def __init__(self, host: str): |
84 | 77 | """ |
85 | | - Initializes the OAuthAuthenticatorBuilder with a given host. |
| 78 | + Abstract builder class for constructing OAuth authenticator instances. |
86 | 79 |
|
87 | | - :param host: The base URL for the OAuth provider. |
| 80 | + This builder provides common configuration options such as the OpenId instance and authentication scopes. |
88 | 81 | """ |
89 | | - self.open_id = OpenId(host) |
90 | | - self.auth_scopes = {"openid", "urn:zitadel:iam:org:project:id:zitadel:aud"} |
91 | 82 |
|
92 | | - def scopes(self: T, *auth_scopes: str) -> T: |
93 | | - """ |
94 | | - Sets the authentication scopes for the OAuth authenticator. |
| 83 | + def __init__(self, host: str): |
| 84 | + """ |
| 85 | + Initializes the OAuthAuthenticatorBuilder with a given host. |
95 | 86 |
|
96 | | - :param auth_scopes: A variable number of scope strings. |
97 | | - :return: The builder instance to allow for method chaining. |
98 | | - """ |
99 | | - self.auth_scopes = set(auth_scopes) |
100 | | - return self |
| 87 | + :param host: The base URL for the OAuth provider. |
| 88 | + """ |
| 89 | + self.open_id = OpenId(host) |
| 90 | + self.auth_scopes = {"openid", "urn:zitadel:iam:org:project:id:zitadel:aud"} |
| 91 | + |
| 92 | + def scopes(self: T, *auth_scopes: str) -> T: |
| 93 | + """ |
| 94 | + Sets the authentication scopes for the OAuth authenticator. |
| 95 | +
|
| 96 | + :param auth_scopes: A variable number of scope strings. |
| 97 | + :return: The builder instance to allow for method chaining. |
| 98 | + """ |
| 99 | + self.auth_scopes = set(auth_scopes) |
| 100 | + return self |
0 commit comments