|
1 | 1 | import functools |
2 | 2 | import gc |
3 | 3 | import secrets |
4 | | -import time |
| 4 | +import ssl |
5 | 5 | from importlib import resources |
6 | 6 | from importlib.metadata import version |
7 | 7 | from pathlib import Path |
@@ -66,18 +66,34 @@ def _setup_httpx_client( |
66 | 66 | ca_bundle: Path | None = None, |
67 | 67 | disable_tls_verify: bool | None = False, |
68 | 68 | ): |
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 | + ) |
72 | 78 |
|
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: |
74 | 89 | ca = ca_bundle.expanduser() |
75 | 90 | if not ca.exists(): |
76 | 91 | 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 |
81 | 97 |
|
82 | 98 | def _load_key(self, key_name: str, path: Path | None = None): |
83 | 99 | if path is not None: |
@@ -266,7 +282,7 @@ def _verify_token( |
266 | 282 | claims_options=claims_options, |
267 | 283 | claims_params={"nonce": nonce}, |
268 | 284 | ) |
269 | | - decoded_token.validate(now=time.time()) |
| 285 | + decoded_token.validate(leeway=300) |
270 | 286 | return decoded_token |
271 | 287 |
|
272 | 288 |
|
|
0 commit comments