Skip to content

Commit 33f266d

Browse files
authored
Use certifi certificate when available (openai#2042)
certifi has a more consistent set of Mozilla maintained root certificates
1 parent d0cf036 commit 33f266d

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

codex-rs/login/src/login_with_chatgpt.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@
4444

4545
EXIT_CODE_WHEN_ADDRESS_ALREADY_IN_USE = 13
4646

47+
CA_CONTEXT = None
48+
try:
49+
import ssl
50+
import certifi as _certifi
51+
52+
CA_CONTEXT = ssl.create_default_context(cafile=_certifi.where())
53+
except Exception:
54+
pass
55+
4756

4857
@dataclass
4958
class TokenData:
@@ -255,7 +264,8 @@ def _obtain_api_key(
255264
data=exchange_data,
256265
method="POST",
257266
headers={"Content-Type": "application/x-www-form-urlencoded"},
258-
)
267+
),
268+
context=CA_CONTEXT,
259269
) as resp:
260270
exchange_payload = json.loads(resp.read().decode())
261271
exchanged_access_token = exchange_payload["access_token"]
@@ -326,7 +336,8 @@ def _exchange_code(self, code: str) -> tuple[AuthBundle, str]:
326336
data=data,
327337
method="POST",
328338
headers={"Content-Type": "application/x-www-form-urlencoded"},
329-
)
339+
),
340+
context=CA_CONTEXT,
330341
) as resp:
331342
payload = json.loads(resp.read().decode())
332343

@@ -506,7 +517,7 @@ def maybe_redeem_credits(
506517
headers={"Content-Type": "application/json"},
507518
)
508519

509-
with urllib.request.urlopen(req) as resp:
520+
with urllib.request.urlopen(req, context=CA_CONTEXT) as resp:
510521
refresh_data = json.loads(resp.read().decode())
511522
new_id_token = refresh_data.get("id_token")
512523
new_id_claims = parse_id_token_claims(new_id_token or "")
@@ -596,7 +607,7 @@ def maybe_redeem_credits(
596607
headers={"Content-Type": "application/json"},
597608
)
598609

599-
with urllib.request.urlopen(req) as resp:
610+
with urllib.request.urlopen(req, context=CA_CONTEXT) as resp:
600611
redeem_data = json.loads(resp.read().decode())
601612

602613
granted = redeem_data.get("granted_chatgpt_subscriber_api_credits", 0)

0 commit comments

Comments
 (0)