Skip to content

Commit 5b339d2

Browse files
authored
Merge pull request #73 from onekey-sec/token-verify-leeway
Add leeway to token verification
2 parents 6c0f7a6 + df22c3e commit 5b339d2

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

onekey_client/client.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import functools
22
import gc
33
import secrets
4-
import time
4+
import ssl
55
from importlib import resources
66
from importlib.metadata import version
77
from pathlib import Path
@@ -66,18 +66,34 @@ def _setup_httpx_client(
6666
ca_bundle: Path | None = None,
6767
disable_tls_verify: bool | None = False,
6868
):
69-
headers = {"User-Agent": f"{APP_NAME}/{APP_VERSION}"}
70-
if disable_tls_verify:
71-
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+
)
7278

73-
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:
7489
ca = ca_bundle.expanduser()
7590
if not ca.exists():
7691
raise errors.InvalidCABundle
77-
78-
return httpx.Client(base_url=api_url, headers=headers, verify=str(ca))
79-
with resources.path(keys, "ca.pem") as ca:
80-
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
8197

8298
def _load_key(self, key_name: str, path: Path | None = None):
8399
if path is not None:
@@ -266,7 +282,7 @@ def _verify_token(
266282
claims_options=claims_options,
267283
claims_params={"nonce": nonce},
268284
)
269-
decoded_token.validate(now=time.time())
285+
decoded_token.validate(leeway=300)
270286
return decoded_token
271287

272288

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "onekey_client"
3-
version = "2.6.0"
3+
version = "2.7.0"
44
description = "ONEKEY API client"
55
authors = [{ name = "ONEKEY", email = "support@onekey.com" }]
66
requires-python = ">=3.10"

0 commit comments

Comments
 (0)