Skip to content

Commit 0e05b2a

Browse files
committed
refact: Use ssl context for httpx
1 parent 1687d04 commit 0e05b2a

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

onekey_client/client.py

Lines changed: 24 additions & 4 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,37 @@ 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+
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+
)
78+
79+
def _create_ssl_context(
80+
self,
81+
ca_bundle: Path | None,
82+
disable_tls_verify: bool | None,
83+
) -> ssl.SSLContext:
84+
6985
if disable_tls_verify:
70-
return httpx.Client(base_url=api_url, headers=headers, verify=False) # noqa: S501 (TLS certificate validation disabled)
86+
context = ssl.create_default_context()
87+
context.check_hostname = False
88+
context.verify_mode = ssl.CERT_NONE
89+
return context
7190

7291
if ca_bundle is not None:
7392
ca = ca_bundle.expanduser()
7493
if not ca.exists():
7594
raise errors.InvalidCABundle
7695

77-
return httpx.Client(base_url=api_url, headers=headers, verify=str(ca))
96+
return ssl.create_default_context(cafile=str(ca))
97+
7898
with resources.path(keys, "ca.pem") as ca:
79-
return httpx.Client(base_url=api_url, headers=headers, verify=str(ca))
99+
return ssl.create_default_context(cafile=str(ca))
80100

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

0 commit comments

Comments
 (0)