Skip to content

Commit eb80a14

Browse files
authored
Merge pull request #15 from IoT-Inspector/simplify-client-setup
Simplify client setup
2 parents 0914748 + 50540d4 commit eb80a14

5 files changed

Lines changed: 19 additions & 75 deletions

File tree

iot_inspector_client/client.py

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,6 @@
2222
CLIENT_ID = "IoT Inspector Python SDK"
2323
TOKEN_NAMESPACE = "https://www.iot-inspector.com/"
2424

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-
3825

3926
def _login_required(func):
4027
@functools.wraps(func)
@@ -62,50 +49,43 @@ class Client:
6249
def __init__(
6350
self,
6451
api_url: str,
65-
id_token_public_key: Optional[Path] = None,
66-
tenant_token_public_key: Optional[Path] = None,
6752
ca_bundle: Optional[Path] = None,
53+
disable_tls_verify: Optional[bool] = False,
6854
):
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)
7256

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")
7660

77-
self._client = self._setup_httpx_client(api_url, ca_bundle)
7861
self._state = _LoginState()
7962

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+
8172
if ca_bundle is not None:
8273
ca = ca_bundle.expanduser()
8374
if not ca.exists():
8475
raise errors.InvalidCABundle
8576

8677
return httpx.Client(base_url=api_url, verify=str(ca))
8778
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:
9080
return httpx.Client(base_url=api_url, verify=str(ca))
9181

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):
9383
if path is not None:
9484
return path.read_bytes()
9585
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()
10989

11090
def login(self, email: str, password: str):
11191
nonce = secrets.token_urlsafe()

iot_inspector_client/keys/demo_id_token_public_key.pem

Lines changed: 0 additions & 9 deletions
This file was deleted.

iot_inspector_client/keys/demo_tenant_token_public_key.pem

Lines changed: 0 additions & 9 deletions
This file was deleted.

iot_inspector_client/keys/platform_id_token_public_key.pem

Lines changed: 0 additions & 9 deletions
This file was deleted.

iot_inspector_client/keys/platform_tenant_token_public_key.pem

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)