Skip to content

Commit 930662e

Browse files
committed
refact: Use ssl context for httpx
1 parent 1687d04 commit 930662e

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

onekey_client/client.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import functools
22
import gc
33
import secrets
4+
import ssl
45
from importlib import resources
56
from importlib.metadata import version
67
from pathlib import Path
@@ -65,18 +66,34 @@ def _setup_httpx_client(
6566
ca_bundle: Path | None = None,
6667
disable_tls_verify: bool | None = False,
6768
):
68-
headers = {"User-Agent": f"{APP_NAME}/{APP_VERSION}"}
69-
if disable_tls_verify:
70-
return httpx.Client(base_url=api_url, headers=headers, verify=False) # noqa: S501 (TLS certificate validation disabled)
69+
return httpx.Client(
70+
base_url=api_url,
71+
headers={
72+
"User-Agent": f"{APP_NAME}/{APP_VERSION}",
73+
},
74+
verify=self._create_ssl_context(
75+
ca_bundle=ca_bundle, disable_tls_verify=disable_tls_verify
76+
),
77+
)
7178

72-
if ca_bundle is not None:
79+
def _create_ssl_context(
80+
self,
81+
ca_bundle: Path | None,
82+
disable_tls_verify: bool | None,
83+
) -> ssl.SSLContext:
84+
context = ssl.create_default_context()
85+
if disable_tls_verify:
86+
context.check_hostname = False
87+
context.verify_mode = ssl.CERT_NONE
88+
elif ca_bundle is not None:
7389
ca = ca_bundle.expanduser()
7490
if not ca.exists():
7591
raise errors.InvalidCABundle
76-
77-
return httpx.Client(base_url=api_url, headers=headers, verify=str(ca))
78-
with resources.path(keys, "ca.pem") as ca:
79-
return httpx.Client(base_url=api_url, headers=headers, verify=str(ca))
92+
context.load_verify_locations(cafile=ca)
93+
else:
94+
with resources.path(keys, "ca.pem") as ca:
95+
context.load_verify_locations(cafile=ca)
96+
return context
8097

8198
def _load_key(self, key_name: str, path: Path | None = None):
8299
if path is not None:

0 commit comments

Comments
 (0)