|
22 | 22 | CLIENT_ID = "IoT Inspector Python SDK" |
23 | 23 | TOKEN_NAMESPACE = "https://www.iot-inspector.com/" |
24 | 24 |
|
25 | | -IOT_INSPECTOR_KEYS = { |
26 | | - "demo.iot-inspector.com": { |
27 | | - "id_token_public_key": "demo_id_token_public_key.pem", |
28 | | - "tenant_token_public_key": "demo_tenant_token_public_key.pem", |
29 | | - "ca_path": "ca.pem", |
30 | | - }, |
31 | | - "*.iot-inspector.com": { |
32 | | - "id_token_public_key": "platform_id_token_public_key.pem", |
33 | | - "tenant_token_public_key": "platform_tenant_token_public_key.pem", |
34 | | - "ca_path": "ca.pem", |
35 | | - }, |
36 | | -} |
37 | | - |
38 | 25 |
|
39 | 26 | def _login_required(func): |
40 | 27 | @functools.wraps(func) |
@@ -62,50 +49,43 @@ class Client: |
62 | 49 | def __init__( |
63 | 50 | self, |
64 | 51 | api_url: str, |
65 | | - id_token_public_key: Optional[Path] = None, |
66 | | - tenant_token_public_key: Optional[Path] = None, |
67 | 52 | ca_bundle: Optional[Path] = None, |
| 53 | + disable_tls_verify: Optional[bool] = False, |
68 | 54 | ): |
69 | | - self._id_token_public_key = self._load_key( |
70 | | - api_url, "id_token_public_key", id_token_public_key |
71 | | - ) |
| 55 | + self._client = self._setup_httpx_client(api_url, ca_bundle, disable_tls_verify) |
72 | 56 |
|
73 | | - self._tenant_token_public_key = self._load_key( |
74 | | - api_url, "tenant_token_public_key", tenant_token_public_key |
75 | | - ) |
| 57 | + self._id_token_public_key = self._load_key("id-token-public-key") |
| 58 | + |
| 59 | + self._tenant_token_public_key = self._load_key("tenant-token-public-key") |
76 | 60 |
|
77 | | - self._client = self._setup_httpx_client(api_url, ca_bundle) |
78 | 61 | self._state = _LoginState() |
79 | 62 |
|
80 | | - def _setup_httpx_client(self, api_url: str, ca_bundle: Optional[Path] = None): |
| 63 | + def _setup_httpx_client( |
| 64 | + self, |
| 65 | + api_url: str, |
| 66 | + ca_bundle: Optional[Path] = None, |
| 67 | + disable_tls_verify: Optional[bool] = False, |
| 68 | + ): |
| 69 | + if disable_tls_verify: |
| 70 | + return httpx.Client(base_url=api_url, verify=False) |
| 71 | + |
81 | 72 | if ca_bundle is not None: |
82 | 73 | ca = ca_bundle.expanduser() |
83 | 74 | if not ca.exists(): |
84 | 75 | raise errors.InvalidCABundle |
85 | 76 |
|
86 | 77 | return httpx.Client(base_url=api_url, verify=str(ca)) |
87 | 78 | else: |
88 | | - resource_name = self._get_resource_name(api_url, "ca_path") |
89 | | - with resources.path(keys, resource_name) as ca: |
| 79 | + with resources.path(keys, "ca.pem") as ca: |
90 | 80 | return httpx.Client(base_url=api_url, verify=str(ca)) |
91 | 81 |
|
92 | | - def _load_key(self, api_url: str, key_name: str, path: Optional[Path] = None): |
| 82 | + def _load_key(self, key_name: str, path: Optional[Path] = None): |
93 | 83 | if path is not None: |
94 | 84 | return path.read_bytes() |
95 | 85 | else: |
96 | | - resource_name = self._get_resource_name(api_url, key_name) |
97 | | - return resources.read_binary(keys, resource_name) |
98 | | - |
99 | | - @staticmethod |
100 | | - def _get_resource_name(api_url: str, key_name: str): |
101 | | - domain = httpx.URL(api_url).host |
102 | | - try: |
103 | | - return IOT_INSPECTOR_KEYS[domain][key_name] |
104 | | - except KeyError: |
105 | | - # We try to match on a wildcard domain as it is used for *.iot-inspector.com domain |
106 | | - domain_base = domain.split(".", maxsplit=1)[-1] |
107 | | - wildcard_domain = f"*.{domain_base}" |
108 | | - return IOT_INSPECTOR_KEYS.get(wildcard_domain, {}).get(key_name) |
| 86 | + response = self._client.get(f"/{key_name}.pem") |
| 87 | + response.raise_for_status() |
| 88 | + return response.read() |
109 | 89 |
|
110 | 90 | def login(self, email: str, password: str): |
111 | 91 | nonce = secrets.token_urlsafe() |
|
0 commit comments