| 1 | from __future__ import annotations |
| 2 | |
| 3 | from typing import Any |
| 4 | |
| 5 | from .client_authentication import add_client_authentication |
| 6 | from .rest import RestClient, RestClientOptions |
| 7 | from .types import RequestData, TimeoutType |
| 8 | |
| 9 | UNKNOWN_ERROR = "a0.sdk.internal.unknown" |
| 10 | |
| 11 | |
| 12 | class AuthenticationBase: |
| 13 | """Base authentication object providing simple REST methods. |
| 14 | |
| 15 | Args: |
| 16 | domain (str): The domain of your Auth0 tenant |
| 17 | client_id (str): Your application's client ID |
| 18 | client_secret (str, optional): Your application's client secret |
| 19 | client_assertion_signing_key (str, optional): Private key used to sign the client assertion JWT. |
| 20 | client_assertion_signing_alg (str, optional): Algorithm used to sign the client assertion JWT (defaults to 'RS256'). |
| 21 | telemetry (bool, optional): Enable or disable telemetry (defaults to True) |
| 22 | timeout (float or tuple, optional): Change the requests connect and read timeout. Pass a tuple to specify both values separately or a float to set both to it. (defaults to 5.0 for both) |
| 23 | protocol (str, optional): Useful for testing. (defaults to 'https') |
| 24 | client_info (dict, optional): Custom telemetry data for the Auth0-Client header. |
| 25 | When provided, overrides the default SDK telemetry. Useful for wrapper |
| 26 | SDKs that need to identify themselves. Ignored when telemetry is False. |
| 27 | """ |
| 28 | |
| 29 | def __init__( |
| 30 | self, |
| 31 | domain: str, |
| 32 | client_id: str, |
| 33 | client_secret: str | None = None, |
| 34 | client_assertion_signing_key: str | None = None, |
| 35 | client_assertion_signing_alg: str | None = None, |
| 36 | telemetry: bool = True, |
| 37 | timeout: TimeoutType = 5.0, |
| 38 | protocol: str = "https", |
| 39 | client_info: dict[str, Any] | None = None, |
| 40 | ) -> None: |
| 41 | self.domain = domain |
| 42 | self.client_id = client_id |
| 43 | self.client_secret = client_secret |
| 44 | self.client_assertion_signing_key = client_assertion_signing_key |
| 45 | self.client_assertion_signing_alg = client_assertion_signing_alg |
| 46 | self.protocol = protocol |
| 47 | self.client = RestClient( |
| 48 | None, |
| 49 | options=RestClientOptions( |
| 50 | telemetry=telemetry, timeout=timeout, retries=0, client_info=client_info |
| 51 | ), |
| 52 | ) |
| 53 | |
| 54 | def _add_client_authentication(self, payload: dict[str, Any]) -> dict[str, Any]: |
| 55 | return add_client_authentication( |
| 56 | payload, |
| 57 | self.domain, |
| 58 | self.client_id, |
| 59 | self.client_secret, |
| 60 | self.client_assertion_signing_key, |
| 61 | self.client_assertion_signing_alg, |
| 62 | ) |
| 63 | |
| 64 | def post( |
| 65 | self, |
| 66 | url: str, |
| 67 | data: RequestData | None = None, |
| 68 | headers: dict[str, str] | None = None, |
| 69 | ) -> Any: |
| 70 | return self.client.post(url, data=data, headers=headers) |
| 71 | |
| 72 | def authenticated_post( |
| 73 | self, |
| 74 | url: str, |
| 75 | data: dict[str, Any], |
| 76 | headers: dict[str, str] | None = None, |
| 77 | ) -> Any: |
| 78 | return self.client.post( |
| 79 | url, data=self._add_client_authentication(data), headers=headers |
| 80 | ) |
| 81 | |
| 82 | def get( |
| 83 | self, |
| 84 | url: str, |
| 85 | params: dict[str, Any] | None = None, |
| 86 | headers: dict[str, str] | None = None, |
| 87 | ) -> Any: |
| 88 | return self.client.get(url, params, headers) |