Skip to content

Commit e8670ad

Browse files
authored
Support truststore when available and add tracing (#2232)
Supports minimal tracing and detection of working ssl cert.
1 parent 596a9d6 commit e8670ad

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

codex-rs/login/src/login_with_chatgpt.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,54 @@
4545
EXIT_CODE_WHEN_ADDRESS_ALREADY_IN_USE = 13
4646

4747
CA_CONTEXT = None
48+
CODEX_LOGIN_TRACE = os.environ.get("CODEX_LOGIN_TRACE", "false") in ["true", "1"]
49+
4850
try:
49-
import ssl
50-
import certifi as _certifi
5151

52-
CA_CONTEXT = ssl.create_default_context(cafile=_certifi.where())
52+
def trace(msg: str) -> None:
53+
if CODEX_LOGIN_TRACE:
54+
print(msg)
55+
56+
def attempt_request(method: str) -> bool:
57+
try:
58+
with urllib.request.urlopen(
59+
urllib.request.Request(
60+
f"{DEFAULT_ISSUER}/.well-known/openid-configuration",
61+
method="GET",
62+
),
63+
context=CA_CONTEXT,
64+
) as resp:
65+
if resp.status != 200:
66+
trace(f"Request using {method} failed: {resp.status}")
67+
return False
68+
69+
trace(f"Request using {method} succeeded")
70+
return True
71+
except Exception as e:
72+
trace(f"Request using {method} failed: {e}")
73+
return False
74+
75+
status = attempt_request("default settings")
76+
if not status:
77+
try:
78+
import truststore
79+
80+
truststore.inject_into_ssl()
81+
status = attempt_request("truststore")
82+
except Exception as e:
83+
trace(f"Failed to use truststore: {e}")
84+
85+
if not status:
86+
try:
87+
import ssl
88+
import certifi as _certifi
89+
90+
CA_CONTEXT = ssl.create_default_context(cafile=_certifi.where())
91+
status = attempt_request("certify")
92+
except Exception as e:
93+
trace(f"Failed to use certify: {e}")
94+
95+
5396
except Exception:
5497
pass
5598

0 commit comments

Comments
 (0)