Skip to content

Commit a89e661

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

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

onekey_client/client.py

Lines changed: 23 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,36 @@ 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:
6984
if disable_tls_verify:
70-
return httpx.Client(base_url=api_url, headers=headers, verify=False) # noqa: S501 (TLS certificate validation disabled)
85+
context = ssl.create_default_context()
86+
context.check_hostname = False
87+
context.verify_mode = ssl.CERT_NONE
88+
return context
7189

7290
if ca_bundle is not None:
7391
ca = ca_bundle.expanduser()
7492
if not ca.exists():
7593
raise errors.InvalidCABundle
7694

77-
return httpx.Client(base_url=api_url, headers=headers, verify=str(ca))
95+
return ssl.create_default_context(cafile=str(ca))
96+
7897
with resources.path(keys, "ca.pem") as ca:
79-
return httpx.Client(base_url=api_url, headers=headers, verify=str(ca))
98+
return ssl.create_default_context(cafile=str(ca))
8099

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

0 commit comments

Comments
 (0)